none
Add a field to word document using add in RRS feed

  • Question

  • I have created a simple word add in that insert some text to the word document,

    Microsoft.Office.Interop.Word.Document Doc = Globals.ThisAddIn.Application.ActiveDocument;
    Doc.Content.InsertAfter("[USER_NAME]");

    This code will insert [USER_NAME] as a text in the document. But I have noticed that in Word I can insert fields.

    So I was wondering if I can change something in my code to make my add-in insert [USER_NAME] as a field not as a text?

    Thanks

    Tuesday, November 21, 2017 10:19 AM

Answers

  • Hi Riyad Bandi,

    below is an example how to add fields in document.

    private void button1_Click(object sender, RibbonControlEventArgs e)
            {
                object fieldPages = WdFieldType.wdFieldPage;
                object fieldNumPages = WdFieldType.wdFieldNumPages;
                object fieldMerge = WdFieldType.wdFieldMergeField;
                object fieldAuthor = WdFieldType.wdFieldAuthor;
                object fieldIF = WdFieldType.wdFieldIf;
    
                Document activeDoc = Globals.ThisAddIn.Application.ActiveDocument;
    
                Range Rng = activeDoc.Sections.Last.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
    
                object txt = string.Empty;
    
                Field field = Rng.Fields.Add(Rng, ref fieldIF, "{PAGE} = {PAGENUM} \"{AUTHOR}\" \"\"", false);
    
                var ifExprRng = field.Code;
    
                var find1 = ifExprRng.Find.Execute("{PAGE}");
    
                activeDoc.Fields.Add(ifExprRng, ref fieldPages, ref txt, false);
    
                var find2 = ifExprRng.Find.Execute("{PAGENUM}");
    
                activeDoc.Fields.Add(ifExprRng, ref fieldNumPages, ref txt, false);
    
                var find3 = ifExprRng.Find.Execute("{AUTHOR}");
    
                activeDoc.Fields.Add(ifExprRng, ref fieldAuthor, ref txt, false);
    
                field.Update();
            }

    Reference:

    Fields.Add method

    you can try to modify the example as per your requirement.

    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.

    • Marked as answer by Riyad Bandi Sunday, November 26, 2017 10:31 AM
    Wednesday, November 22, 2017 6:04 AM
    Moderator

All replies

  • Hi Riyad Bandi,

    below is an example how to add fields in document.

    private void button1_Click(object sender, RibbonControlEventArgs e)
            {
                object fieldPages = WdFieldType.wdFieldPage;
                object fieldNumPages = WdFieldType.wdFieldNumPages;
                object fieldMerge = WdFieldType.wdFieldMergeField;
                object fieldAuthor = WdFieldType.wdFieldAuthor;
                object fieldIF = WdFieldType.wdFieldIf;
    
                Document activeDoc = Globals.ThisAddIn.Application.ActiveDocument;
    
                Range Rng = activeDoc.Sections.Last.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
    
                object txt = string.Empty;
    
                Field field = Rng.Fields.Add(Rng, ref fieldIF, "{PAGE} = {PAGENUM} \"{AUTHOR}\" \"\"", false);
    
                var ifExprRng = field.Code;
    
                var find1 = ifExprRng.Find.Execute("{PAGE}");
    
                activeDoc.Fields.Add(ifExprRng, ref fieldPages, ref txt, false);
    
                var find2 = ifExprRng.Find.Execute("{PAGENUM}");
    
                activeDoc.Fields.Add(ifExprRng, ref fieldNumPages, ref txt, false);
    
                var find3 = ifExprRng.Find.Execute("{AUTHOR}");
    
                activeDoc.Fields.Add(ifExprRng, ref fieldAuthor, ref txt, false);
    
                field.Update();
            }

    Reference:

    Fields.Add method

    you can try to modify the example as per your requirement.

    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.

    • Marked as answer by Riyad Bandi Sunday, November 26, 2017 10:31 AM
    Wednesday, November 22, 2017 6:04 AM
    Moderator
  • Hello Deepak, 

    Thank you for your reply, 

    So this code will add a field when i click the add in button? or it will create a field in my fields collection?

    And let's say that i want to add a field user_name, so when the user click the add in button it will insert [user_name] as a field, in the code where should i add the text "user_name" ?

    I'm sorry but i am new to this.

    Best regards


    Thursday, November 23, 2017 9:47 AM
  • Hi Riyad Bandi,

    did you try to run the code on your side?

    if no, I suggest you to run it and check the output.

    Fields.Add method is used to add fields.

    in the example , you will notice that we add the author name.

    which will show you the author name(user name) at the footer.

    you just need to understand the example and need to modify it as per your requirement.

    if you are not familiar with Word VBA object model then first you can try to refer 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.

    Friday, November 24, 2017 5:10 AM
    Moderator