Word Macros: How to Find and Select A Block of Text Which Has a Particular Font in a Word Document

Question

I'm trying to write a macro in Microsoft Word, which finds and selects a block of text which has a particular font.

E.g. suppose I want to find and select the next block of text which is in 'Courier New' font.

Note: Finding the block is straight-forward, but how do I create a macro to select the whole block which has that font.


Show source
| vba   | macros   | search   | ms-word   2016-11-24 09:11 3 Answers

Answers ( 3 )

  1. 2016-11-24 09:11

    Not sure what you got so far, but how about this:

    Dim p As Paragraph
    For Each p In ActiveDocument.Paragraphs    
        If p.Range.Font.Name = "Courier New" Then p.Range.Select
        ' do whatever you gotta do here
    Next p
    

    Notice: in this case, don't use the Paragraph.Style.Font.Name-property since it's possible that a predefined style was initially used, but the font was changed afterwards without updating the style.

    Hope this helps.

  2. 2016-11-24 13:11

    Take a look at this solution where they use highlighting text and search again for the highlighted text in the selection:

    http://answers.microsoft.com/en-us/msoffice/forum/msoffice_word-mso_mac/finding-text-and-marking-text-that-does-not-equal/4a688a88-8df5-4d42-9713-598accb09f98

    It does what you need but is in fact doing exactly the opposite of what you are looking for. By turning it around a bit you should be able to find the fonts that do equal the ones you want. It needs some fiddling but you can always post new questions around the issues that you run into.

  3. 2016-11-30 21:11

    The solution is easy. What you do is that you write the part of the macro that searches for courier new font; then change the range to extend. Then in your macro, you search for times new roman font. This leaves the block that is in courier new as selected.

◀ Go back