【问题标题】:Download Attachment from Outlook VBA从 Outlook VBA 下载附件
【发布时间】:2020-03-17 15:52:54
【问题描述】:

我在 Outlook VBA ThisOutlookSession 中有这段代码,它没有给出任何错误,但没有下载附件。任何想法

Public WithEvents objInboxItems As Outlook.Items
Private Sub Application_Startup()
   Set objInboxItems = Session.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub objInboxItems_ItemAdd(ByVal Item As Object)
   Dim objMail As Outlook.MailItem
   Dim strSenderAddress As String
   Dim strSenderDomain As String
   Dim objAttachment As Attachment
   Dim strFolderPath As String
   Dim strFileName As String

   If Item.Class = olMail Then
      Set objMail = Item

      'Get sender domain
      strSenderAddress = objMail.SenderEmailAddress
      strSenderDomain = Right(strSenderAddress, Len(strSenderAddress) - InStr(strSenderAddress, "@"))

      'Change to the specific domain as per your needs
      If strSenderDomain = "gmail.com" Then
       If strSenderAddress = "vs@gmail.com" Then
         If objMail.Attachments.Count > 0 Then
            For Each objAttachment In objMail.Attachments
                strFolderPath = "E:\Cisco - Qutar\Performance Report Automation\"
                strFileName = objMail.Subject & " " & Chr(45) & " " & objAttachment.FileName
                objAttachment.SaveAsFile strFolderPath & strFileName
            Next
         End If
      End If
   End If
 End If
End Sub

已更新

【问题讨论】:

  • 嗨。如果 strSenderDomain = "vs@gmail.com" 然后你希望得到 vs@gmail.com,但是 var strSenderDomain 与域 gmail.com 相同...将 "vs@gmail.com" 更改为 " gmail.com”并且应该可以工作
  • 嗨 Luis,我只需要一个特定的发件人,如果这样做的话,可能会有来自不同用户的电子邮件,它会下载来自 gmail 发件人的所有邮件。 ''' 如果 strSenderDomain = "gmail.com"',仍然没有下载
  • 在这种情况下更改 --> If strSenderDomain = "vs@gmail.com" Then If strSenderAddress = "vs@gmail.com" Then
  • 改了,还是不行,''' If strSenderAddress = "vs@gmail.com" Then '''
  • if 是否输入?并在每个?

标签: vba outlook


【解决方案1】:

如下设置你的Application_Startup()

Option Explicit
Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
    Dim olNs As Outlook.NameSpace
    Set olNs = Application.GetNamespace("MAPI")

    Dim Inbox  As Outlook.MAPIFolder
    Set Inbox = olNs.GetDefaultFolder(olFolderInbox)

    Set Items = Inbox.Items
End Sub

【讨论】:

  • 错误'''设置项目 = Inbox.Items '''对象未定义
  • 对象已定义,请查看答案代码@vmjan02 上的第二行
  • Set Items = Inbox.Items 错误仍然与未定义变量相同。
猜你喜欢
  • 1970-01-01
  • 2022-01-19
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
  • 2018-08-18
  • 2017-02-01
相关资源
最近更新 更多