#1
|
|||
|
|||
Searching font names
When I look at font names they sometimes have a prefix or suffix such as “Cambria (Heading)”, “(Body) Calibri” or “(Default) Times New Roman”.
I want to search for text with specific fonts in VBA but while I can search for the font name such as Calibri but it will not find text with the font “(Body) Calibri”, nor can I find it by setting sFontName = "(Body) Calibri". How can I find these fonts My code is: sFontName = "Calibri" With Selection.Find .ClearFormatting .Replacement.ClearFormatting .Replacement.Text = "" .Format = True .Text = "" .Font.Name = sFontName .Execute End With |
#2
|
|||
|
|||
Hi kae0088,
I don't know if this helps or not, but I did a very simple routine in order to return the font name of each paragraph in the document (code is below). For the first paragraph, I formatted "Calibri (Body)" and the second paragraph was just "Calibri". When I ran the code, the font returned was "Calibri" for both paragraphs. Perhaps, there is no difference? Roy Code:
Sub findme2() Dim oParagraph As Paragraph For Each oParagraph In ActiveDocument.Paragraphs MsgBox oParagraph.Range.Font.Name Next End Sub |
#3
|
|||
|
|||
Hi scienceguy
If you execute: Code:
sFontName = oParagraph.Range.Font.Name oParagraph.Range.Font.Name = sFontNamne There is an interaction with Office Find & Replace (CTRL+H). If you execute: Code:
Sub SetFontName() Selection.Find.ClearFormatting Selection.Find.Font.Name = "Calibri" Selection.Find.Execute End Sub It all doesn't help me with my task. |
#4
|
||||
|
||||
The font described by the theme's minor font (in your case Calibri (Body)) can be found by searching for a font by the name of "+Body"
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
Thanks. I am now getting somewhere. I can search for "+Headings" and "+Body" and determine the font from the found text.
|
#6
|
|||
|
|||
Having resolved the problem with (Body) and (Headings) suffixes there is still the problem of (Default) prefix as in “(Default) Times New Roman”.
If I run Code:
Sub FindFontName() Selection.Find.ClearFormatting Selection.Find.Font.Name = "Times New Roman" Selection.Find.Execute End Sub 1. In F&R click No Formatting then add the font “Times New Roman” 2. In F&R click Find Next to find at least one instance of the font. 3. Now the following VBA macro will find both “Times New Roman” and “(Default) Times New Roman”. Code:
Sub FindFontName() Selection.Find.Execute End Sub Any views on how I can resolve the issue of the (Default) prefix. |
#7
|
||||
|
||||
Can you post a sample document that shows this Default font? I don't recall ever seeing this and I haven't played with default fonts as I define typefaces for all my styles (usually with the theme fonts).
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#8
|
|||
|
|||
Run these steps in order.
1. Run the macro in the attached document and it will find the last paragraph but not the earlier paragraphs that supposedly have the same font. 2. Now open F&R and without changing anything click find. It will also find only the last paragraph. 3. In F&R with the cursor in Find What, click No Formatting then Format > Font > Times New Roman. F&R will now find all paragraphs. 4. In the macro comment out the first three statements leaving only If Not Selection.Find.Execute Then MsgBox "Not found" The macro will now find all paragraphs. 5. Select one of the earlier paragraphs and re-apply Times New Roman. Now run the steps again. |
#9
|
|||
|
|||
Did the file arrive unscathed?
I still can't resolve mt problem. |
#10
|
||||
|
||||
Yes, I downloaded the file and can verify that your described steps gave me the same result as you got.
I don't have a solution for this issue at the moment and haven't got a great deal of time to fiddle with it. I will try to get back onto looking at this on the weekend but someone else may be able to step with a suggestion in the meantime.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
Tags |
vba find and replace |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
General Question on List of Font Names | SamDsouza | Word VBA | 10 | 02-28-2021 07:24 PM |
Would like to shortern the coding for searching docx file names in specific directory | SamDsouza | Word VBA | 4 | 01-06-2021 07:27 AM |
Macro for Font Names and Sizes | apollox | Word VBA | 8 | 06-13-2018 06:00 AM |
Formula for searching names in one go | SilverChat | Excel | 2 | 05-07-2018 06:11 PM |
How to enter names in Resource Pool/names | pstein | Project | 1 | 03-26-2012 07:37 AM |