Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Mailmerge and Fax / December 2006

Tip: Looking for answers? Try searching our database.

VB.NET mailmerge routing - wdSendToNewDocument

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
bigHairy - 01 Dec 2006 13:05 GMT
Hello.

I am building a vb.net routine that takes a datasource and merges
mailmerge documents. I am very happy with the results so far. This runs
in the background as an automated routine and currently just sends to a
printer. Again, all is good and happy. We have been asked if some of
the documents can be merged and then sent by email. This is proving
trickier than I thought. I want to send a merged document as an
attachment to a subscriber. I don't want it going as a mailmerge
document.

So far I have

With aDoc.MailMerge
      .Destination = WdMailMergeDestination.wdSendToNewDocument
      .Execute()
End With

This is great, but where is the new document? I want to grab it and do
stuff with it but I can not find it's path anywhere. Where does it
live? Can I specify an output path for the new document?

Sorry if this is a stupid question, all help is appreciated.
Peter Jamieson - 01 Dec 2006 13:32 GMT
> This is great, but where is the new document?

It's an unsaved document. To attach it to an e-mail you have to save it in
the file system first.

Immediately after you execute a merge, the newly created merge document is
(usually) referenced by ActiveDocument, unless you have requested an error
document.

Depending on what you are doing, you can also merge directly to e-mail and
specify that you want to send the document as an attachment. But then you
don't get to specify the body text. If you need to do that and you happen to
be using Outlook (not Outlook Express) as your email client, the following
(VBA) macro may give you some clues:

Sub EmailOneDocPerSourceRecWithBody()
Dim bOutlookStarted As Boolean
Dim bTerminateMerge As Boolean
Dim intSourceRecord As Integer
Dim objMailItem As Outlook.MailItem
Dim objMerge As Word.MailMerge
Dim objOutlook As Outlook.Application
Dim strMailSubject As String
Dim strMailTo As String
Dim strMailBody As String
Dim strOutputDocumentName As String

bOutlookStarted = False
bTerminateMerge = False

' Set up a reference to the
' Activedocument, partly because
' the ActiveDocument changes as you
' merge each record

Set objMerge = ActiveDocument.MailMerge

' Start Outlook as necessary

On Error Resume Next
Set objOutlook = GetObject(, "Outlook.Application")
If Err <> 0 Then
 Set objOutlook = CreateObject("Outlook.Application")
 bOutlookStarted = True
End If

With objMerge

' If no data source has been defined,
' do it here using OpenDataSource.
' But if it is already defined in the
' document, you should not need to
' define it here.

' .OpenDataSource _
' Name:="whatever"

intSourceRecord = 1

Do Until bTerminateMerge
 .DataSource.ActiveRecord = intSourceRecord

 ' if we have gone past the end
 ' (and possibly, if there are no records)
 ' then the Activerecord will not be what
 ' we have just tried to set it to

 If .DataSource.ActiveRecord <> intSourceRecord Then
   bTerminateMerge = True
   ' the record exists
 Else

   ' while we are looking at the
   ' correct activerecord,
   ' create the mail subject, body and "to"
   ' Just some sample code here - replace it with
   ' whatever you need

   strMailSubject = _
     "Results for " & _
     objMerge.DataSource.DataFields("Firstname") & _
     " " & objMerge.DataSource.DataFields("Lastname")

   strMailBody = _
     "Dear " & objMerge.DataSource.DataFields("Firstname") & _
     vbCrLf & _
     "Please find attached a Word document containing" & vbCrLf & _
     "your results for..." & vbCrLf & _
     vbCrLf & _
     "Yours" & vbCrLf & _
     "Your name"
   strMailTo = objMerge.DataSource.DataFields("Emailaddress")

   ' create the document path name
   ' In this case it can be te same for every recipient,
   ' but if you want to retain copies of the
   ' document, you can use info. in the data source

   ' this is an example - insert your
   ' own pathname here

   strOutputDocumentName = "c:\a\results.doc"

   ' strOutputDocumentName = _
   '  "c:\mymergeletters\_" & _
   '  .DataSource.DataFields("Lastname").Value & _
   '  " letter.doc"
   .DataSource.FirstRecord = intSourceRecord
   .DataSource.LastRecord = intSourceRecord
   .Destination = wdSendToNewDocument
   .Execute

   ' The Activedocument is always the
   ' output document

   ' Add any parameters you need to these calls
   ActiveDocument.SaveAs strOutputDocumentName
   ActiveDocument.Close

   ' Now create a mail item

   Set objMailItem = objOutlook.CreateItem(olMailItem)
   With objMailItem
     .Subject = strMailSubject
     .Body = strMailBody
     .To = strMailTo
     .Attachments.Add strOutputDocumentName, olByValue, 1
     '.Save
     .Send
    End With
    Set objMailItem = Nothing

   intSourceRecord = intSourceRecord + 1
 End If
Loop
End With

' Close Outlook if appropriate

If bOutlookStarted Then
 objOutlook.Quit
End If

Set objOutlook = Nothing
Set objMerge = Nothing

End Sub

Peter Jamieson

> Hello.
>
[quoted text clipped - 19 lines]
>
> Sorry if this is a stupid question, all help is appreciated.
bigHairy - 01 Dec 2006 14:41 GMT
You're great. Thank you very much. Simple when you know how...
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.