Hi,
I've searched these forums but have not been able to find exactly what I need.
I know how to send emails with VBA in Word, but the only problem I have is that as far as i'm aware the subject line is always fixed in the code ( .Subject = "New subject" )
What i'm trying to do, is send reports i have reviewed to an email address by clicking a button in the toolbar. The email address is always the same but the subject line shows the customer's name and his account number (so that's different all the time)
Is there any way I can get the VBA code to copy the first line of the report (which has the customer's account number and name) and use that in the subject line
Many thanks for your help in advance.

sending emails from MS Word with variable subject line
FSOR
Here is an example. I did this on Excel, but I think the rules are the same. You can put more parameters on it so that they are variable based. I am not Word expert so I can't help you on that.
Private Sub OneEmail(EmailAddress As String, emailBody As String)
' Create the message.
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Set objOutlookMsg = ObjOutlook.CreateItem(olMailItem)
With objOutlookMsg
' Add the To recipient(s) to the message.
Set objOutlookRecip = .Recipients.Add(EmailAddress)
objOutlookRecip.Type = olTo
'.BCC = "abc@hotmail.com"
' .SentOnBehalfOfName = "support@hotmail.com"
' Set the Subject, Body, and Attachment.
.Subject = "Hello I am a subject line"
.HTMLBody = emailBody
.Attachments.Add "C:\email attachment folder\an attachment.zip"
' Resolve recipient's name.
objOutlookRecip.Resolve
' .Display
.Save
.Send
End With
End Sub
jmcole76
Is it possible that the text is not part of the bookmark Rather the bookmark is next to the text.
If so maybe this code will grab the word to the right of the bookmark.
With Selection.GoTo(What:=wdGoToBookmark, Name:="EmailAddress")
Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
MsgBox Selection.Text
End With
sagittarian
Sub x()
' requries reference to MS Forms library, which can be done by simply adding a userform
Dim objDO As MSForms.DataObject
Set objDO = New DataObject
objDO.GetFromClipboard
' not sure of your code for getting email object
Email.Subject = objDO.GetText
End Sub
jreis
Something along the lines of the following. Although this may grab too much text depending on your document setup.
.Subject = Activedocument.Paragraphs(1).Range.Text
laiseng
With Selection.GoTo(what:=wdGoToBookmark, Name:="EmailAddress")
Selection.MoveRight Unit:=wdWord, Count:=7, Extend:=wdExtend
myMailItem.To = Selection.Text
End With
instead of myMailItem.Recipient.Add ...
Thanks magicalclick, because before the message was just sending and closing the Outlook session, when I really wanted it to create the E-mail, leave it open for the user to check and send themselves.
.Display
.Save
its just what was needed. So I'm almost there!
Next, I need to (optional) add the Active Doc as an attachment, put in error handling for when the bookmarks don't exist and one thing I am stuck on is finding the end of the selection. i.e. with the code above you'll notice Count:=7. That's roughly the number of parts of an e-mail address as it includes several .dots, an @ symbols, and words. These bookmarks are in a table embedded in Word. I have been able to use ActiveDocument.Table(1).Cells(2,2).Range.Text to access the same data but there may be other tables entered into the doc hence using the unique bookmark names. Guess i'll just increase the count for each bookmark I'm using to a suitable maximum. One bookmark is a 'comments' field which could be up to 100-maybe even 200 words. Might have to compromise or have a rethink.
Thanks Andy & MagicalClick for your input, I rarely use Forums but I like this one !
Ntc
I found this useful for a tool I'm developing and was wondering how to grab data in a bookmark from an active document and put into an E-mail Subject, Body, To field, etc.
I imagined it would look like this;
myMailItem.Subject = ActiveDocument.Bookmarks("EmailAddress").Range.Text
But this leavesthe subject blank and theres no Help doc on this. Any ideas if bookmarks can be used
Thanks
Blue_Gene
Many thanks for your help, Andy!
Just thinking: is there any way you could paste into the subject line a string of text that's been copied with Selection.Copy
Cheers
Gerard
Surjeet Singh
myMailItem.Body = "Text" & vbNewLine + "..Text"
gives a line return to help format the body of the E-mail to look like;
Text
..Text
And attachments using this method can be done by..
Dim myOutlook As Object
Dim myMailItem As Object
Set myOutlook = CreateObject("Outlook.Application")
Set myMailItem = myOutlook.CreateItem(olMailItem)
Set myAttachments = myMailItem.Attachments
.....
myAttachments.Add "C:\Documents and Settings\user\My Documents\somefile.doc"
myAttachments.Add ActiveDocument.FullName
Now for more formatting and error handling :)
mamrg
Strange thing is, your code (above) works fine, and if this line is included;
myMailItem.Subject = Selection.Text
puts it into the E-mail.
However it really doesn't like it when used for adding recipients, like;
myMailItem.Recipients.Add Selection.Text
or
myMailItem.Recipients.Add = Selection.Text
myMailItem.Recipients = Selection.Text
So it's great I can enter the Subject and Body and I'm sure To and CC will work as well,. I shall keep trying!
Thanks Andy.