Adding a range object to a Mail Merge field Result

S

Simon

Any assistence for the following problem will be most appreciated.

I am writing a macro that takes codes and text from one document, called
Structure.doc and inserts the text, and text associated with several codes
[which are stored in tables in several other word documents] into an existing
report.

The codes are stored in a word document that contains 1 table with 2
columns. The first column hads the code, the second column has the text
[which may include numbering, formating, tables etc]

This macro works fine.

However, in the Strucutre document, I need to be able to toggle the codes on
and off where on displays just the codes, and off displays the full text
assosated with the code, just as in the Mail Merge preview.

However, as the codes are stored in several documents, and only certain
codes from each document are selected, I do not think I can use the in-built
Mail-merge function.

My thought was to use a mail merge field that the user could enter the code
in the field, and then use a macro to insert the field Result from the
appropriate document. I found this will work if using the
fields(i).Result.text except that tables and all the formating is lost.

From my understanding, the fields(i).Result is a range object and so I
should be able to insert a Selection.Range object into the fields(i).Result.
However, whenever I use the code:

ReportDoc.fields(i).Result = Selection.Range

word crashes. Can anyone explain what I am doing wrong?
If the above is not clear enough, let me know and I will try to reword.

Thanks,
Simon
 
S

Simon

To all out there;

I solved my problem and thought I should post the solution for anyone else
who is looking.

The code required was instead of using:

ReportDoc.fields(i).Result

I added FormattedText to the end to make

ReportDoc.Fields(i).Result.FormattedText = Selection.Range.FormattedText.

This works with only one other issue. It the fields are blank/have no
result to start with, instaed of putting the text in the field, it is added
after the field.
The fix for this is before trying to add a result to a field, to loop
through all the fields and update them. The following code is appropriate:

NumFields = ReportDoc.Fields.Count
For i = 1 to NumFields
ReportDoc.Fields(i).Update
ReportDoc.Fields(i).ShowCodes = True
Next i

Hope this helps someone in the future,
Simon
 

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