none
Selecting text by font RRS feed

  • Question

  • Is it possible to select all the the text that matches a certain font? I cannot seem to find the function in the Microsoft.Office.Interop.Word namespace.

    In addition, it is possible to see what type of font some text in the document is formatted with (say, the first line)?

    Monday, January 2, 2012 12:03 AM

Answers

  • Hi soando

    So you don't really need to select all instances formatted with a particular font. All you need to do is extract all text that's formatted with it?

    In that case, using the Word API I'd work with the Range.Find method. Something like (to give you start with the syntax):

    string foundText;
    object oTrue = true;
    object oFalse = false;
    object missing = System.Type.Missing;
    object findStop = Word.WdFindWrap.wdFindStop;
    Word.Range rng = DocObject.Content;
    rng.Find.Font.Name = "Arial";
    rnf.Find.Font.Size = 12;
    bool found = false;
    do
    {
    found = rng.Find.Execute(ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref findStop, ref oTrue, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
    //if successful, Range is now the found segment:
    if (found)
    {
    Word.Range rngFound = rngFound.Range;
    foundText = rng.Text;
    //Now do something with the text, then...
    //Reset the search range to start after the found font, otherwise it will loop
    rng.Start = rngFound.End;
    rng.End = DocObject.Content.End;
    }
    } while found


    Cindy Meister, VSTO/Word MVP
    Monday, January 2, 2012 2:15 PM
    Moderator

All replies

  • Hi soando

    Which version of Word is this and what programming language are you using?

    What do you intend to do with this selection?


    Cindy Meister, VSTO/Word MVP
    Monday, January 2, 2012 8:08 AM
    Moderator
  • Word 2010, C#, run it though a function and then place it in a csv.
    Monday, January 2, 2012 1:14 PM
  • Hi soando

    So you don't really need to select all instances formatted with a particular font. All you need to do is extract all text that's formatted with it?

    In that case, using the Word API I'd work with the Range.Find method. Something like (to give you start with the syntax):

    string foundText;
    object oTrue = true;
    object oFalse = false;
    object missing = System.Type.Missing;
    object findStop = Word.WdFindWrap.wdFindStop;
    Word.Range rng = DocObject.Content;
    rng.Find.Font.Name = "Arial";
    rnf.Find.Font.Size = 12;
    bool found = false;
    do
    {
    found = rng.Find.Execute(ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref findStop, ref oTrue, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
    //if successful, Range is now the found segment:
    if (found)
    {
    Word.Range rngFound = rngFound.Range;
    foundText = rng.Text;
    //Now do something with the text, then...
    //Reset the search range to start after the found font, otherwise it will loop
    rng.Start = rngFound.End;
    rng.End = DocObject.Content.End;
    }
    } while found


    Cindy Meister, VSTO/Word MVP
    Monday, January 2, 2012 2:15 PM
    Moderator