【问题标题】:Adding SendersEmailAddress [duplicate]添加发件人电子邮件地址[重复]
【发布时间】:2020-10-09 01:26:00
【问题描述】:

这是我在 stackoverflow 上的第一篇文章,我遇到了一个问题,如果发件人的域不是来自我公司的域(我公司的域,即 info@mycompany.com),我的语法只保存电子邮件附件并且只保存附件是从 yahoo、gmail 等收到的。如何编辑我的代码以便它保存所有附件而不管域是什么?

 Private Sub btn_extractemails_Click()

Dim OlApp As Object
Dim OlMail As Object
Dim OlItems As Object
Dim OlFolder As Object
Dim j As Integer
Dim strFolder As String
    
On Error Resume Next
Set OlApp = GetObject(, "Outlook.Application")

If Err.Number = 429 Then
    Set OlApp = CreateObject("Outlook.Application")
End If

strFolder = "C:\Users\p00113357\Desktop\Attaches\"

Set OlFolder = OlApp.getnamespace("MAPI").Folders("EEO").Folders("Inbox").Folders("Incoming")

Set OlItems = OlFolder.Items

For Each OlMail In OlItems
    If OlMail.Attachments.Count > 0 Then
        For j = 1 To OlMail.Attachments.Count
            OlMail.Attachments.Item(j).SaveAsFile strFolder & "\" & OlMail.SenderEmailAddress & OlMail.Attachments.Item(j).Filename
        Next j
    End If
Next

Set OlFolder = Nothing
Set OlItems = Nothing
Set OlMail = Nothing
Set OlApp = Nothing

MsgBox "Done", vbInformation

End Sub

----------------------更新------------- -----------------------------

我包含代码来显示我的错误,当我运行它时,我没有看到任何错误代码弹出。来自内部电子邮件域(即 info@mycompany.com)的附件未下载到我指定的文件夹而来自外部电子邮件域(雅虎、gmail 等)的附件正在下载的问题仍在发生。以下是我获取错误代码的尝试。

Private Sub btn_extractemails_Click()

Dim OlApp As Object
Dim OlMail As Object
Dim OlItems As Object
Dim OlFolder As Object
Dim j As Integer
Dim strFolder As String

Set OlApp = GetObject(, "Outlook.Application")

If Err.Number = 429 Then
    Set OlApp = CreateObject("Outlook.Application")
End If

**On Error GoTo 0
On Error Resume Next**
strFolder = "C:\Users\p00113357\Desktop\Attaches\"

Set OlFolder = OlApp.getnamespace("MAPI").Folders("EEO").Folders("Inbox")

Set OlItems = OlFolder.Items

For Each OlMail In OlItems

    If OlMail.Attachments.Count > 0 Then
        For j = 1 To OlMail.Attachments.Count
            OlMail.Attachments.Item(j).SaveAsFile strFolder & "\" & OlMail.SenderEmailAddress & "-" & OlMail.Attachments.Item(j).Filename
        Next j
    End If
Next

Set OlFolder = Nothing
Set OlItems = Nothing
Set OlMail = Nothing
Set OlApp = Nothing

MsgBox "Done", vbInformation

End Sub

谢谢

---------------更新------- --------------------------

按照@notin 建议的帖子中的说明,我尝试编辑我的代码,但它仍然无法正常工作。我稍微改变了语法,在将代码行按正确的顺序/位置放置后它就起作用了,感谢@notin 和 Josh P 在我的第一篇文章中提供的帮助。展望未来,我将在发布时遵循最佳实践

Private Sub btn_extractemails_Click()

Dim OlApp As Object
Dim OlMail As Object
Dim OlItems As Object
Dim OlFolder As Object
Dim j As Integer
Dim strFolder As String

Set OlApp = GetObject(, "Outlook.Application")

If Err.Number = 429 Then
    Set OlApp = CreateObject("Outlook.Application")
End If

On Error GoTo 0
On Error Resume Next
strFolder = "C:\Users\p00113357\Desktop\Attaches\"

Set OlFolder = OlApp.getnamespace("MAPI").Folders("EEO").Folders("Inbox")

Set OlItems = OlFolder.Items

For Each OlMail In OlItems

    If OlMail.Attachments.Count > 0 Then
     If OlMail.SenderEmailType = "EX" Then
        For j = 1 To OlMail.Attachments.Count
                    OlMail.Attachments.Item(j).SaveAsFile strFolder & "\" & OlMail.Sender.GetExchangeUser().PrimarySmtpAddress & "-" & OlMail.Attachments.Item(j).Filename
        Next j
     End If
    End If
