Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-11-2022, 10:43 PM
burniksapwet burniksapwet is offline VBA to select a specific font type Windows 10 VBA to select a specific font type Office 2007
Novice
VBA to select a specific font type
 
Join Date: Jan 2022
Posts: 6
burniksapwet is on a distinguished road
Default VBA to select a specific font type


I have a simple request to all the great people in this forum. I need a way to select all fonts in my document that is Times New Roman, font size of 12, uppercase. I know this may not be an ideal way to do this but this is really something that we need right now. Thank you so much. If I find a better way to select what we need I will post it. I have search google for something like this and have not found anyone that ask this type of question. Thank you.
Reply With Quote
  #2  
Old 01-12-2022, 12:11 AM
gmayor's Avatar
gmayor gmayor is offline VBA to select a specific font type Windows 10 VBA to select a specific font type Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

You cannot select non-contiguous items with VBA. You can select them sequentially, but then there would have to be some point in doing so. What do you want to do with them once found?
E.g. The following will highlight the texts so formatted:
Code:
Sub Macro1()
    With Selection.Find
        .ClearFormatting
        With .Font
            .Name = "Times New Roman"
            .Size = 12
            .Bold = True
        End With
        .Replacement.ClearFormatting

        Options.DefaultHighlightColorIndex = wdYellow
        .Replacement.Highlight = True

        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute Replace:=wdReplaceAll
    End With
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 01-12-2022, 09:03 AM
burniksapwet burniksapwet is offline VBA to select a specific font type Windows 10 VBA to select a specific font type Office 2007
Novice
VBA to select a specific font type
 
Join Date: Jan 2022
Posts: 6
burniksapwet is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
You cannot select non-contiguous items with VBA. You can select them sequentially, but then there would have to be some point in doing so. What do you want to do with them once found?
E.g. The following will highlight the texts so formatted:
Code:
Sub Macro1()
    With Selection.Find
        .ClearFormatting
        With .Font
            .Name = "Times New Roman"
            .Size = 12
            .Bold = True
        End With
        .Replacement.ClearFormatting

        Options.DefaultHighlightColorIndex = wdYellow
        .Replacement.Highlight = True

        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute Replace:=wdReplaceAll
    End With
End Sub
This is absolutely amazing. And yes I wanted it highlighted, I forgot to mention that. Thank you so much for looking into this and creating it for me. Much appreciated.
Reply With Quote
  #4  
Old 01-12-2022, 09:23 AM
burniksapwet burniksapwet is offline VBA to select a specific font type Windows 10 VBA to select a specific font type Office 2007
Novice
VBA to select a specific font type
 
Join Date: Jan 2022
Posts: 6
burniksapwet is on a distinguished road
Default

Quick question, is the uppercase section this statement?
.Bold = True
Reply With Quote
  #5  
Old 01-12-2022, 03:53 PM
Guessed's Avatar
Guessed Guessed is offline VBA to select a specific font type Windows 10 VBA to select a specific font type Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

No, it is not.

Uppercase can be searched for in two different ways according to how it occurs.
1. If the Uppercase is a font formatting attribute applied to regular (upper and lowercase) text then you could change .Bold = True to .AllCaps = True
2. If the text was actually typed only with capital letters then you would need a wildcard search for those specific characters along the following lines
Code:
Sub Seeker()
    Options.DefaultHighlightColorIndex = wdYellow
    With Selection.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      With .Font
        .Name = "Times New Roman"
        .Size = 12
      End With
      .Replacement.Highlight = True
      .Text = "[ABCDEFGHIJKLMNOPQRSTUVWXYZ]"
      .Replacement.Text = "^&"
      .Forward = True
      .Wrap = wdFindStop
      .MatchWildcards = True
      .Format = True
      .Execute Replace:=wdReplaceAll
    End With
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #6  
Old 01-12-2022, 09:59 PM
gmayor's Avatar
gmayor gmayor is offline VBA to select a specific font type Windows 10 VBA to select a specific font type Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

That's embarrassing. I appear to have misread 'upper case' as 'bold' I guess old age is taking its toll.

Andrew's correction will of course work, but I would be inclined also to include the space character. You can shorten the search string e.g.
Code:
.Text = "[A-Z ]"
See also Replace using wildcards
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #7  
Old 01-12-2022, 10:21 PM
Guessed's Avatar
Guessed Guessed is offline VBA to select a specific font type Windows 10 VBA to select a specific font type Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Good call Graham. Old age already biting deep here.

If you are going to include spaces then it will probably find spaces between lower case letters too. Assuming there are no double spaces in the document, you might ask the find to only return hits with at least two or three found characters
Code:
.Text = "[A-Z ]{2,}"
or
.Text = "[A-Z ]{3,}"
I would probably expand this to a minimum of three characters to avoid words that have an initial cap preceded by a space unless this is what the OP really wanted.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #8  
Old 01-12-2022, 11:55 PM
gmayor's Avatar
gmayor gmayor is offline VBA to select a specific font type Windows 10 VBA to select a specific font type Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Yes agreed re the spaces.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA to select a specific font type Merging two Word documents: 2nd document not maintaining original font type and font size Swarup Word 31 08-28-2018 06:55 PM
VBA to select a specific font type Macro that swaps out font type namedujour Word 3 08-01-2018 01:27 PM
Font Type changes with change in language animex Word 1 09-08-2017 05:23 PM
VBA to select a specific font type Type 1 font d4okeefe Word 3 02-09-2016 01:16 AM
Mysterious Font Type Symbol curiousUser Word 2 08-18-2014 05:10 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:11 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft