Email subject line in Mail Merge

M

mikelee101

Hello,
I'm trying to do a merge to email, but would like the subject line of
the email to contain a mergefield. Per the mailmerge forum, this
isn't supported, but may be possible through VBA. What I'd like is
for the subject line to be:

Important information regarding <<domain>>.

I have Word2003, and am somewhat proficient in Excel VBA, but have
little to no experience with Word. The document itself looks ok, and
I can scroll through the recipients.

I found the MailMerge object, but am not having any success with it.
Here's what I tried:

Sub TestMerge()
Dim MgDoc As Document

Set MgDoc = Application.Documents("Notification.doc")

MgDoc.MailMerge.MailAddressFieldName = "email address"
MgDoc.MailMerge.MailSubject = "Important Information Regarding" &
"domain"
MgDoc.MailMerge.Destination = wdSendToEmail
MgDoc.MailMerge.MailAsAttachment = False
MgDoc.MailMerge.Execute

End Sub

However, after the execute statement, I get an error that says:

Word cannot merge documents that can be distributed by mail or fax
without a valid mail address. Choose the Setup button to select a mail
address data field.

So...any thoughts? What "Setup button" is it talking about? I can
scroll through the recipients in Word, so I know it knows that they're
there. Also, I can't imagine that the line that should set the
mailsubject field will be able to tell the difference between text
"Important Information Regarding" and the mailmerge field "domain."

If anyone knows if I can reference a merge field in the mailsubject
property and, if so, what's causing the run-time error, I'd really
appreciate the help.

Thanks,
Mike Lee
Coppell, TX, USA
 
M

mikelee101

Hello,
I'm trying to do a merge to email, but would like the subject line of
the email to contain a mergefield.  Per the mailmerge forum, this
isn't supported, but may be possible through VBA.  What I'd like is
for the subject line to be:

Important information regarding <<domain>>.

I have Word2003, and am somewhat proficient in Excel VBA, but have
little to no experience with Word.  The document itself looks ok, and
I can scroll through the recipients.

I found the MailMerge object, but am not having any success with it.
Here's what I tried:

     Sub TestMerge()
     Dim MgDoc As Document

     Set MgDoc = Application.Documents("Notification.doc")

     MgDoc.MailMerge.MailAddressFieldName = "email address"
     MgDoc.MailMerge.MailSubject = "Important Information Regarding" &
"domain"
     MgDoc.MailMerge.Destination = wdSendToEmail
     MgDoc.MailMerge.MailAsAttachment = False
     MgDoc.MailMerge.Execute

     End Sub

However, after the execute statement, I get an error that says:

Word cannot merge documents that can be distributed by mail or fax
without a valid mail address. Choose the Setup button to select a mail
address data field.

So...any thoughts?  What "Setup button" is it talking about?  I can
scroll through the recipients in Word, so I know it knows that they're
there.  Also, I can't imagine that the line that should set the
mailsubject field will be able to tell the difference between text
"Important Information Regarding" and the mailmerge field "domain."

If anyone knows if I can reference a merge field in the mailsubject
property and, if so, what's causing the run-time error, I'd really
appreciate the help.

Thanks,
Mike Lee
Coppell, TX, USA

I've since figured out the run-time error (funny how a little
underscore character can bollux everything up).

However, I'm still not having any luck getting a value from a
mergefield into the subject line, so if anyone has any suggestions,
please let me know.

Thanks,
Mike
 
M

mikelee101

You could do it with a modification of the method/code in the article "Mail
Merge to E-mail with Attachments" at:

http://word.mvps.org/FAQs/MailMerge/MergeWithAttachments.htm

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP


















- Show quoted text -

Hello Doug,
Thanks for the article. It took a while, but I think I figured out a
way to cycle through the records using this code, except that I'm now
getting hung up on a line that I basically took directly from the
sample. Here's my procedure (in case the problem is in a Dim
statement or anything like that):

<---------------code begin----------------->
Sub TestMerge2()
Dim MgDoc As Document, NumRecords As Integer
Dim olApp As Outlook.Application, olMsg As Outlook.MailItem

Set MgDoc = Application.Documents("Notification.doc")

NumRecords = MgDoc.MailMerge.DataSource.RecordCount

MgDoc.MailMerge.DataSource.FirstRecord = 1

For i = 1 To NumRecords

Set olMsg = olApp.CreateItem(olMailItem) '!!!Run-time
error at this line

With olMsg
.To = MgDoc.MailMerge.DataSource.DataFields(2)
.Subject = "Important Information Regarding " &
MgDoc.MailMerge.DataSource.DataFields(1)
.Body = MgDoc.Content
End With

olMsg.Send
Set olMsg = Nothing

MgDoc.MailMerge.DataSource.ActiveRecord = wdNextRecord

Next i

End Sub
<----------------code end------------------>

At the Set olMsg line, I get "Run-time error '91': Object variable or
With block variable not set"

I've double checked and the reference to Microsoft Outlook 11.0 Object
Library is checked. I've also tried putting the Set olMsg = Nothing
statement before the CreateItem line. The = Nothing statement
executes fine, but I still get the error when it gets to the
CreateItem method.

Any thoughts?

Thanks again for the help.

Mike
 
D

Doug Robbins - Word MVP

You have omitted the following part of the code:

' Check if Outlook is running. If it is not, start Outlook
On Error Resume Next
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

You could do it with a modification of the method/code in the article
"Mail
Merge to E-mail with Attachments" at:

http://word.mvps.org/FAQs/MailMerge/MergeWithAttachments.htm

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP


















- Show quoted text -

Hello Doug,
Thanks for the article. It took a while, but I think I figured out a
way to cycle through the records using this code, except that I'm now
getting hung up on a line that I basically took directly from the
sample. Here's my procedure (in case the problem is in a Dim
statement or anything like that):

<---------------code begin----------------->
Sub TestMerge2()
Dim MgDoc As Document, NumRecords As Integer
Dim olApp As Outlook.Application, olMsg As Outlook.MailItem

Set MgDoc = Application.Documents("Notification.doc")

NumRecords = MgDoc.MailMerge.DataSource.RecordCount

MgDoc.MailMerge.DataSource.FirstRecord = 1

For i = 1 To NumRecords

Set olMsg = olApp.CreateItem(olMailItem) '!!!Run-time
error at this line

With olMsg
.To = MgDoc.MailMerge.DataSource.DataFields(2)
.Subject = "Important Information Regarding " &
MgDoc.MailMerge.DataSource.DataFields(1)
.Body = MgDoc.Content
End With

olMsg.Send
Set olMsg = Nothing

MgDoc.MailMerge.DataSource.ActiveRecord = wdNextRecord

Next i

End Sub
<----------------code end------------------>

At the Set olMsg line, I get "Run-time error '91': Object variable or
With block variable not set"

I've double checked and the reference to Microsoft Outlook 11.0 Object
Library is checked. I've also tried putting the Set olMsg = Nothing
statement before the CreateItem line. The = Nothing statement
executes fine, but I still get the error when it gets to the
CreateItem method.

Any thoughts?

Thanks again for the help.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top