PDA

View Full Version : Problem finding/not-finding the Default Paragraph Font



johndavidson
12-20-2013, 09:04 PM
The routine below exemplifies a problem I’m having in Word 2003. If you search using VBA for a font (let’s say Arial) that happens to be the Default Paragraph Font, then the find/replace dialogue box shows the find formatting as:

'Font: (Default) Arial' rather than 'Font: Arial'

The two produce different results when searching and can cause problems in the rest of the my code under certain circumstances. I need the code to search for 'Font: Arial', not 'Font: (Default) Arial'.

I thought that if I checked the font name first and temporarily changed the default paragraph font to something else if the font name to be searched was the same as the default paragraph font, that then the problem would go away. But it doesn't. The F/R dialogue box after the search still shows the find formatting as: 'Font: (Default) Arial', which is what it searches for. I want it to search for 'Font: Arial', which can be set manually. I can't see how to do it in VBA. It always substitutes 'Font: (Default) Arial'

Does anyone know how to search in VBA for a font that happens to be the default paragraph font, without it being flagged as such? There is some online mention of this issue but apart from one suggestion to try .ClearFormatting, I could find no solutions.

If you run the code below, you will find that the FR dialogue box, when examined manually shows the formatting to be 'Font: (Default) Arial', even though at that point the default paragraph font has been set to the font 'Chiller'. All suggestions gratefully accepted.


Sub defFontTest()
Dim fontName As String
Dim defaultFont As String

fontName = "Arial"
If fontName = ActiveDocument.Styles(wdStyleDefaultParagraphFont).Font.Name Then
Selection.Find.ClearFormatting ' An online source suggested this would resolve the issue, but it doesn't work
defaultFont = ActiveDocument.Styles(wdStyleDefaultParagraphFont).Font.Name
ActiveDocument.Styles(wdStyleDefaultParagraphFont).Font.Name = "Chiller"
End If

With Selection.Find
.ClearFormatting
.Font.Name = fontName
.text = ""
.Replacement.text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
ActiveDocument.Styles(wdStyleDefaultParagraphFont).Font.Name = "Arial"
End Sub

fumei
12-21-2013, 07:00 AM
and temporarily changed the default paragraph font to something else
and

even though at that point the default paragraph font has been set to the font 'Chiller'

This is quite incorrect. You have not changed the font in anyway at all. You just changed the NAME. If your default font is Times New Roman and you run:

ActiveDocument.Styles(wdStyleDefaultParagraphFont).Font.Name = "Chiller"
or

ActiveDocument.Styles(wdStyleDefaultParagraphFont).Font.Name = "Arial"
or

ActiveDocument.Styles(wdStyleDefaultParagraphFont).Font.Name = "Yabba Dabba Doo"
or

ActiveDocument.Styles(wdStyleDefaultParagraphFont).Font.Name = "Verdana"
or anything else...the FONT is still Times New Roman.

johndavidson
12-21-2013, 08:26 AM
Umm ... Have you tried it? You do actually get the Chiller font face as the Default Paragraph Font when you run ActiveDocument.Styles(wdStyleDefaultParagraphFont).Font.Name = "Chiller"

johndavidson
12-21-2013, 08:29 AM
X

fumei
12-21-2013, 09:03 AM
Umm ... Have you tried it? You do actually get the Chiller font face as the Default Paragraph Font when you run ActiveDocument.Styles(wdStyleDefaultParagraphFont).Font.Name = "Chiller"
Yes, umm, I have tried it, and the font itself does not change. All I am saying is that the name is changed, not any font characteristics. At least for me. Just the name. DefaultParagraphFont is the font used by Normal.

Sub WhatISDefault()
MsgBox ActiveDocument.Styles(wdStyleDefaultParagraphFont).Font.Name
' returns Times New Roman

MsgBox ActiveDocument.Styles("Normal").Font.Name
' also returns Times New Roman

ActiveDocument.Styles("Normal").Font.Name = "SomeNonExistingStyleNameTsldkhadlakhdfahdlad"


MsgBox ActiveDocument.Styles("Normal").Font.Name
' returns SomeNonExistingStyleNameTsldkhadlakhdfahdlad

End Sub


SomeNonExistingStyleNameTsldkhadlakhdfahdlad is a non-existing "Font". If you use Normal the font actually used is...wait for it...Times New Roman. Not SomeNonExistingStyleNameTsldkhadlakhdfahdlad, which does not exist. But the NAME of Default Paragraph Font is SomeNonExistingStyleNameTsldkhadlakhdfahdlad.

All I am saying is that (for me anyway) the name is not the same as the thing. Yes, you can search for the Name.

Perhaps there may be an issue for you from the inheritance of font by style. If you change Default (used by Normal), then every other style that is based on Normal - which by default is the most common - (and that style does not have overriding explicit font changes) will return a positive by search.

johndavidson
12-21-2013, 08:06 PM
.

johndavidson
12-21-2013, 08:33 PM
OK. So my 'solution' doesn't work!

What I am trying to do is to create a macro that tags all fonts regardless of whether they are Normal + FontName or are in some other style. I already have routines to tag font attributes and font styles, now I want to tag selected fonts. The purpose is in cleaning up OCR'ed documents that have a wealth of extraneous styles etc. So I tag the things I really want, clear the rest, and then reset/untag the attributes, fonts and styles. It's all working fine except for some 'oddities' in the font tagging, which seem to arise from the way fonts are handled in Word (2003).

When I search, using VBA code, for a fontname that happens to be the default font (let’s say Arial), then the find/replace dialogue box (when investigated) shows the find formatting to be: 'Font: (Default) Arial' rather than 'Font: Arial'

If I search manually for 'Font: Arial' in Word I find what I want. 'Font: (Default) Arial', which is created when the search is set up in VBA, behaves differently.

So how do I persuade the VBA code find to search for 'Font: Arial' rather than 'Font: (Default) Arial'.

Hope I'm being clear.

fumei
12-21-2013, 09:07 PM
I am afraid I can not seem to duplicate this. I do not get the (Default)Name situation. But then I do not have any OCR'd documents to play with.

I am still a bit confused. Are you saying if you explicitly set up a VBA search for Arial

.Font.Name = Arial (NOT something from DefaultParagraph)

it does not find it?

johndavidson
01-18-2014, 09:45 PM
Yes. The code used is essentially the same as in my original quote. I'm presently on vacation and will have a deeper look on my return after Jan 24th.

Thanks ...