different format into one single line Interop.word

2020-02-06 07:50发布

问题:

I've been trying to figure out how to insert 2 different formats into the same paragraph using interop.word in c# like this:

hello planet earth here's what I want to do

回答1:

Assuming you have your document defined as oDoc, the following code should get you the desired result:

Word.Paragraph oPara = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara.Range.Text = "hello planet earth here's what I want to do";
object oStart = oPara.Range.Start + 13;
object oEnd = oPara.Range.Start + 18;

Word.Range rBold = oDoc.Range(ref oStart, ref oEnd);
rBold.Bold = 1;


回答2:

I had to modify Dennis' answer a little to get it to work for me.

What I'm doing it totally automated, so I have to only work with variables.

private void InsertMultiFormatParagraph(string text, int size, int spaceAfter = 10) {
    var para = docWord.Content.Paragraphs.Add(ref objMissing);

    para.Range.Text        = text;
    // Explicitly set this to "not bold"
    para.Range.Font.Bold   = 0;
    para.Range.Font.Size   = size;
    para.Format.SpaceAfter = spaceAfter;

    var start = para.Range.Start;
    var end   = para.Range.Start + text.IndexOf(":");

    var rngBold = docWord.Range(ref objStart, ref objEnd);
    rngBold.Bold = 1;

    para.Range.InsertParagraphAfter();
}

The main difference that made me want to make this post was that the Paragraph should be inserted AFTER the font is changed. My initial thought was to insert it after setting the SpaceAfter property, but then the objStart and objEnd values were tossing "OutOfRange" Exceptions. It was a little counter-intuitive, so I wanted to make sure everyone knew.



回答3:

The following code seemed to work the best for me when formatting a particular selection within a paragraph. Using Word's built in "find" function to make a selection, then formatting only the selected text. This approach would only work well if the text to select is a unique string within the document. But for most situations I have run across, this seems to work.

        oWord.Selection.Find.Text = Variable_Containing_Text_to_Select;
        oWord.Selection.Find.Execute();
        oWord.Selection.Font.Bold = 1;

If the string of text that you are dropping into a paragraph comes from a database, a switch statement, or some other method, then I just drop that text into a variable string and use it to execute the find function.

Hope this helps someone!



回答4:

I know this post is old, but it came out in almost all my searches. The answer below is in case someone, like me, wants to do this for more than one word in a sentence. In this case, I loop through a string array of variables that contain strings and change that text to bold--modifing @joshman1019

string[] makeBold = new string[4] {a, b, c, d};

foreach (string s in makeBold)
{
   wApp.Selection.Find.Text = s; //changes with each iteration
   wApp.Selection.Find.Execute(); 
   wApp.Selection.Font.Bold = 1;
   wApp.Selection.Collapse(); //used to 'clear' the selection
   wApp.Selection.Find.ClearFormatting();
}

So, each string represented by the variable will be bold. So if a = "hello world", then Hello World is made bold in the Word doc. Hope it saves someone some time.



回答5:

I know this is an old thread, but I thought I'd post here anyway for those that come across it via Google (like I did). I got most of the way to a solution with krillgar's approach, but I had trouble because some of my text contains newlines. Accordingly, this modification worked best for me:

private void WriteText(string text)
    {
        var para = doc.Content.Paragraphs.Add();
        var start = para.Range.Start;
        var end = para.Range.Start + text.IndexOf(":");
        para.Range.Text = text;
        para.Range.Font.Bold = 0;
        para.Range.InsertParagraphAfter();

        if(text.Contains(":")){
            var rngBold = doc.Range(start, end);
            rngBold.Bold = 1;
        }
    }

The key difference is that I calculate start and end earlier in the function. I can't quite put my finger on it, but I think if your new text has newlines in it, the later calculation of start/end messes something up.

And obviously my solution is intended for text with the format:

Label: Data

where Label is to be bolded.



回答6:

Consider usage of Range.Collapse eventually with Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd as parameter. That would allow next text to have formatting different than previous text (and next text formatting will not affect formatting of previous one).