Problem getting Selection.text data

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
 
M

Michael Moschella

Was able to find a solution to the problem by doing geting
the value of the property
sSelText = mWrd.ActiveDocument.Sections.First.Range.Text
before I do my cut.

Mike
 
C

Cindy M -WordMVP-

Hi Michael,

Would you please more closely describe "junk data", and how
it compares to what you expect you should see?

Do you get anything different if you use
Selection.Range.Text?

Actually, for what you're trying to do, you'd probably be
better off to use For each on the Sections collection and
pick up each Section's Range.Text. You might want to look at
the code Doug Robbins has posted at mvps.org/word for the
same task, as I believe he does loop through the sections
that way...
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.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep
30 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :)
 
Top