Replacing text with Merge Fields in Word

D

Drew

I am attempting to convert a plain text document that contains certain
variables as delimited by the "@" or other user specified delimiters
and I can't seem to get the merge fields to position themselves in the
proper location. What currently happens is that the variables are
successfully removed, but the merge fields are all placed at the end of
the document. Also, any merge fields that have more than one word in
the field name is truncated to only the first word of the field's name.
The code that I am using is below:

With objWord.Content.find
rsVariableDefinitions.MoveFirst
Do Until rsVariableDefinitions.EOF 'loop
through variable definition table and replace text with their merge
fields
.Execute strDelimiter &
rsVariableDefinitions!Variable & strDelimiter, , True, , , , True, , ,
"", True 'Delete the variable placeholder
If .Found Then
Let strField =
rsVariableDefinitions("Field")
objWord.MailMerge.Fields.Add
oSel.Range, strField
End If
rsVariableDefinitions.MoveNext
Loop
End With

Can any one please help!

Thanks, Drew
 
P

Peter Jamieson

It would be easier to answer this if we knew anything about the
rsVariableDefinitions object (which I think is "proprietary"), but:
a. What is actually being returned by the statement

Let strField = rsVariableDefinitions("Field") ?
b. is anything setting oSel to be at the location you just found (i.e. the
Selection object might be at the correct location, but probably not oSel)

Peter Jamieson
 
D

Drew

Hi Peter,

Actually the rsVariableDefinitions object is simply a recordset that
maps the variable name to the merge field to be used for that name.

What you say about selection object is exactly correct; my problem is
that I don't know how to set the oSel to be at the location I just
found. How do I do that??

thanks, Drew
 
P

Peter Jamieson

Suggestons off the top of my head...

just use "Selection" instead of "oSel" (but qulaify it if necessary if the
code is e.g. being run from Excel rather than WOrd

or try

Set oSel = Selection

before referencing oSel.

Peter Jamieson
 
D

Drew

I tried both methods and each time Access returned an error: 'Object
is deleted.' So I used Selection.range and this did not return an
error, but the merge fields are still placed at the end of the
document. Is there a way to return the character positions of each of
the variables in the document so that I could implicitly instruct
Access to place the merge fields at those locations?
 
P

Peter Jamieson

Ah you're working in Access :)

But in any case, now I've had a proper look I don't think the existing
approach will take you where you want to go.

I would start with something more like the following:

Dim objWordApp As Word.Application
'Dim rngFound As Word.Range
' If you already have a reference to the Word Application, just use that
' instead of setting up this new one
Set objWordApp = ActiveDocument.Application
rsVariableDefinitions.MoveFirst
Do Until rsVariableDefinitions.EOF
objWordApp.Selection.HomeKey wdStory
objWordApp.Selection.Find.ClearFormatting
With objWordApp.Selection.Find
Do While .Execute( _
FindText:=strDelimiter & rsVariableDefinitions!Variable &
strDelimiter, _
Wrap:=wdFindContinue, _
Forward:=True) = True
'Set rngFound = Selection.Range
ActiveDocument.Fields.Add _
Range:=objWordApp.Selection.Range, _
Type:=wdFieldMergeField, _
Text:=rsVariableDefinitions("Field")
Loop
End With
rsVariableDefinitions.MoveNext
Loop

Some of that stuff may be surplus to requirements but I think it's closer to
the mark.

Peter Jamieson
 
D

Drew

Thank you, thank you, thank you! That's exactly what i needed. After
so many hours of frustration it's great to finally see this thing work!

Drew
 
P

Peter Jamieson

Great, thanks for the feedback.

Peter Jamieson

Drew said:
Thank you, thank you, thank you! That's exactly what i needed. After
so many hours of frustration it's great to finally see this thing work!

Drew
 

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