VBA +从 Word 2007 发送邮件

发布于 2024-08-25 14:47:35 字数 897 浏览 4 评论 0原文

我在我的Word文档(office 2007)中得到了下面的代码来发送带有附件的邮件 行抛出语法错误(找不到文件)

它在.Attachement.Add "C:\abc.txt"

代码:

Private Sub CommandButton1_Click()

Dim outlookapp As Object
Dim item As Object
Dim subject As String
Dim msg As String

    Set outlookapp = CreateObject("outlook.application")

    msg = "Enter Message here"
    subject = "Enter subject here"
    Set item = outlookapp.createitem(0)

    With item
        .to = "[email protected] <mailto:[email protected]> "
        .subject = subject
        .body = msg
        .Display
        .Attachments.Add "C:\abc.txt"
    End With

    End Sub

我做错了什么?

谢谢

I got below code in my Word Document (office 2007) to send a mail with attachement
It throws syntax error (file not found) at line

.Attachement.Add "C:\abc.txt"

Code:

Private Sub CommandButton1_Click()

Dim outlookapp As Object
Dim item As Object
Dim subject As String
Dim msg As String

    Set outlookapp = CreateObject("outlook.application")

    msg = "Enter Message here"
    subject = "Enter subject here"
    Set item = outlookapp.createitem(0)

    With item
        .to = "[email protected] <mailto:[email protected]> "
        .subject = subject
        .body = msg
        .Display
        .Attachments.Add "C:\abc.txt"
    End With

    End Sub

What am I doing wrong ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小瓶盖 2024-09-01 14:47:35

向项目添加附件的语法应将文件名括在方括号中。

尝试使用

.Attachments.Add ("C:\abc.txt")

而不是

.Attachments.Add "C:\abc.txt"

The syntax for adding an attachment to an item should have the file name enclosed in brackets.

Try using

.Attachments.Add ("C:\abc.txt")

instead of

.Attachments.Add "C:\abc.txt"
绾颜 2024-09-01 14:47:35

我尝试了上面的代码,它对我有用。您能否附加位于 C 根目录以外的位置的文件,例如 c:\docs\ ?

编辑评论

如果路径有空格,您将需要额外的引号:

strfile="""c:\abc def.txt"""

I tried the code above and it worked for me. Can you attach a file located somewhere other than the root of C, for example, c:\docs\ ?

EDIT Re Comment

If the path has spaces, you will need extra quotes:

strfile="""c:\abc def.txt"""
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文