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