Log in

View Full Version : [SLEEPER:] Finding "Theme" fonts using VBA



gmaxey
01-17-2013, 11:26 AM
I have a theme applied to my normal template that has the following theme fonts:

Cambira (Headings) and Times New Roman (Body)

When I use the built-in Find utility, I leave the "Find what:" field empty and I then click "More>Format>Font" and select the +Body as the font to find. This results in "+Body (Times New Roman)" appearing under the find what field. If I click "Find Next" the next instance of the text so formatted is selected.

Attempting the same thing with VBA:


Sub ScratchMacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Text = ""
.Font.Name = "+Body (" & ActiveDocument.DocumentTheme.ThemeFontScheme.MinorFont.Item(1).Name & ")"
.Format = True
.Forward = True
.Wrap = wdFindStop
While .Execute
MsgBox oRng.Text
oRng.Collapse wdCollapseEnd
Wend
End With
End Sub

The text is not found. What I am missing here?

macropod
01-29-2013, 12:30 AM
Hi Greg,

I don't use themes, so I can't help you directly. However, have you checked what the macro record captures when you do it through the UI?

gmaxey
01-29-2013, 07:39 AM
Paul,

It provides nothing helpful. Whatever is used as .Font.Name is not recorded. This is really weird. If I type a sentence peppered with +Body theme text, plain Times New Roman text and plain Arial text. I can select text of each type and run:

Debug.Print Selection.Font.Name in the immediate window. The result returned is:

Times New Roman
Times New Roman
Arial

Now is the macro below, regardless of what I have as:

.Font.Name = "X"

whether "X" = what is hown, or "Time New Roman" or "Arial", the result of the Debug.Print line nothing returned.

However, using "Times New Roman" or "Arial" will find the text with plain Times New Roman or the text with "Arial" applied. It just won't find the theme text regardless.

There must be something in the find object that separates and excludes text formatted with +Body "Times New Roman" and plain "Times New Roman" but I can't figure out what it is.


Thanks.


Sub ScratchMacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Text = ""
.Format = True
.Font.Name = "+Body (" & ActiveDocument.DocumentTheme.ThemeFontScheme.MinorFont.Item(1).Name & ")"
Debug.Print .Font.Name
.Forward = True
.Wrap = wdFindStop
While .Execute
MsgBox oRng.Text
oRng.Collapse wdCollapseEnd
Wend
End With
End Sub

fumei
01-30-2013, 04:58 PM
Are you sure it is MinorFont?

gmaxey
02-01-2013, 10:07 AM
Gerry,

Yes I'm sure.