1

I need to add a few fields to a Word 2010 DOTX template which are to be populated automatically with custom content at "run time" when the document is opened in a C# program using Word Interop services. I don't see any way to assign a unique name to "Ask" or "Fill-In" fields when adding them to the template via the QuickParts ribbon-menu option.

When I iterate the document.Fields collection in the C# program, I must know which field I'm referencing, so it can be assigned the correct value.

It seems things have changed between previous versions of Word and Word 2010. So, if you answer please make sure your answer applies to 2010. Don't assume that what used to work in previous versions works in 2010. Much appreciated, since I rarely work with Word and feel like a dolt when trying to figure out the ribbon menuing in 2010.

Beth
  • 9,531
  • 1
  • 24
  • 43
Tim
  • 8,669
  • 31
  • 105
  • 183

1 Answers1

0

You are correct in that fields don't necessarily have a built-in way to uniquely distinguish themselves from other field instances (other than its index in the Fields collection). However, you can use the Field.Type property to test for wdFieldAsk or wdFieldFillIn . If this is not narrow enough to ID then you will need to parse your own unique identifier from the Field.Code. For example, you can construct your FILLIN field as:

{ FILLIN "Hello, World!" MYIDENTIFER }

when you iterate through your document.Fields collection just have a test for the identifier being in the string. EDIT: example:

For Each fld In ActiveDocument.Fields
    If InStr("CARMODEL", fld.Code) <> 0 Then
        ''this is the carmodel field
    End If
Next

Another alternative - seek your specific field with a Find.Text for "^d MYIDENTIFIER" (where ^d is expression for 'field code')

Let me know if this helps and expand on your question if any gaps.

JohnZaj
  • 3,080
  • 5
  • 37
  • 51
  • @Thanks for the reply jJack. What field property should I be looking at when testing for "the identifier being in the string"? What field property will contain "the string"? Also, are there two strings within the braces {} separated by a space? Do you have an extra or a missing quotation mark in your example? `{ FILLIN "Hello, World!" MYIDENTIFIER"}` – Tim Sep 27 '12 at 15:34
  • Also, I added the field to my document manually by typing the string `{ FILLIN "carmodel" }` yet when I iterate the fields collection, the field does not show up in the collection. – Tim Sep 27 '12 at 15:40
  • Type Ctrl+F9 to get the brackets for fields, then FILLIN "carmodel" – JohnZaj Sep 27 '12 at 18:56
  • Let me know if my edits help or not. If this isn't helpful, you should edit your question and add some relevant code you have so far so that I understand the problem better. – JohnZaj Sep 30 '12 at 04:47
  • I found out that what I'm trying to accomplish is best done not with "fields" but with "ContentControls". Thank you for the help. – Tim Oct 05 '12 at 14:15