Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Writing a Microsoft Word letter . . .

using Word = Microsoft.Office.Interop.Word;

The following code will BOLD the sentence. How do you specify a range that includes only one word . . . say you want to bold only the word electronically in the sentence or paragraph . . . how would you specify the range. I've read info in help but cannot find any examples.

Word.Paragraph oPara1;
oPara1.Range.Font.Bold = 1;
oPara1.Range.Text = "We will file your 2013 Federal Individual Income Tax Return electronically with the IRS.";
Posted
Updated 7-Mar-20 9:03am
v2

This[^] should help.

[Please encourage participation by up-voting solutions or answers that work for you]
 
Share this answer
 
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.

C#
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);
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900