2
votes

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

2
I have added a further note to my post. - Fionnuala

2 Answers

1
votes

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

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