【问题标题】:Outlook does not seem to be detecting Excel AttachmentsOutlook 似乎没有检测到 Excel 附件
【发布时间】:2016-06-14 15:18:09
【问题描述】:

我目前的目标是,当有人给我发邮件,主题为"SES Gas Matrix" 我的outlook会从邮件中取出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 文档以便绕过它的不同方式。

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

提前谢谢你,

【问题讨论】:

  • 你有没有单步执行代码来查看是否执行了正确的行?顺便说一句,如果此代码在 Outlook 中,您将拥有一大堆不合格的对象引用,这些引用将使您运行孤立的不可见 excel 实例。
  • 我已经通过代码,它拿起一个附件(这是一个 jpeg 签名),但它认为这是唯一的附件。所以它会直接跳过 for 循环中的另存为命令。然后 FilePath 是空的,当您尝试打开不存在的文件时它会出错。我不确定如何修复不合格的对象。
  • 那么Msg.Attachments.Count 等于什么? 1 个?
  • 表示两个附件。有没有办法弄清楚这两者是什么?
  • 在循环中检查msgattach.FileName

标签: excel vba outlook


【解决方案1】:

感谢罗里,

我的 for 循环中有一个额外的“Exit 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

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

【讨论】:

    猜你喜欢
    • 2021-04-01
    • 2012-05-10
    • 1970-01-01
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    • 2010-11-13
    相关资源
    最近更新 更多