Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.word.mailmerge.fields
|
|||
|
|||
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
Posted to microsoft.public.word.mailmerge.fields
|
|||
|
|||
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
Posted to microsoft.public.word.mailmerge.fields
|
|||
|
|||
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
Posted to microsoft.public.word.mailmerge.fields
|
|||
|
|||
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 | |
|
|
Similar Threads | ||||
Thread | Forum | |||
part of a document I want to merge seems to be in the header box. | Microsoft Word Help | |||
How do I mail merge to EMAIL from MS Word AND add a pdf attachment | Mailmerge | |||
Merge document with form fields | Microsoft Word Help | |||
Form text fields are disappearing during a merge to new document. | Mailmerge | |||
Populate Merge Fields via VB.NET | Mailmerge |