Access to Word automation removing a merge field

K

KSH

Is it possible to remove one of the fields in the word document that you are
replacing data in.

For example:

{Name}
{address line 1}
{address line 2}

All the people in my list will not have 2 lines of address. I either want
to delete the 2 in those cases. Or set up the word doc to only have one line
and do an insert if I need to, but I can't figure out how to code that. Here
is my code where I try to inser:

Do While Not rsdocfields.EOF
'''' Read through fields for this document and replace
varreplacewith = RS.Fields(rsdocfields!replacewithfield)

With docword.Content.Find

With .Replacement
''' additional formating here if needed
' .ClearFormatting

End With
.Execute findtext:=rsdocfields!docfieldtag,
replacewith:=varreplacewith
End With
If RS.Fields(rsdocfields!replacewithfield) = "Address Line
2" Then
If varreplacewith <> "N/A" Then
With docword.Content
.InsertAfter (varreplacewith)
.InsertAfter vbCrLf
.Collapse

End With
End If
End If
rsdocfields.MoveNext

Loop



Working in Automation is new for me, so I know what I want to do, just can't
always figure out the code to do it. Thanks for any help.
 
A

Astrid

Hi KSH,

If the mergefield is on one line and if it's empty, the other lines will
shift up automatically.
If it's not on one line you could use a nested field (if then else) to check
if the field is empty and act accordingly.
The syntax is:
{ IF { MERGEFIELD FieldName }= "value" "true" "false" }
So in your case check for an empty value (= "") and in the true part add
the mergefield, in the false part use an empty string ("")

The {} can be entered with Ctrl + F9

{ IF { MERGEFIELD FieldName }= "" "" "{ FieldName }" }

Hope this helps,
kind regards,
Astrid
 
K

KSH

Thanks Astrid,

The merge field is on one line but when I set the replace variable to "" the
line does not shift up automatically, is it because I am doing .replacement
and not .merge?
 
K

KSH

To clarify, I am not actually using merge. I am just reading through the
document and replacing because I will have a lot of logic to go through when
populating the documents and do not want to use the merge. I am using
..Replacement and I am not sure if it is possible or how to delete the line in
the document that is tagged with {address line 2} if the person does not have
a line 2. Is there a way to bookmark the line and then delete it if the case
is true?
 
C

Cindy M.

Hi =?Utf-8?B?S1NI?=,
To clarify, I am not actually using merge. I am just reading through the
document and replacing because I will have a lot of logic to go through when
populating the documents and do not want to use the merge. I am using
..Replacement and I am not sure if it is possible or how to delete the line in
the document that is tagged with {address line 2} if the person does not have
a line 2. Is there a way to bookmark the line and then delete it if the case
is true?
In this case you need to include the paragraph mark (and anything else that might
follow the search term) in the search criteria. Then executing Replace will
remove that, too.

Or you need to work directly with the range to remove characters. I'm having some
difficulty working out exactly what your code is doing, based on the sample you
provide. But I *think* if you work with a Range object you'd get the finer
control you need:

Dim rngFind as Word.Range

Set rngFind = docWord.Content

With rngFind.Find ' and so on

If Find.Execute is successful, rngFind is located at the found term (instead of
being the entire document). Then you can work with that range, such as deleting
the entire paragraph:
rngFind.Paragraphs(1).Delete

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

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

Astrid

Hi,

Sorry I didn't read your question good enough, got on the wrong foot by the
subject of the message ;-)
In addition to Cindy's post, you might want to consider combining the fields
in one for the whole address, and in your recordset create an additioneel
field which is the combined result of all these fields as well. If you use
this method, you won't need to
delete fields in the document itself.

Regards,
Astrid
 

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