locked
C# Office.Interop How to select Text and apply template RRS feed

  • Question

  • Hello,

    I am new to C# and am writing a small application with the Microsoft.Office API which can insert documents into a new one and edit the newly create document. I am currently working on selecting all text and changing the font-family and restart numbered lists with the help of these functions:

    https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.word.list.applylisttemplate?view=word-pia

    https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.word.editor.selectall?view=word-pia

    But I don't know how to use them

                var realPathCover = "\\Document";
                //Object of the end of the document
                Object endOfDoc = "\\endofdoc";
                //Object as infill for missing values
                Object missing = System.Reflection.Missing.Value;
                //Objects of true and false
                Object oTrue = true;
                Object oFalse = false;
                //Creating the objects Word and Document
                Word._Application word = new Word.Application();
                Word._Document doc = new Word.Document();
    
                //Making the application visible
                word.Visible = true;
                //Adding a new Document to the Application
                doc = word.Documents.Add(ref missing, ref missing, ref missing, ref missing);
                //Setting up the paper
                doc.PageSetup.FooterDistance = word.CentimetersToPoints(1);
                doc.PageSetup.HeaderDistance = word.CentimetersToPoints(1);
                doc.PageSetup.DifferentFirstPageHeaderFooter = -1;
                
    
                object gotoPage = Word.WdGoToItem.wdGoToLine;
                object gotoLast = Word.WdGoToDirection.wdGoToLast;
                Console.WriteLine(realPathCover);
                //HERE COMES THE FIRST PAGE
                doc.Bookmarks.get_Item(ref endOfDoc).Range.InsertFile(realPathCover, ref missing, ref oFalse, ref oFalse, ref oFalse);
    
                Word.Editor editor = new Editor();
                editor.SelectAll();
    
                Word.ListTemplate template = doc.ListTemplates.Add(ref oTrue, ref missing);
                word.Selection.Range.ListFormat.ApplyListTemplate(template, ref oFalse);

    Thanks in advance and appreciate your help

    Wednesday, November 11, 2020 12:25 PM

Answers

All replies

  • Hi Vadim199,
    Based on your code, what problem or error did you encounter?
    In order to solve the problem more effectively, please describe in detail.
    And I suggest you refer to this document in first.
    Hope this helpful to you.
    Best Regards,
    Daniel Zhang


    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, November 12, 2020 2:51 AM
  • Hi Daniel, I am sorry for not responding to your anwser for a while. I succeeded in applying a Template on my document thanks to your suggestion and noticed, that I had the Boolean for the object

    ContinueFromPreviousList

    set to true instead of false. Now my only problem is, that I do not know how to configure the interface Word.Editor, so I can select all Text and change it to a single Font. With this Code:

    Word.Editor editor = new Editor();
                editor.SelectAll();

    I am getting the Error "Cannot create an instance of the abstract class or interface Editor".

    What am I doing wrong and is there maybe a better way to change the Font of the newly created document to a single font? I am creating a new instance of the Editor at the beginning of my setup.

    Thanks in advance and appreciate your help :)

    Best regards

    Vadim199


    • Edited by Vadim199 Wednesday, December 2, 2020 11:31 AM
    Wednesday, December 2, 2020 11:31 AM
  • Hi Vadim199,
    First, Word.Editor is a interface and you cannot instantiate interface. You need to implement it.
    And you can use Microsoft.Office.Interop.Word to set the font of document.
    Here are some code examples in these links you can refer to.
    how to change font size of word document using .net
    Change word document body font and Footnotes font
    How to: Programmatically format text in documents
    Best Regards,
    Daniel Zhang


    "Visual c#" forum will be migrating to a new home on Microsoft Q&A ! We invite you to post new questions in the "Developing Universal Windows apps" forum’s new home on Microsoft Q&A ! For more information, please refer to the sticky post.

    • Marked as answer by Vadim199 Thursday, December 3, 2020 11:39 AM
    Thursday, December 3, 2020 5:37 AM
  • Hi Daniel,

    thanks for the helpful links. I was able to implement the code and change the font to a single font-family :)

    Best regards

    Vadim199

    Thursday, December 3, 2020 11:39 AM