Outlook似乎没有检测到Excel附件

我目前的目标是什么时候有人给我发送一封邮件,内容是"SES Gas Matrix"我的观点是将Excel文件从电子邮件中删除,保存在指定位置,打开Excel,在两个位置打印PDF然后复制Excel文档中的各种单元格。

然后杀掉excel文件。

我遇到的问题是我的代码在早上把文件发给我的时候起作用,把它下载到可能的计算机上,然后把它附加到我自己的电子邮件中。 这工作得很好。

问题是,当电子邮件来自源,Outlook似乎并没有拿起电子邮件的Excel附件的事实。 我正在拿起电子邮件签名中的jpeg,而不是excel文档。 这就是为什么我在if语句中添加了只为excel文档进行过滤的原因。

这已经解决了拉JPEG的问题,但似乎还找不到明显存在的Excel文件。 我也可以手动下载它。

这是我目前使用的代码:

  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 

我认为它不一定是代码,但是excel文档是如何附加的。 我不太熟悉excel文档可以附加的不同方式,以便绕过。

任何帮助将不胜感激。 我是一个初学者,总是欢迎更多的解释。

先谢谢你,

所有信贷罗里,

我有一个额外的“退出”在我的for循环这是导致循环退出后,它拿起了JPEG图像,所以它好像不是读取Excel文档。 固定For循环:

  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 

祝大家好运。 再次感谢你。