简体   繁体   中英

Changing the formating of text using word interop and c#

I have a word file having some text and I want to change the formatting of the text like Tabing,numbaring,page number calculation ,bold etc. How could I do it with using C# and word interop.

Thanks all

If you can use docx, I would recommend the OpenXML Api from Microsoft. Much easier than Word Interop.

Interop is more Complex but can be used for other file formats besides Docx

With interop either use:

// Make Text Between Start And End Bold
MyWordDoc.Range(start, end).Font.Bold = 1;

or

// Make Selected Text Bold
MyWordApp.Selection.Range.Font.Bold = 1;

Hope this helps

Here's some sample C# code with a few demonstrations of Interop Word usage, including one approach to selectively setting individual words to a bold font. The trick is to collapse the range in between every change or deviation with Word styles and Word object types.

using System;
using Word = Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices;

namespace WordDemo {
    public class WordDemo {
        string wordFile;
        Word.Application wordApp;
        Word.Document wordDoc;
        Word.Range range;

        public WordDemo() {
            // Set your filename here; defaults to saving to your desktop
            wordFile = SetFileDetails("WordCreationTest.docx");
            InitializeWordObjects();
            AddTextToWord();
            AddTableToWord();
            SaveCloseQuitWord();
        }

        private string SetFileDetails(string filename) {
            string userDesktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            return userDesktop + "\\" + filename;
        }

        private void InitializeWordObjects() {
            wordApp = new Word.Application();
            wordApp.Visible = true;
            wordDoc = new Word.Document();
            wordDoc = wordApp.Documents.Add();
            range = wordDoc.Range();
        }

        private void AddTextToWord() {
            range.InsertAfter("Testing 1, 2, 3");
            range.InsertParagraphAfter();
            range.InsertParagraphAfter();
            range.InsertAfter(
                "Adding a collection of text to the document here, some " +
                "really incredibly interesting stuff to read, I'm sure... " +
                "or at least that's the rumor. Also, "
                );
            range.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
            range.InsertAfter("these words");
            // Interop doesn't recognize C# booleans, so we convert to integers:
            range.Font.Bold = Convert.ToInt32(true);
            range.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
            range.InsertAfter(" are bold.");
            range.Font.Bold = Convert.ToInt32(false);
            range.InsertParagraphAfter();
            range.InsertParagraphAfter();
        }

        private void AddTableToWord() {
            range.InsertAfter(
                "Okay, well, that was interesting. Next, here is a table creation " +
                "demonstration. This table is comprised of 3 rows and 4 columns, " +
                "defaulting to autofit, and setting the inside and outside " +
                "line styles to single line style."
                );
            range.InsertParagraphAfter();
            range.InsertParagraphAfter();
            range.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
            Word.Table table = wordDoc.Tables.Add(
                range, 3, 4,
                Word.WdDefaultTableBehavior.wdWord9TableBehavior
                );
            table.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
            table.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
            range = wordDoc.Content;
            range.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
            range.InsertParagraphAfter();
        }

        private void SaveCloseQuitWord() {
            wordDoc.SaveAs2(wordFile);
            wordDoc.Close();
            Marshal.FinalReleaseComObject(wordDoc);
            wordApp.Quit();
            Marshal.FinalReleaseComObject(wordApp);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM