Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Mailmerge and Fax / January 2007

Tip: Looking for answers? Try searching our database.

How do I walk all the merge fields in a doc document from C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Raul - 30 Jan 2007 22:41 GMT
Hi there,

I have a word doc with a few mail merge fields, some of those fields are
inside text boxes.
But I can get to those merge fields inside the text boxes.

here is a piece of the code i use
Enumerators oFielsEnumerator = _oWordDoc.MailMerge.GetEnumerator();
while(oFieldEnumerator.MoveNext)
{
   WordMailMergeField field = oFieldEnumerator.Current as
Word.MailMergeField;
   field.Select();
   Word.Range FieldRange = _oWordApp.Selection.Range;
   string strField = FieldRange.Text;
   // this will show the merge fields with the << >>
}

Any advise is welcome.
Raul.
Peter Jamieson - 31 Jan 2007 11:39 GMT
There are two problems:
a. how do you iterate through the fields in text boxes?
b. how do you get information about a field when you have got a reference
to it?

For (a), you probably need to think about iterating through the Shapes
collection then using the Shape's range to iterate through the fields or
mergefields in that Shape.

e.g. to return the text of field 1 in Shape 1,

activedocument.Shapes(1).Select

(You may need to change these code snippets for C#, and in particular change
the indexes from 1-based to 0-based. I don't know).

For (b), it depends on what you want to do, but there are several properties
of a Field or MailMergeField - e.g.

Selection.Range.Fields(1).Code

When working with fields you can see how badly designed Ranges in Word are
because they rely on a character count which changes depending on whether
your field codes are displayed, and so on. So be careful using them!

Peter Jamieson

> Hi there,
>
[quoted text clipped - 16 lines]
> Any advise is welcome.
> Raul.
Raul - 31 Jan 2007 14:39 GMT
Peter,

Thanks for the help, It realy helped.
Here is the snippet in C#

Enumerator oFieldEnumerator = WordDoc.Shapes.GetEnumerator();
while(oFiledEnumerator.MoveNext())
{
   Word.Shape oShape = oFieldEnumerator as Word.Shape;
   // select the object
   Object replace = Type.Missing;
   oShape.Select(ref replace);
  // I care about Text boxes only
   if(oShape.Type == Microsoft.Office.Core.MsoShapeType.msoTextBox)
   {
       // note , to get the text you need to look at oShape.AlternativeText
       // do what ever here
   }
}

> There are two problems:
> a. how do you iterate through the fields in text boxes?
[quoted text clipped - 43 lines]
>> Any advise is welcome.
>> Raul.
Peter Jamieson - 31 Jan 2007 14:56 GMT
Raul,

Thanks for the useful feedback.

Peter Jamieson
> Peter,
>
[quoted text clipped - 65 lines]
>>> Any advise is welcome.
>>> Raul.

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.