none
C# Word: Add new in sequence field RRS feed

  • Question

  • I have made a new sequence field in Word named MyList. Now I can manually type MyList and press F3, it inserts a new entry in this sequence. How do I do the same using C#? No need to create a new list, but just an entry there. So insert a new entry where the cursor is.
    Tuesday, July 18, 2017 10:11 AM

Answers

  • If you just need to insert a SEQ field with the list name MyList at the Insertion point, using Interop and recent versions of C# you can use the following, where wordApp is a reference to the Word Application.

    wordApp.Selection.Fields.Add(wordApp.Selection.Range, Word.WdFieldType.wdFieldSequence, "MyList", false);

    but when inserting fields in this way it can be more reliable to insert an empty field and provide the complete field text:

     wordApp.Selection.Fields.Add(wordApp.Selection.Range, Word.WdFieldType.wdFieldEmpty, "SEQ MyList", false);





    Peter Jamieson


    • Edited by Peter Jamieson Wednesday, July 19, 2017 1:38 PM
    • Marked as answer by ArtKilp Thursday, August 3, 2017 12:22 AM
    Wednesday, July 19, 2017 1:37 PM

All replies

  • Hi ArtKilp,

    Do you mean you want to replace a string using Building Block? You could use Range.InsertAutoText after you type the name of a Building Block, or you could select the name and use the name as BuildingBlock name to get BuildingBlock value from BuildingBlockEntries collection. Building Blocks are stored in a template.

    Here is the example.

                //wdapp is word application object

                Word.Document doc = wdapp.ActiveDocument;

                Word.Range rng = wdapp.Selection.Range;

                Word.Template tmp=doc.get_AttachedTemplate();

                try

                {

                    //replace Building Block after you type the name of the building block

                    rng.InsertAutoText();

                }

                catch (Exception ex)

                {

                    MessageBox.Show(ex.Message);

                }

    Or you could

                             try

                {

                    //select the name and use it as name to get building block from BuildingBlockEntries

                    string name = rng.Text.Trim();

                    Word.BuildingBlock bdBlock = tmp.BuildingBlockEntries.Item(name);

                    bdBlock.Insert(rng);            }

               catch (Exception ex)

                {

                    MessageBox.Show(ex.Message);

                }

    Best Regards,

    Terry

    Wednesday, July 19, 2017 7:58 AM
  • If you just need to insert a SEQ field with the list name MyList at the Insertion point, using Interop and recent versions of C# you can use the following, where wordApp is a reference to the Word Application.

    wordApp.Selection.Fields.Add(wordApp.Selection.Range, Word.WdFieldType.wdFieldSequence, "MyList", false);

    but when inserting fields in this way it can be more reliable to insert an empty field and provide the complete field text:

     wordApp.Selection.Fields.Add(wordApp.Selection.Range, Word.WdFieldType.wdFieldEmpty, "SEQ MyList", false);





    Peter Jamieson


    • Edited by Peter Jamieson Wednesday, July 19, 2017 1:38 PM
    • Marked as answer by ArtKilp Thursday, August 3, 2017 12:22 AM
    Wednesday, July 19, 2017 1:37 PM
  • Hi ArtKilp,

    Has your original issue been resolved? If it has, I would suggest you mark the helpful reply as answer or share yours solution and mark it as answer to close this thread.
    If not, please feel free to let us know your current issue.

    Best Regards,

    Terry

    Wednesday, August 2, 2017 8:37 AM