How to attach active Excel workbook to an email

邮差的信 提交于 2021-02-07 04:13:30

问题


I have been trying all morning to get this VBA script to attach my active excel document to an auto-generated outlook message. Everything works fine if I declare the file path as a string and attach it. Except that I would like to attach the full file path of the current excel document instead of using a static string value.

Here is my code:

Private Sub CommandButton1_Click()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim strbody As String
    Dim sAttach As String
    Dim sTo As String
    Dim sCC As String



    'For To field
    Set emailRng = Worksheets("Pre-Clearance Email").Range("E11:J14")

    For Each cl In emailRng
        sTo = sTo & ";" & cl.Value
    Next

    sTo = Mid(sTo, 2)

    'For CC field
    Set emailRngCC = Worksheets("Pre-Clearance Email").Range("E16:J19")

    For Each cl In emailRngCC
        sCC = sCC & ";" & cl.Value
    Next

    sCC = Mid(sCC, 2)



    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    'variable declarations for email body and attachment
    strbody = "<BODY style=font-size:11pt;font-family:Calibri>Good Morning;<p>Please see the attached aliases for validation. Please let me know if you have any questions.<p>Thank you.</BODY>"
    sAttach = "K:\CRM Support\Data\Systematic Trade Recon (1).xlsm"

    'the below code adds a users default signature to the email
    With OutMail
        .Display
    End With
        signature = OutMail.HTMLBody

    With OutMail
        .to = sTo
        .CC = sCC
        .Subject = "STR Pre-Clearance"
        .HTMLBody = strbody & signature
        .Attachments.Add (ActiveDocument.FullName)

        '.Attachments.Add sAttach
        .Display 'Instead of .Display, you can use .Send to send the email _
                    or .Save to save a copy in the drafts folder
    End With 

The compiler gives me an error at this line:

.Attachments.Add (ActiveDocument.FullName)

I have done some research, and tried to fix the problem myself, but I just can't figure out how to make this script attach the active file to this outlook message. As you can see by my code, my backup option is to just use a string variable and a static address to attach the file, but I would rather make this script more versatile than that.

Here is one of the sites which I found that gave me this idea to begin with: Here


回答1:


Well, after some more effort I was able to get the workbook to attach flawlessly. Here was the revision I made to the OutMail Object in my orginial code:

With OutMail
        .to = sTo
        .CC = sCC
        .Subject = "STR Pre-Clearance"
        .HTMLBody = strbody & signature
        .Attachments.Add (ActiveDocument.FullName) 'this is the correction I made
        .Display 

I figured I would answer my own question so it doesn't linger without a technical answer. Maybe it will help someone in the future.




回答2:


The fix should actually be:

With OutMail
    .To = sTo
    .CC = CC
    .Subject = "STR Pre-Clearance"
    .HTMLBody = strbody & signature
    .Attachments.Add (ActiveWorkbook.FullName) 'should be workbook not document
    .Display 'or .Send


来源:https://stackoverflow.com/questions/22255655/how-to-attach-active-excel-workbook-to-an-email

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