Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Raul Raul is offline
external usenet poster
 
Posts: 2
Default How do I walk all the merge fields in a doc document from C#

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.


  #2   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Peter Jamieson Peter Jamieson is offline
external usenet poster
 
Posts: 4,582
Default How do I walk all the merge fields in a doc document from C#


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

"Raul" wrote in message
...
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.



  #3   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Raul Raul is offline
external usenet poster
 
Posts: 2
Default How do I walk all the merge fields in a doc document from C#

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
}
}

"Peter Jamieson" wrote in message
...

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

"Raul" wrote in message
...
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.





  #4   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Peter Jamieson Peter Jamieson is offline
external usenet poster
 
Posts: 4,582
Default How do I walk all the merge fields in a doc document from C#

Raul,

Thanks for the useful feedback.

Peter Jamieson
"Raul" wrote in message
...
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
}
}

"Peter Jamieson" wrote in message
...

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

"Raul" wrote in message
...
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.







Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
part of a document I want to merge seems to be in the header box. iamweetodddid Microsoft Word Help 3 January 24th 07 03:21 PM
How do I mail merge to EMAIL from MS Word AND add a pdf attachment Lily@Insight Mailmerge 24 January 15th 07 09:33 PM
Merge document with form fields Dottie Microsoft Word Help 1 May 26th 06 04:20 AM
Form text fields are disappearing during a merge to new document. Trevor Drew Mailmerge 7 January 30th 06 09:35 PM
Populate Merge Fields via VB.NET ben Mailmerge 5 June 21st 05 05:43 PM


All times are GMT +1. The time now is 11:15 PM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"