none
C# Word Interop FormFields

    Question

  • Hi,

    I am trying to add text to the form fields of a protected word document in c#. However, when i try to add the text, I get an error about the selection being protected.

    oBookMark = "From_Address";
     ((Microsoft.Office.Interop.Word.FormField)oDoc.FormFields.get_Item(ref oBookMark)).Range.Text = "test";

    The formfield has a bookmark name of "From_Address", and when I turn off the document protection I can insert the text without a problem.

    I think I am accessing the form field incorrectly, since the inserted text seems to replace the form field rather than being inserted into it. Any help someone could provide on the CORRECT way to access and add inner text to form fields would be greatly appreciated.

    Thanks!
    Thursday, May 29, 2008 5:51 AM

Answers

  • Funny stuff, I struggle with this for 2 hours, then 5 minutes after my first post I figure it out.

    Instead of :
    ((Microsoft.Office.Interop.Word.FormField)oDoc.FormFields.get_Item(ref oBookMark)).Range.Text = "test";

    Use:
    ((Microsoft.Office.Interop.Word.FormField)oDoc.FormFields.get_Item(ref oBookMark)).Result = "test";

    I imagine I will have plenty more questions, as the interop libraries are a bit finicky.

    Thursday, May 29, 2008 5:59 AM

All replies

  • Funny stuff, I struggle with this for 2 hours, then 5 minutes after my first post I figure it out.

    Instead of :
    ((Microsoft.Office.Interop.Word.FormField)oDoc.FormFields.get_Item(ref oBookMark)).Range.Text = "test";

    Use:
    ((Microsoft.Office.Interop.Word.FormField)oDoc.FormFields.get_Item(ref oBookMark)).Result = "test";

    I imagine I will have plenty more questions, as the interop libraries are a bit finicky.

    Thursday, May 29, 2008 5:59 AM
  • To change original field text use:

    ((Microsoft.Office.Interop.Word.FormField)oDoc.FormFields.get_Item(ref oBookMark))
    .TextInput.Default = "test";

    and then update fields with:

    oDoc.
    Fields.Update();
    Monday, August 10, 2009 1:12 PM
  • Thank you very much Humy, thanks to you I succeeded in my project :)
    Thursday, March 15, 2012 12:56 PM