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
'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