Sending Emails with Attachments VBA

醉酒当歌 提交于 2020-05-23 13:58:47

问题


I am trying to add an attachment functionality to my emails. My email code is working however the attachments are being sent as ATT00001.bin files.

The variable Forms![frmMain]!TabCtl54.Pages("page56").Controls("subtblcontent").Form![attachmentlnk] is a textbox on a form which is where I would put my file name.

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

Is there a way I can send my files as pdfs?


回答1:


I am happy to share with you the function which I use to sent all my emails:

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

The variable AttachmentPath can contain multiple paths to attachments delimited by ";"




回答2:


Don't use file:// etc., just the path. And backslashes.

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



回答3:


Instead of .Attachments.Add attachmentlnkvar have you tried .AddAttachment attachmentlnkvar? That's what I use to send PDF reports via an SMTP server instead of Outlook.




回答4:


The problem is with your SMTP server. Try putting the attachment after the body to avoid this problem. If that doesn't work, try sending the message as plain text instead of HTML using:

.TextBody = bodyText

EXAMPLE:

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

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



来源:https://stackoverflow.com/questions/52648682/sending-emails-with-attachments-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!