none
Searching fonts in VBA in Word

    Question

  • Part of an upgrade from a Word 2003 template to 2010 is a macro that does a bunch of search and replaces for certain fonts and characters. I discovered that sometimes the characters weren't getting found, yet the same character, even within the same document DOES get found, all that particular character with that same font get found when I do the search manually. It seems to have something to do with how the character was added to the document, or perhaps more specifically how the font gets on that character.

    The font change seems to be the problem. If I clear the formatting by selecting the "Clear Formatting" from the styles menu, hitting Ctrl + Shift + N or applying the "Default Paragraph Font" through VBA, then reassign the font that was originally there, the characters get found. This, however, isn't a practical solution because applying the Default Paragraph Font character style, of course, removes any bold, italic, superscript, etc.

    Stopping the macro I can see that this line:

    Selection.find.found.font.name="Arial"

    actually sets the font to "(Default) Arial" which, I think, is the problem. If I clear the formatting manually on the search and select the font the find field doesn't show the "(Default)."

    While I'm working in 2003 the same thing seems to happen in 2010, not sure yet about 2013.

    Does anyone know what that "(Default)" signifies? Is there a way initiate a search in VBA that DOESN'T cause that?

    Many thanks,
    Ken

    Thursday, July 04, 2013 12:45 AM

Answers

  • Hello,

    The default font is not "(Default) Arial" in Word 2010 on my machine, but I found a similar issue:

    The default font of my Word 2010 is "Calibri (Body)". if I clear formatting with the range, the range's font is set to "Calibri (Body)". Then I search for the font of "Calibri", no item gets found. Even it returns "Calibri" for "Calibri (Body)" if I use VBA code to return the font's name by Range.Font.Name. But when you use Advanced Find, "Calibri (Body)" and "Calibri" seem not regarded as the same. So, I think "(Default) Arial" and "Arial" are also not regarded as the same in your Word 2010.

    This issue can be reproduced in Word 2013 as well.

    I will try to internally report this issue. I will update in this thread if there is any update.

    Thanks for sharing this information.

    Regards,

    ---------------------------------------

    Updated:

    I have got some information from senior engineers:

    "I looked into this issue on the Word side and have determined this behavior to be By Design (not a bug). Word has behaved in such a fashion since Word 12 (Word 2007). Body font types (regardless of their font face) and non-body fonts are treated as being distinctly different in Word. So when searching using Find/Replace you must be explicit about what you are actually looking for. If you want to search for the body text, you simply select (Body) in the font dropdown. Searching for “Calibri” will look for any direct-formatting or styled text specifying Calibri as a non-body font."


    Damon Zheng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, July 04, 2013 12:42 PM

All replies

  • Hello,

    The default font is not "(Default) Arial" in Word 2010 on my machine, but I found a similar issue:

    The default font of my Word 2010 is "Calibri (Body)". if I clear formatting with the range, the range's font is set to "Calibri (Body)". Then I search for the font of "Calibri", no item gets found. Even it returns "Calibri" for "Calibri (Body)" if I use VBA code to return the font's name by Range.Font.Name. But when you use Advanced Find, "Calibri (Body)" and "Calibri" seem not regarded as the same. So, I think "(Default) Arial" and "Arial" are also not regarded as the same in your Word 2010.

    This issue can be reproduced in Word 2013 as well.

    I will try to internally report this issue. I will update in this thread if there is any update.

    Thanks for sharing this information.

    Regards,

    ---------------------------------------

    Updated:

    I have got some information from senior engineers:

    "I looked into this issue on the Word side and have determined this behavior to be By Design (not a bug). Word has behaved in such a fashion since Word 12 (Word 2007). Body font types (regardless of their font face) and non-body fonts are treated as being distinctly different in Word. So when searching using Find/Replace you must be explicit about what you are actually looking for. If you want to search for the body text, you simply select (Body) in the font dropdown. Searching for “Calibri” will look for any direct-formatting or styled text specifying Calibri as a non-body font."


    Damon Zheng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, July 04, 2013 12:42 PM
  • Do you know what the "(Default)" in the find field indicates?

    I should also say that what ever aspect of the font change is causing the text to NOT be found seems to get set at random times. For instance, I have text I was using for testing this and had just a few characters within that text that was not getting found. I eventually made that text an AutoText entry. Now, however, when I insert that AutoText entry there are ADDITIONAL characters that are not getting found.

    I've not been able to pinpoint what might have changed made that text change from being getting found to NOT getting found.

    Thanks for answering, hopefully there's at least a work-around we can find.

    Friday, July 05, 2013 2:11 PM
  • I found no answers as to why any of this is occurring but I DID find that clearing the formatting with

    Selection.Find.ClearFormatting

    before setting the find font does NOT cause the "(Default)" to go in before the font name on the find dialog. This, as I mentioned in a previous post, makes it so that all instances of the font gets found.

    Well, at least all the instances of the font changes I'm dealing with. As I can't determine what makes one instance of a font get found when the "(Default)" is in there and one NOT get found, I can't possibly say whether there might be other font changes with whatever underlying/invisible "attributes" (for lack of a better word) there might be.

    Friday, July 05, 2013 5:48 PM
  • Any update on this issue then?
    Tuesday, July 16, 2013 2:34 PM
  • Hello Mike,

    I've not heard anything from Microsoft beyond the reply in this post. I've run the macro I was working on when I came across this on several projects with at least 10 files each and everything seemed to work with the .ClearFormatting  command in there.

    Are you having trouble with something that wasn't fixed by the using the .ClearFormatting?

    Thanks for checking in,
    Ken

    Tuesday, July 16, 2013 2:52 PM
  • Hello Ken,

    I have got some information from senior engineer, and updated my first reply.

    Please have a look at it.

    Thanks,


    Damon Zheng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, July 18, 2013 7:45 AM