来自Excel单元的电子邮件地址返回该地址无效的消息

发布于 2025-02-13 09:49:28 字数 1159 浏览 0 评论 0原文

我创建了一个Excel宏,该宏可以使用模板生成电子邮件,并将其保存在草稿文件夹中,以便由我的团队成员发送。

是时候发送消息了,它会返回地址无效的消息。
地址正确。手动填充电子邮件是发送的。

Sub create_email()

Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim OutAccount As Outlook.Account

For linha = 2 To 16
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItemFromTemplate(' .oft file path)
    Set OutAccount = OutApp.Session.Accounts.Item(2)
    With OutMail
        .To = Cells(linha, 1).Value
        .CC = ' CC address
        .BCC = ' BCC address
        .Subject = ' text  & Cells(linha, 2)
        strFile = ""
        strFile = Dir(" ' file path ")  
        If strFile <> "" Then
            .Attachments.Add (ThisWorkbook.Path & "\Base - " & Cells(linha, 2).Value & ".xlsx")
            .SendUsingAccount = OutAccount
            .Save
          Else
            GoTo Fim
        End If
Fim:
        
    End With
Next

Set OutMail = Nothing
Set OutApp = Nothing
Set OutAccount = Nothing
End Sub

对于每个电子邮件地址,该代码都会陷入此问题。如果我将其更改为一个联系人名称组,则会创建相同的问题。

所有电子邮件都显示在草稿文件夹中,正确分开。

I created an Excel macro that generates e-mails using a template and saves it in draft folders so it can be sent by a member of my team.

When it's time to send the message, it returns the message that the address is invalid.
The address is correct. Filling it manually the e-mail is sent.

Sub create_email()

Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim OutAccount As Outlook.Account

For linha = 2 To 16
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItemFromTemplate(' .oft file path)
    Set OutAccount = OutApp.Session.Accounts.Item(2)
    With OutMail
        .To = Cells(linha, 1).Value
        .CC = ' CC address
        .BCC = ' BCC address
        .Subject = ' text  & Cells(linha, 2)
        strFile = ""
        strFile = Dir(" ' file path ")  
        If strFile <> "" Then
            .Attachments.Add (ThisWorkbook.Path & "\Base - " & Cells(linha, 2).Value & ".xlsx")
            .SendUsingAccount = OutAccount
            .Save
          Else
            GoTo Fim
        End If
Fim:
        
    End With
Next

Set OutMail = Nothing
Set OutApp = Nothing
Set OutAccount = Nothing
End Sub

The code runs into this issue for every e-mail address. If I change it for a contact name group, it creates the same issue.

All e-mails appear in the draft folder correctly separated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长亭外,古道边 2025-02-20 09:49:31

resolveall 可能会触发地址更新。

注意:当收件人在电子邮件地址格式中不是时,链接中的示例中的测试适用。

Option Explicit

Sub create_email()

    Dim OutApp As Outlook.Application
    Dim OutAccount As Outlook.Account
    Dim OutMail As Outlook.MailItem
    
    Dim myRecipients As Outlook.Recipients
    'Dim myRecipient As Outlook.Recipient
    
    Dim linha As Long
    
    Set OutApp = CreateObject("Outlook.Application")
    Set OutAccount = OutApp.Session.Accounts.Item(1)
    Debug.Print OutAccount
    
    For linha = 2 To 16
    
        'Set OutMail = OutApp.CreateItemFromTemplate(" .oft file path")
        Set OutMail = OutApp.CreateItem(olMailItem)
        
        With OutMail
        
            .To = Cells(linha, 1).Value
            .Subject = "Test " & linha
            
            Set myRecipients = OutMail.Recipients
            
            ' May trigger an address update
            myRecipients.ResolveAll
            
            ' Not for email address format. Resolved always True.
            '  https://learn.microsoft.com/en-us/office/vba/api/Outlook.Recipients.ResolveAll
            '
            'If Not myRecipients.ResolveAll Then
            '    For Each myRecipient In myRecipients
            '        If Not myRecipient.Resolved Then
            '            MsgBox myRecipient.Name
            '        End If
            '        Set myRecipient = Nothing
            '    Next
            'End If
            
            .Save
            '.Send
            
        End With
        
        Set OutMail = Nothing
        
    Next
    
    Set OutApp = Nothing
    Set OutAccount = Nothing
    
    Set myRecipients = Nothing
    
End Sub

ResolveAll may trigger an address update.

Note: The test in the example in the link applies when the recipient is not in email address format.

Option Explicit

Sub create_email()

    Dim OutApp As Outlook.Application
    Dim OutAccount As Outlook.Account
    Dim OutMail As Outlook.MailItem
    
    Dim myRecipients As Outlook.Recipients
    'Dim myRecipient As Outlook.Recipient
    
    Dim linha As Long
    
    Set OutApp = CreateObject("Outlook.Application")
    Set OutAccount = OutApp.Session.Accounts.Item(1)
    Debug.Print OutAccount
    
    For linha = 2 To 16
    
        'Set OutMail = OutApp.CreateItemFromTemplate(" .oft file path")
        Set OutMail = OutApp.CreateItem(olMailItem)
        
        With OutMail
        
            .To = Cells(linha, 1).Value
            .Subject = "Test " & linha
            
            Set myRecipients = OutMail.Recipients
            
            ' May trigger an address update
            myRecipients.ResolveAll
            
            ' Not for email address format. Resolved always True.
            '  https://learn.microsoft.com/en-us/office/vba/api/Outlook.Recipients.ResolveAll
            '
            'If Not myRecipients.ResolveAll Then
            '    For Each myRecipient In myRecipients
            '        If Not myRecipient.Resolved Then
            '            MsgBox myRecipient.Name
            '        End If
            '        Set myRecipient = Nothing
            '    Next
            'End If
            
            .Save
            '.Send
            
        End With
        
        Set OutMail = Nothing
        
    Next
    
    Set OutApp = Nothing
    Set OutAccount = Nothing
    
    Set myRecipients = Nothing
    
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文