Word Fields returning full merge condition and not just fieldname

Andrew Kennard 146 Reputation points
2023-09-21T09:47:58.0533333+00:00

I have code that extracts the names of the merge fields in a Word template

This has worked fine but I have now been caught out by people using conditional content based on merge field content

My code uses COM to get the Fields collection and then for each field it gets the Code property to give it the Range and then finally the Text property of the Range

For normal merge fields this gives

MERGEFIELD "FirstName"

I then remove the unwanted characters to leave me with FirstName

Now if the merge field is conditional the Text property contains something like

{ IF { MERGEFIELD Gender } = “Male” “Mr.” “Ms. }

So I can look for MERGEFIELD and take the next word / check for quotes in case of space in name

If that safe?

Are there other types of content that will come back in the Text property that this won't work for?

Thanks

Word
Word
A family of Microsoft word processing software products for creating web, email, and print documents.
816 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.