M
Michael Moschella
I got some code from http://www.gmayor.dsl.pipex.com on
how to do a mailmerge and then break the merged file into
seperate files by doing Selections.Cut and Selection.Paste
into these files of each story in the merged document.
What I need to do is get the text of each file and put it
into a string. But when I try to use the
Selection.text property it gives me junk data. I know the
clipboard has the correct data because it uses the
clipboard data when it creates the seperate files
correctly. But the text property does not give me the
data correctly.
Thanks
Michael
Below is the Code I am using
Dim iLetter, iCounter As Integer
Dim sDocName As String
Dim sSelText As String
On Error GoTo eh
bNew = False
Set mWrd = CreateObject("Word.Application")
sversion = mWrd.Version
If Not (bNew) Then
Set mDoc = mWrd.Documents.Open
("C:\MailMerge\Demo\TempTemplate.doc")
Else
Set mDoc = mWrd.Documents.Add
End If
If Not (bNew) Then
'attach MailMerge data source file to the document
mDoc.MailMerge.MainDocumentType = wdFormLetters
mDoc.MailMerge.OpenDataSource
Name:="C:\MailMerge\Demo\data1.dat"
End If
mWrd.Visible = True
mWrd.Activate
sDocName = "C:\WordFiles\MergeFile1"
Screen.MousePointer = vbDefault
mDoc.MailMerge.Destination = wdSendToNewDocument
mDoc.MailMerge.Execute
Set mMergeDoc = mWrd.ActiveDocument
mMergeDoc.Activate
mWrd.Selection.EndKey Unit:=wdStory, Extend:=wdMove
iLetters = mWrd.Selection.Information
(wdActiveEndSectionNumber)
mWrd.Selection.HomeKey Unit:=wdStory
iCounter = 1
While iCounter < iLetters
Set mMsg = mOut.CreateItem(olMailItem)
mWrd.ActiveDocument.Sections.First.Range.Cut
sSelText = mWrd.Selection.Text
mWrd.Documents.Add
mWrd.Selection.Paste
sDocName = "C:\WordFiles\MergeFile" &
LTrim$(Str$(iCounter))
mWrd.ActiveDocument.SaveAs FileName:=sDocName
mWrd.ActiveDocument.Close
iCounter = iCounter + 1
mWrd.Documents("Form Letters1").Activate
Set mMsg = Nothing
Wend
Set mOut = Nothing
Unload Me
ShutDownWordApp mWrd
mWrd.Quit
Exit Sub
eh:
Unload Me
ShutDownWordApp mWrd
End Sub
how to do a mailmerge and then break the merged file into
seperate files by doing Selections.Cut and Selection.Paste
into these files of each story in the merged document.
What I need to do is get the text of each file and put it
into a string. But when I try to use the
Selection.text property it gives me junk data. I know the
clipboard has the correct data because it uses the
clipboard data when it creates the seperate files
correctly. But the text property does not give me the
data correctly.
Thanks
Michael
Below is the Code I am using
Dim iLetter, iCounter As Integer
Dim sDocName As String
Dim sSelText As String
On Error GoTo eh
bNew = False
Set mWrd = CreateObject("Word.Application")
sversion = mWrd.Version
If Not (bNew) Then
Set mDoc = mWrd.Documents.Open
("C:\MailMerge\Demo\TempTemplate.doc")
Else
Set mDoc = mWrd.Documents.Add
End If
If Not (bNew) Then
'attach MailMerge data source file to the document
mDoc.MailMerge.MainDocumentType = wdFormLetters
mDoc.MailMerge.OpenDataSource
Name:="C:\MailMerge\Demo\data1.dat"
End If
mWrd.Visible = True
mWrd.Activate
sDocName = "C:\WordFiles\MergeFile1"
Screen.MousePointer = vbDefault
mDoc.MailMerge.Destination = wdSendToNewDocument
mDoc.MailMerge.Execute
Set mMergeDoc = mWrd.ActiveDocument
mMergeDoc.Activate
mWrd.Selection.EndKey Unit:=wdStory, Extend:=wdMove
iLetters = mWrd.Selection.Information
(wdActiveEndSectionNumber)
mWrd.Selection.HomeKey Unit:=wdStory
iCounter = 1
While iCounter < iLetters
Set mMsg = mOut.CreateItem(olMailItem)
mWrd.ActiveDocument.Sections.First.Range.Cut
sSelText = mWrd.Selection.Text
mWrd.Documents.Add
mWrd.Selection.Paste
sDocName = "C:\WordFiles\MergeFile" &
LTrim$(Str$(iCounter))
mWrd.ActiveDocument.SaveAs FileName:=sDocName
mWrd.ActiveDocument.Close
iCounter = iCounter + 1
mWrd.Documents("Form Letters1").Activate
Set mMsg = Nothing
Wend
Set mOut = Nothing
Unload Me
ShutDownWordApp mWrd
mWrd.Quit
Exit Sub
eh:
Unload Me
ShutDownWordApp mWrd
End Sub