none
How to iterate form fields in a word document and insert text into them RRS feed

  • Question

  • Hi there. 

    I have a document which I need to perform a "mail merge" (find / replace) operation on. However, the text I need to find and replace is contained in form fields in the word document. There is no way I can remove these from the source document. 

    The existing class library I have written for office interop tasks will only find and replace text in the main body of a word document at the moment. 

    I have been trying to iterate through the form fields in the document by using the FormFields property of the word document object to insert some text but nothing is working for me. 

    I could really do with some pointers on how to get started for this. 

     

    Thanks

    Wednesday, March 9, 2011 9:48 AM

Answers

  • After more google searching, I have found the answer to my problem. 

    The document I am dealing with uses both bookmark fields and form fields. I have assigned a unique name to each of these fields in the source document and my code is as follows (snippet for just this section):

     if (range.Find.Execute(ref findText, ref matchCase, ref matchWholeWord, ref missing,
                      ref missing, ref missing, ref missing, ref findWrap, ref missing,
                      ref replaceText, ref replaceAll, ref missing, ref missing, ref missing, ref missing))
                    {
                      // text replace successfully - continue to the next dictionary entry
                      continue;
                    }
                    else
                    {
                      bool replacementDone = false;
    
                      // if this text has not been processed, see if it is a checkbox that needs to be processed
                      try
                      {
                        bool checkBoxValue = Convert.ToBoolean(replaceText);
    
                        for (int i = 1; i <= doc.FormFields.Count; i++)
                        {
                          object oCheckBox = i;
    
                          Microsoft.Office.Interop.Word.FormField f = doc.FormFields.get_Item(ref oCheckBox);
    
                          if (f != null && !string.IsNullOrEmpty(f.Name) && f.Name.Equals(findText.ToString(), StringComparison.OrdinalIgnoreCase))
                          {
                            f.CheckBox.Value = checkBoxValue;
                            replacementDone = true;
                            break;
                          }
                        }
                      }
                      catch (FormatException) 
                      { 
                        // this is not a true / false value to set checkbox state, continue to perform find / replace
                      }
    
                      if (!replacementDone)
                      {
                        // Text to find not found in plain text in the document - iterate through bookmarks
                        for (int i = 1; i <= doc.Bookmarks.Count; i++)
                        {
                          object objI = i;
    
                          Microsoft.Office.Interop.Word.Bookmark b = doc.Bookmarks.get_Item(ref objI);
    
                          // the bookmark name will be the text to replace
                          if (b != null && !string.IsNullOrEmpty(b.Name) && b.Name.Equals(findText.ToString(), StringComparison.OrdinalIgnoreCase))
                          {
                            // found the matching bookmark, now enter the replacement text
                            b.Range.Text = replaceText.ToString();
                            break;
                          }
                        }
                      }                  
                    }
    

    Thursday, March 10, 2011 4:47 PM

All replies

  • See the article "How to find the name of the current formfield” at:

    http://www.word.mvps.org/FAQs/TblsFldsFms/GetCurFmFldName.htm

    Then use the .Result attribute of the formfield to assign a value to it
     -- Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "kevinjp" wrote in message news:4f3a8abd-60ff-40e6-9e29-0eff6e073395@communitybridge.codeplex.com...

    Hi there.

    I have a document which I need to perform a "mail merge" (find / replace) operation on. However, the text I need to find and replace is contained in form fields in the word document. There is no way I can remove these from the source document.

    The existing class library I have written for office interop tasks will only find and replace text in the main body of a word document at the moment.

    I have been trying to iterate through the form fields in the document by using the FormFields property of the word document object to insert some text but nothing is working for me.

    I could really do with some pointers on how to get started for this.



    Thanks


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    Thursday, March 10, 2011 11:01 AM
  • After more google searching, I have found the answer to my problem. 

    The document I am dealing with uses both bookmark fields and form fields. I have assigned a unique name to each of these fields in the source document and my code is as follows (snippet for just this section):

     if (range.Find.Execute(ref findText, ref matchCase, ref matchWholeWord, ref missing,
                      ref missing, ref missing, ref missing, ref findWrap, ref missing,
                      ref replaceText, ref replaceAll, ref missing, ref missing, ref missing, ref missing))
                    {
                      // text replace successfully - continue to the next dictionary entry
                      continue;
                    }
                    else
                    {
                      bool replacementDone = false;
    
                      // if this text has not been processed, see if it is a checkbox that needs to be processed
                      try
                      {
                        bool checkBoxValue = Convert.ToBoolean(replaceText);
    
                        for (int i = 1; i <= doc.FormFields.Count; i++)
                        {
                          object oCheckBox = i;
    
                          Microsoft.Office.Interop.Word.FormField f = doc.FormFields.get_Item(ref oCheckBox);
    
                          if (f != null && !string.IsNullOrEmpty(f.Name) && f.Name.Equals(findText.ToString(), StringComparison.OrdinalIgnoreCase))
                          {
                            f.CheckBox.Value = checkBoxValue;
                            replacementDone = true;
                            break;
                          }
                        }
                      }
                      catch (FormatException) 
                      { 
                        // this is not a true / false value to set checkbox state, continue to perform find / replace
                      }
    
                      if (!replacementDone)
                      {
                        // Text to find not found in plain text in the document - iterate through bookmarks
                        for (int i = 1; i <= doc.Bookmarks.Count; i++)
                        {
                          object objI = i;
    
                          Microsoft.Office.Interop.Word.Bookmark b = doc.Bookmarks.get_Item(ref objI);
    
                          // the bookmark name will be the text to replace
                          if (b != null && !string.IsNullOrEmpty(b.Name) && b.Name.Equals(findText.ToString(), StringComparison.OrdinalIgnoreCase))
                          {
                            // found the matching bookmark, now enter the replacement text
                            b.Range.Text = replaceText.ToString();
                            break;
                          }
                        }
                      }                  
                    }
    

    Thursday, March 10, 2011 4:47 PM