Word 2010 can Field added via QuickParts be given an ID and later referenced in document.Fields collection

百般思念 提交于 2019-12-11 07:56:53

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/12608038/word-2010-can-field-added-via-quickparts-be-given-an-id-and-later-referenced-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!