tags:

views:

196

answers:

2

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

A: 

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"""
Remou
+1  A: 

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"
Robert Mearns
Which version are you using? As I said, the code works as it stands.
Remou
Nev_Rahd