Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Emailing an attachment while in MS Word 2K >

Status
Not open for further replies.

ViperGTS

Programmer
Jun 4, 2002
18
US
Hi All.
I am trying to do this:

I have a form that has fields for users to fill in.
At the bottom of the document is a SEND button.
What I need to have happen is...

1. When the user clicks the SEND button, the current document (my form) is attached to an email message;
2. My address is already in the To Field;
3. 'MY Form Return' is in the Subject line;
4. and the email is then sent to me.

The code that I have so far is:

Private Sub Send1_Click()
Set objOutlook = CreateObject("Outlook.Application")
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
Set objOutlookRecip = .Recipients.Add("me @ myddress.com")
Set objOutlookAttach = .Attachments.Add("C:\Documents and Settings\UserFolder\Desktop\Email From WordDoc\My Form.doc")
objOutlookRecip.Type = olTo
.Subject = "My Form Return"
.Body = "This is an automailer response from My Form.doc"
.Send
End With
End Sub

I know little about using VBA in Word. In Excel I can hold my own well enough, but this one is beyond me.
I have set up my VBA Object Libraries.
I cannot seem to get the active document to attach - which is what I would like to have happen.
The code above reflects changes made to send the attachment once it is saved...but I do not want the user to have to save the form - just fill it out and hit the button. I also do not want them to have to enter the address.
I also realize that this code ASSUMES that the user will have Outlook as an email client. Is there another way to leave the email client unspecified and have this work in some other program as well?

Anyway, I will go with my assumptions of Outlook as the client for now just to get some progress on this (because I am not making any on my own now...LOL!!)
Yes they will have MS Word.

This is (hopefully) supposed to work in Word 2K and 2K2 - I do not think that I will need to consider '97 at this time.
Windows versions would be '98, and above.
I think that this covers it.

Thank you very much for having a look!
I appreciate your help if you have any to offer! :)

Regards,

ViperGTS
 
Still hoping that a/the Word VBA Guru will happen upon this thread...

ViperGTS
 
I just put this in another response. Here is the one I use in Access.

Private Sub Command2_Click()
Dim patha As String
Dim result As Integer
Dim displaymessage As Boolean
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment
patha = "c:\abc.zip"
displaymessage = True
' Create the Outlook session.
Set objOutlook = CreateObject("Outlook.Application")

' Create the message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

With objOutlookMsg
' Add the To recipient(s) to the message.
Set objOutlookRecip = .Recipients.Add("red54@iocorp.net")
objOutlookRecip.Type = olTo

' Add the CC recipient(s) to the message.
Set objOutlookRecip = .Recipients.Add("red54@iocorp.net")
objOutlookRecip.Type = olCC

' Add the BCC recipient(s) to the message.
Set objOutlookRecip = .Recipients.Add("red54@iocorp.net")
objOutlookRecip.Type = olBCC

' Set the Subject, Body, and Importance of the message.
.Subject = "This is an Automation test with Microsoft Outlook"
.Body = "This is the body of the message." & vbCrLf & vbCrLf
.Importance = olImportanceHigh 'High importance

' Add attachments to the message.
'If Not IsMissing(AttachmentPath) Then
Set objOutlookAttach = .Attachments.Add(patha)
'End If

' Resolve each Recipient's name.
For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
Next

' Should we display the message before sending?
If displaymessage Then
.Display
Else
.Save
.Send
End If
End With
Set objOutlook = Nothing
End Sub Time flies when you don't know what you're doing...
 
Hey Red!

Thank you for the code - I will give it a try in the morning (with a few adjustments) and then see how it works.

I will return with the verdict later today! :)

Regards,

ViperGTS
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top