What my current goal is that when someone sends me an email with the subject
"SES Gas Matrix" My outlook will take the excel document off the email, save it in a specified location, open the excel, print it to pdf in two locations and then copy various cells off the excel document.
Then kill the excel document.
The issue I am running into is that my code works when I take document sent to me in the morning, download it to may computer and then attach it to an email back to myself. This is working perfectly fine.
The issue is when the email comes from the source, Outlook doesn’t seem to be picking up the fact that there is an excel attachment on the email. I was picking up the jpeg in the email signature, just not the excel document. This why I have added in the if statement to filter for only excel documents.
This has fixed the issue of pulling the jpeg, but it still cannot seem to find the excel document that is clearly there. I can manually download it as well.
Here is the code I am currently using:
Private WithEvents myOlItems As Outlook.Items Private Sub Application_Startup() Dim olApp As Outlook.Application Dim objNS As Outlook.NameSpace Set olApp = Outlook.Application Set objNS = olApp.GetNamespace("MAPI") Set myOlItems = objNS.GetDefaultFolder(olFolderInbox).Items End Sub Private Sub myOlItems_ItemAdd(ByVal item As Object) 'On Error Resume Next Dim Msg As Outlook.MailItem Dim msgattach As Object Dim wb As Workbook Dim myXLApp As Excel.Application If TypeName(item) = "MailItem" Then Set Msg = item Dim wbtemp As Workbook Dim testcode As Workbook If Left(Msg.Subject, 14) = "SES Gas Matrix" Then Set myXLApp = CreateObject("Excel.Application") myXLApp.Visible = True If Msg.Attachments.Count <> 0 Then For Each msgattach In Msg.Attachments If Right(msgattach.FileName, 5) = ".xlsx" Then FilePath = "G:\Betts\Floor Matricies\FIFOs\" & Format(Now(), "YYYYMMDD") & " - " & "Gas Rates" & Right(msgattach.FileName, 5) msgattach.SaveAsFile FilePath End If Exit For Next End If Set wbtemp = Workbooks.Open(FilePath) wbtemp.Activate FilePathtwo = Left(FilePath, Len(FilePath) - 5) ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _ FilePathtwo & ".pdf" _ , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _ :=False, OpenAfterPublish:=False FilePathone = "http://intranet/Pricing%20and%20Rates/Floor%20Matrices/FIFOs/" & Format(Now(), "YYYYMMDD") & "%20-%20Gas%20Rates.pdf" ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _ FilePathone _ , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _ :=False, OpenAfterPublish:=False wbtemp.Sheets("Sheet1").Range("B5:L9").Copy Set testcode = Workbooks.Open(FileName:="G:\Betts\ReturnOnInvestment_Master_Backup Testcode.xlsm", UpdateLinks:=3) testcode.Sheets("Floor Pricing").Range("A44").PasteSpecial xlPasteValues testcode.Close savechanges:=True wbtemp.Close Kill (FilePath) End If End If
I think it has to be something not necessarily with the code but how the excel document is attached. I am not very familiar with different ways that the excel document could be attached so that it gets by-passed.
Any help would be greatly appreciated. I am a beginner and more explanation is always welcomed.
Thank you in advance,
All Credit to Rory,
I had an additional “Exit For” in my for loop which was causing the loop to exit after it picked up the jpeg image so it seemed like it was not reading the excel document. Fixed For loop:
If Msg.Attachments.Count <> 0 Then For Each msgattach In Msg.Attachments If Right(msgattach.FileName, 5) = ".xlsx" Then FilePath = "G:\Betts\Floor Matricies\FIFOs\" & Format(Now(), "YYYYMMDD") & " - " & "Gas Rates" & Right(msgattach.FileName, 5) msgattach.SaveAsFile FilePath End If Next End If
Best of luck to everyone. Thank you again.