CDO attachments

austin350s10

Active Member
Joined
Jul 30, 2010
Messages
321
Does anyone know how to successfully send an attachment using CDO. Below is a script that I know works without the AddAttacment line included. As soon as I add the AddAttachemt line back into the code it will keep erroring out at the AddAttachment line.

If I change the attachment path to a pure string without using any variables it will send without any errors. Only problem is I need the ability to save the document to the users temp folder which will be different from one system to the next. The only way I can see to do it is to use the variable Environ("UserName") .

Is there something I am missing here? How can I get the attachment to work while using a variable in the file path?

NOTE: I am using this particular code in Word 07. It has been modified a bit from a similar working code in Excel 07.
Code:
Private Sub CommandButton1_Click()

ActiveDocument.SaveAs "C:\Documents and Settings\" & Environ("UserName") & "\Local Settings\Temp\AHCagreement.docx"


On Error GoTo ErrHandler:
Const cdoSendUsingPickup = 1
Const cdoSendUsingPort = 2
Const cdoAnonymous = 0
Const cdoBasic = 1
Const cdoNTLM = 2
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "New Agreement for - "
objMessage.From = """Service Agreement"" me@you.com"
objMessage.To = "me@you.com"
objMessage.AddAttachment "C:\Documents and Settings\" & Environ("UserName") & "\Local Settings\Temp\AHCagreement.docx"
objMessage.Textbody = "Test Message"
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "fakeServer.com"
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusername") = "me@you.com"
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "******"
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
objMessage.Configuration.Fields.Update
objMessage.Send
MsgBox "good"
Exit Sub
ErrHandler:
    MsgBox "Unable to send. Please make sure you are connected to the internet", vbCritical, "Error"
    Exit Sub
End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

Forum statistics

Threads
1,223,231
Messages
6,170,885
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top