Next

Set OlFolder = Nothing
Set OlItems = Nothing
Set OlMail = Nothing
Set OlApp = Nothing

MsgBox "Done", vbInformation

End Sub

【问题讨论】:

  • 你有它 itm.SenderEmailAddress 但我在你的示例代码中没有看到你声明了 itm 对象的任何地方。会是 OlMail.SenderEmailAddress 吗?
  • 嗨乔希,你的建议奏效了。我现在唯一的问题是它只保存来自hotmail、yahoo、aol、gmail等域的附件,并绕过我公司的电子邮件域。我的代码中是否存在不允许保存来自某些电子邮件域的附件的内容?
  • 嘿,兄弟,仅供参考,最好的做法可能是在收到答案后不要更改您的问题。就这样当人们遇到你的问题时,他们收到的答案是有意义的。但是关于你的新问题......现在忘记寻找附件。当您浏览您的文件夹时,您能看到从您的域发送的电子邮件吗?因为如果您根本看不到它们,那就解释了为什么您无法保存附件。
  • 乔希,我真诚的道歉,我完全理解,我在重新发布我的语法和新问题时遇到了困难,但将来我不会这样做。是的,我可以,所以我的收件箱包括从我的同事和我们组织外部的人发送的电子邮件,但是,我的代码仅从我的组织/公司外部的人那里下载附件。再次感谢您的帮助。

标签: excel vba outlook email-attachments naming


【解决方案1】:

SenderEmailAddress property does not contain a standard email address for internal contacts

If OlMail.SenderEmailType = "EX" then 改为使用OlMail.Sender.GetExchangeUser().PrimarySmtpAddress

Option Explicit
            
Private Sub btn_extractemails_Click()

Dim OlApp As Object
Dim OlMail As Object
Dim OlItems As Object
Dim OlFolder As Object
Dim j As Integer
Dim strFolder As String

Dim strPathFile As String


' This is a rare valid use of
On Error Resume Next
' Bypass expected error if Outlook is not open

Set OlApp = GetObject(, "Outlook.Application")

If err.Number = 429 Then
    Set OlApp = CreateObject("Outlook.Application")
End If

' Return to normal error handling to see unexpected errors
On Error GoTo 0


strFolder = "C:\Users\p00113357\Desktop\Attaches\"

Set OlFolder = OlApp.GetNamespace("MAPI").folders("EEO").folders("Inbox")

Set OlItems = OlFolder.Items

For Each OlMail In OlItems

    If OlMail.Attachments.Count > 0 Then
        
        ' The expectation is internal addresses will not have the @ type format
        '  instead the format will be similar to "/O=APPLE/CN=RECIPIENTS/CN=JOBSS6738"
        '  https://stackoverflow.com/questions/36900156/senderemailaddress-property-does-not-contain-a-standard-email-address-for-intern
        Debug.Print "OlMail.SenderEmailAddress: " & OlMail.SenderEmailAddress
        
        For j = 1 To OlMail.Attachments.Count
        
            ' Note the double backslash has no impact. Do not fix. Better to have two than none.
            Debug.Print strFolder & "\" & OlMail.SenderEmailAddress & "-" & OlMail.Attachments.Item(j).Filename
            
            ' If the SenderEmailAddress is in a format similar to "/O=APPLE/CN=RECIPIENTS/CN=JOBSS6738" then
            '   Error: "Cannot save the attachment. Path does not exist. Verify the path is correct."
            
            OlMail.Attachments.Item(j).SaveAsFile strFolder & "\" & OlMail.SenderEmailAddress & "-" & OlMail.Attachments.Item(j).Filename
            
        Next j
    End If
Next

Set OlFolder = Nothing
Set OlItems = Nothing
Set OlMail = Nothing
Set OlApp = Nothing

MsgBox "Done", vbInformation

End Sub

【讨论】:

  • 我之前使用 OlMail.Sender.GetExchangeUser().PrimarySmtpAddress 尝试过,但无济于事。我只是不确定我是否将语法放在正确的区域。我还尝试运行您发送的代码,但收到一条错误消息“无法保存附件,路径不存在”我将编辑我的帖子以显示修改,谢谢。
  • “没用”是什么意思?您是否收到错误消息?这是什么?
猜你喜欢
  • 1970-01-01
  • 2016-01-21
  • 2017-07-21
  • 2012-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-27
  • 1970-01-01
相关资源
最近更新 更多