none
Adding a Form Field to a document with an empty name RRS feed

  • Question

  • What I'm trying to do: add a text form field that does not contain a bookmark.

    Code I have tried: 

    try
    {
    
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine("An error occured: '{0}'", ex);
    }

    The exception im getting: 

    A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in projectName.dll
    An error occured: 'System.Runtime.InteropServices.COMException (0x80004005): Error HRESULT E_FAIL has been returned from a call to a COM component.
       at Microsoft.Office.Interop.Word.FormField.set_Name(String prop)
       at projectName.methodName(IRibbonControl control) in c:\directory of project:line 217'


    Thursday, June 1, 2017 9:17 PM

All replies

  • Hi PhoneixFlyer,

    I try to test the code on my side.

    I find that you cant leave name empty.

    you have to specify the name.

    when I try to run the code without passing the name, it gives an error.

    when I try to pass the name , code run successfully without any error.

    I suggest you to pass valid name if you want to use ".name" property to set name.

    if you don't want to give name then remove that property from your code to run code without any error.

    Reference:

    FormFields.Add Method (Word)

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, June 2, 2017 3:23 AM
    Moderator
  • Thanks Deepak, I am trying to remove the bookmark behind the formfield. When a text formfield is added to the document, it creates a bookmark, and I am trying insert a form field without a bookmark. 
    Friday, June 2, 2017 1:08 PM
  • Hi PhoneixFlyer,

    you can see that when you don't pass the name in parameter then it returns an error.

    when we remove that name parameter then also word add the name by itself.

    so it looks like whatever we try , it will going to add bookmark.

    I did not get anyway to create form fields without bookmark.

    so there is only one work around that may solve your issue.

    after adding the form fields you can try to delete the bookmarks programmatically.

    if you know the name of the bookmark then you can delete it by its name.

    or you can try to loop through bookmarks and delete it.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 5, 2017 6:53 AM
    Moderator
  • Hi PhoneixFlyer,

    We could not add FormField without name.

    Per to this document FormField Object (Word), use FormFields(index), where index is a bookmark name or index number, to return a single FormField object. I recommend you not to remove bookmark, otherwise you will not enable to find the FormField by bookmark name.

    If you insist on creating FormField without bookmark, you could follow the suggestion from Deepak to delete the bookmark from bookmarks.

    Here is a simple code:

    Microsoft.Office.Tools.Word.Document extendedDocument = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveDocument);
    
                    Word.Selection currentSelection = Globals.ThisAddIn.Application.Selection;
    
                    Word.Range oldRange= Globals.ThisAddIn.Application.Selection.Range;
                    Word.FormField ff= extendedDocument.FormFields.Add(oldRange, Word.WdFieldType.wdFieldFormTextInput,);
                    Word.Bookmarks bms = extendedDocument.Bookmarks;
                    foreach (Word.Bookmark bm in bms)
                    {
                        
                            if (bm.Name == ff.Name)
                            {
                                bm.Delete();
                            }
                    }

    Best Regards,


    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, June 8, 2017 7:12 AM
  • Thanks but I made a work around for this problem. 
    Thursday, June 8, 2017 3:51 PM
  • I am glad you have found a work around, and I would suggest you share us the solution, and you could mark it as answer. Then others who run into the same issue would find the solution easily.

    Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, June 9, 2017 1:15 AM