Sreedhar
Sreedhar

Reputation: 30025

VBA + Send Mail from Word 2007

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

Upvotes: 2

Views: 4624

Answers (2)

Fionnuala
Fionnuala

Reputation: 91346

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"""

Upvotes: 0

Robert Mearns
Robert Mearns

Reputation: 11986

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"

Upvotes: 1

Related Questions