使用VBA将Excel文件添加到Outlook

时间:2018-01-29 10:46:25

标签: excel vba excel-vba outlook outlook-vba

在搜索问题时尝试了所有资源,我无法弄清楚是什么问题。

使用简单的代码行

Attachment.Add ("C:\Users\320006190\test.txt")

无法添加文件,尽管该文件已在该确切位置退出。

1 个答案:

答案 0 :(得分:0)

尝试以下代码:

Sub Test()
    Dim strLocation As String

    Dim OutApp As Object
    Dim OutMail As Object
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    With OutMail
        .To = ""
        .CC = ""
        .BCC = ""
        .Subject = "This is the Subject line"
        .Body = "Hello World!"

        strLocation = "C:\Users\Desktop\location\your_file_name.xlsx"
        .Attachments.Add (strLocation)
        .Display
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With

End Sub