You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Microsoft Access VBA发送Outlook邮件:多行内容报错及发件人设置问题

解决方案

1. 修复HTML邮件内容的字符串拼接错误

你的Msg字符串拼接存在语法混乱问题,VBA中多行字符串拼接需要正确闭合引号并使用&运算符连接,同时HTML标签格式需规范。修正后的代码如下:

Msg = "Dear " & FirstName & ",<p>" & _
      "Below is your Personal Identification Number.</p>" & _
      "<p>" & Pin & "</p>" & _
      "<p>This PIN is unique to you.</p>" & _
      "<p>You must safeguard, and not let anyone have access to your PIN.</p>" & _
      "<p>To initiate a wire, have your PIN available and call us at 555-555-5555.</p>" & _
      "<p>Questions? Please reply to this email, or call us at 555-555-5555.</p>"

问题根源:

  • 原代码中每行结尾的&"、"<P> &"写法错误,导致字符串未正确闭合
  • HTML段落标签<p>需配对使用</p>,保证邮件格式正常渲染

2. 正确设置发件人地址

Outlook的MailItem.From属性不能直接赋值字符串,需通过已配置的Outlook账号指定发件人,同时解析发件人地址确保显示正常。修正后的发件人设置逻辑如下:

' 遍历Outlook账号,匹配目标发件地址
Dim outAccount As Outlook.Account
For Each outAccount In O.Session.Accounts
    If outAccount.SmtpAddress = strFrom Then
        M.SendUsingAccount = outAccount
        Exit For
    End If
Next outAccount
' 解析并设置发件人显示
Set M.From = O.Session.CreateRecipient(strFrom)
M.From.Resolve

将这段代码插入到With M块中,完整的With块代码:

With M
    .BodyFormat = olFormatHTML
    .HTMLBody = Msg
    .To = Email
    .Subject = strSubject
    
    ' 设置发件人账号
    Dim outAccount As Outlook.Account
    For Each outAccount In O.Session.Accounts
        If outAccount.SmtpAddress = strFrom Then
            .SendUsingAccount = outAccount
            Exit For
        End If
    Next outAccount
    ' 解析发件人地址
    Set .From = O.Session.CreateRecipient(strFrom)
    .From.Resolve
    
    .Display
End With

问题根源:

  • 直接给.From赋值字符串不符合Outlook对象模型要求,必须通过已配置的账号或解析后的收件人对象设置

完整修正代码

Private Sub Command7_Click()

    Dim Msg As String
    Dim strFrom As String
    Dim strSubject As String
    
    ' 修正后的HTML邮件内容拼接
    Msg = "Dear " & FirstName & ",<p>" & _
          "Below is your Personal Identification Number.</p>" & _
          "<p>" & Pin & "</p>" & _
          "<p>This PIN is unique to you.</p>" & _
          "<p>You must safeguard, and not let anyone have access to your PIN.</p>" & _
          "<p>To initiate a wire, have your PIN available and call us at 555-555-5555.</p>" & _
          "<p>Questions? Please reply to this email, or call us at 555-555-5555.</p>"
    
    Dim O As Outlook.Application
    Dim M As Outlook.MailItem
    
    Set O = New Outlook.Application
    Set M = O.CreateItem(olMailItem)
    
    strFrom = "MyDepartment@MyCompany.com"
    strSubject = "ENCRYPT - Personal Identification Number (PIN)"
    
    With M
        .BodyFormat = olFormatHTML
        .HTMLBody = Msg
        .To = Email
        .Subject = strSubject
        
        ' 正确设置发件人账号
        Dim outAccount As Outlook.Account
        For Each outAccount In O.Session.Accounts
            If outAccount.SmtpAddress = strFrom Then
                .SendUsingAccount = outAccount
                Exit For
            End If
        Next outAccount
        ' 解析发件人地址
        Set .From = O.Session.CreateRecipient(strFrom)
        .From.Resolve
        
        .Display
    End With
    
    Set M = Nothing
    Set O = Nothing

End Sub

内容的提问来源于stack exchange,提问作者Timothy Couch

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 17:29:51