【问题标题】:Sending Emails with Attachments VBA发送带附件的电子邮件 VBA
【发布时间】:2019-03-09 23:04:46
【问题描述】:

我正在尝试向我的电子邮件添加附件功能。我的电子邮件代码可以正常工作,但是附件作为 ATT00001.bin 文件发送。

变量 Forms![frmMain]!TabCtl54.Pages("page56").Controls("subtblcontent").Form![attachmentlnk] 是表单上的一个文本框,我将在其中放置文件名。

attachmentlnkvar = "file:///C:/Users/desktopname/Desktop/" & Forms![frmMain]!TabCtl54.Pages("page56").Controls("subtblcontent").Form![attachmentlnk] & ".pdf"

With cdomsg
.To = emailstr
.FROM = fromemailstr
.subject = Forms!frmMain.txtSubject
.Attachments.Add attachmentlnkvar
.HTMLBody = strHTML
.Send

End With
    Set cdomsg = Nothing

有没有办法可以将我的文件以 pdf 格式发送?

【问题讨论】:

  • attachmentlnkvar 填充后,with 前的行的值是多少
  • 它只是一个字符串变量,它将创建 pdf 的路径并确保其为 pdf 格式
  • 您好,我明白了,但该变量是否等于您的预期?
  • 是的,它等于“C:\Users\desktopname\Desktop\reportname.pdf”,这是我所期望的
  • 您使用的是什么电子邮件应用程序?

标签: vba email ms-access


【解决方案1】:

不要使用file:// 等,只使用路径。和反斜杠。

attachmentlnkvar = "C:\Users\desktopname\Desktop\" & Forms![frmMain]!TabCtl54.Pages("page56").Controls("subtblcontent").Form![attachmentlnk] & ".pdf"

【讨论】:

  • 它仍然以 bin 文件的形式出现
【解决方案2】:

你试过.AddAttachment attachmentlnkvar而不是.Attachments.Add attachmentlnkvar吗?这就是我用来通过 SMTP 服务器而不是 Outlook 发送 PDF 报告的方法。

【讨论】:

  • 刚试了下,还是以bin文件的形式出现
【解决方案3】:

问题出在您的 SMTP 服务器上。尝试将附件放在身体后以避免此问题。如果这不起作用,请尝试使用纯文本而不是 HTML 发送消息:

.TextBody = bodyText

示例:

attachmentlnkvar = "C:/Users/desktopname/Desktop/" & Forms![frmMain]!TabCtl54.Pages("page56").Controls("subtblcontent").Form![attachmentlnk] & ".pdf"

With cdomsg
    .To = emailstr
    .FROM = fromemailstr
    .Subject = Forms!frmMain.txtSubject
    .HTMLBody = strHTML
    .AddAttachment attachmentlnkvar
    .Send
End With

Set cdomsg = Nothing

说明: https://kb.mit.edu/confluence/pages/viewpage.action?pageId=4981187

【讨论】:

    【解决方案4】:

    我很高兴与您分享我用来发送所有电子邮件的功能:

    Public Sub SendMessage(Optional SubjectText = "", Optional BodyText = "", Optional AttachmentPath = "", Optional sendTo = "", Optional sendCC = "", Optional DeliveryConfirmation = True, Optional DisplayDoNotAutoSend = True, Optional SendHighPriority = True, Optional UseHTML = True)
    
       Dim objOutlook As Outlook.Application
       Dim objOutlookMsg As Outlook.MailItem
       Dim objOutlookRecip As Outlook.Recipient
       Dim objOutlookAttach As Outlook.Attachment
       Dim MultipleAttachmentPath As String
       Dim CurrentAttachment As Variant
       Dim aAtachments() As String
       On Error GoTo ErrorMsgs
        DoCmd.Hourglass True
       ' Create the Outlook session.
       Set objOutlook = New Outlook.Application    
       ' Create the message.
       Set objOutlookMsg = objOutlook.CreateItem(olMailItem)       
       With objOutlookMsg
    
          If UseHTML Then
          .BodyFormat = olFormatHTML          
          End If
    
          If Not isnull(sendTo) And InStr(sendTo, "@") > 0 Then
            .To = sendTo
          End If
          If Not isnull(sendCC) And InStr(sendCC, "@") > 0 Then
            .CC = sendCC
          End If
          .Subject = SubjectText
    
          If UseHTML Then
            .HTMLBody = "<div style='font-family:Calibri,sans-serif'>" & BodyText & GetThankYouSignature & "</div>"
          Else
            .Body = BodyText & vbCrLf & GetUserFullNameInASCIIText & vbCrLf & vbCrLf
          End If
    
          If SendHighPriority Then
              .Importance = olImportanceHigh  'High importance
          End If
    
          If DeliveryConfirmation Then
              .OriginatorDeliveryReportRequested = True
              .ReadReceiptRequested = True
          End If
          On Error Resume Next
          If AttachmentPath <> "" Then
            ' Add attachments to the message.
              If Not IsMissing(AttachmentPath) And InStr(AttachmentPath, ";") = 0 Then
                 Set objOutlookAttach = .Attachments.add(AttachmentPath)
              ElseIf Not IsMissing(AttachmentPath) And InStr(AttachmentPath, ";") > 0 Then
                aAtachments = Split(AttachmentPath, ";")
                For Each CurrentAttachment In aAtachments
                    .Attachments.add (CurrentAttachment)
                Next
              End If
          End If
        On Error GoTo ErrorMsgs
       End With
    
       If DisplayDoNotAutoSend Or isnull(sendTo) Then
           objOutlookMsg.Display
       Else
           objOutlookMsg.Send
       End If
    
       Set objOutlookMsg = Nothing
       Set objOutlook = Nothing
       Set objOutlookRecip = Nothing
       Set objOutlookAttach = Nothing
       DoCmd.Hourglass False
       Exit Sub
    ErrorMsgs:
        DoCmd.Hourglass False
       If Err.Number = "287" Then
          MsgBox "You clicked No to the Outlook security warning. " & _
          "Rerun the procedure and click Yes to access e-mail" & _
          "addresses to send your message. For more information," & _
          "see the document at http://www.microsoft.com/office" & _
          "/previous/outlook/downloads/security.asp. "
       Else
        Call LogError(Err.Number, Err.Description, "SystemUtilities", "SendMessage")
          Resume Next
          Resume
       End If
    End Sub
    

    变量 AttachmentPath 可以包含多个以“;”分隔的附件路径

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-23
      相关资源
      最近更新 更多