Find words with more than one capital letter in word/VBA
find capital letters in word
find uppercase words in excel
small capital letters word
small caps in word
how to change capital letters to lowercase in word mac
small caps word mac
how to turn off all caps in word
I have a piece of VBA code that uses Find to find all the acronyms in a document. It does this by searching for all words consisting of capital letters that are 2 or more characters long using...
<[A-Z]{2,}>
The problem with this is it doesn't pick up all the acronyms, such as CoP, W3C, DVDs and CD-ROM. It picks up hyphenated acronyms in two parts which are not ideal but allowable as the list is checked by a user. I can also pick up words that end with an "s" or other characters by not searching to the end of the word using...
<[A-Z]{2,}
But this doesn't count any non-upper case character as part of the word it finds.
Is there an expression that would allow me to search for words with two or more capital letters in any location and find the whole word?
I don't think it's possible to 'search for words with two or more capital letters in any location and find the whole word' except in combination with macro code. Since you're using a macro, anyway, here's an approach that worked for me using the following sample text
CoP, this That and AnoTher thing W3C, DVDs and CD-ROM
and this wildcard combination (note that the list separator in my Windows configuration is ;
, for other regions a ,
may be required).
<[A-Z][0-9A-Z\-a-z]{1;10}>
The following function checks whether the second or any later letter in the "found" range is capitalized and returns a boolean to the calling procedure. It loops through the characters in the given Range
, checking the ASCII value. As soon as one is found, the loop exits.
Function ContainsMoreThanOneUpperCase(rng As Word.Range) As Boolean Dim nrChars As Long, i As Long Dim char As String Dim HasUpperCase HasUpperCase = False nrChars = rng.Characters.Count For i = 2 To nrChars char = rng.Characters(i).text If Asc(char) >= 65 And Asc(char) <= 90 Then 'It's an uppercase letter HasUpperCase = True Exit For End If Next ContainsMoreThanOneUpperCase = HasUpperCase End Function
An example for using it:
Sub FindAcronyms() Dim rngFind As Word.Range Dim bFound As Boolean Set rngFind = ActiveDocument.content With rngFind.Find .text = "<[A-Z][0-9A-Z\-a-z]{1;10}>" .MatchWildcards = True .Forward = True .wrap = wdFindStop bFound = .Execute Do While bFound If bFound And ContainsMoreThanOneUpperCase(rngFind) Then Debug.Print rngFind.text rngFind.HighlightColorIndex = wdBrightGreen End If rngFind.Collapse wdCollapseEnd bFound = .Execute Loop End With End Sub
2 Quick Ways to Find All Words with Initial or All Letters Capitalized in, The reasons to find words with initial or all letters capitalized are as follows: to recheck the correct spell and explanation of acronyms which are in all caps most time. Click “More” to bring out more options. But its setback is that it finds only words over than one letter. Then press “Alt+ F11” to trigger the VBA editor. I have a need (I wont bother you with why) to find the next occurrence of three or more adjacent capital letters in the text of a Word document. I know I can loop through the text character by character but that approach is unbelievably slow. Is there a better method? I am using Office 2007.
You can't do this in one pass of Find/Replace. You also have to make some allowances for what the Word application considers a Word and then where the acronym is located in the sentence or paragraph.
The following code should provide an idea for how you might do it with a combination of Wildcard searching and then additional VBA string manipulation.
It is setup to deal with words that start with capital letters, you will need to carry it further and add code and wildcard search criteria for words that start with lowercase letters if you expect to have any of those.
Sub FindAcronynms() Dim rng As word.Range Set rng = ActiveDocument.Content With rng.Find .ClearFormatting .Format = False .Forward = True .MatchWildcards = True .Text = "<[A-Z]{1,}[a-z][A-Z]>" .Wrap = wdFindStop .Execute Do While .found MoveEndOfString rng rng.HighlightColorIndex = wdTeal rng.Collapse wdCollapseEnd .Execute Loop End With Set rng = ActiveDocument.Content With rng.Find .ClearFormatting .Format = False .Forward = True .MatchWildcards = True .Text = "[A-Z]{1,5}[0-9][A-Z]{1,5}" .Wrap = wdFindStop .Execute Do While .found MoveEndOfString rng rng.HighlightColorIndex = wdTeal rng.Collapse wdCollapseEnd .Execute Loop End With Set rng = ActiveDocument.Content With rng.Find .ClearFormatting .Format = False .Forward = True .MatchWildcards = True .Text = "<[A-Z]{2,}>" .Wrap = wdFindStop .Execute Do While .found MoveEndOfString rng rng.HighlightColorIndex = wdTeal rng.Collapse wdCollapseEnd .Execute Loop End With MsgBox "Action Complete", vbExclamation, "Custom Find" End Sub Private Function MoveEndOfString(ByRef rng As word.Range) rng.MoveEnd wdCharacter, 1 Select Case Asc(rng.Characters.Last) Case Is <= 32 rng.MoveEnd wdCharacter, -1 Case 45 rng.MoveEnd wdCharacter, 1 rng.MoveEnd wdWord, 1 If Asc(rng.Characters.Last) = 32 Then 'required because move above includes 'trailing space rng.MoveEnd wdCharacter, -1 End If End Select End Function
VBA Developer's Handbook, The most important consideration is to choose names that represent the functionality of the class and its members. It's common practice when writing code to prefix names with letters that indicate Interface class names are often prefixed with a capital I to indicate that they name is made up of more than one word (for. Extract words starting with capital letter from text strings with User Defined Function. If you need to extract the words which start with a capital letter from text strings, the following VBA code may help you. 1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window. 2.
You might use something like:
Sub Demo() Application.ScreenUpdating = False Options.DefaultHighlightColorIndex = wdPink With ActiveDocument.Range With .Find .ClearFormatting .Replacement.ClearFormatting .Replacement.Highlight = True .Forward = True .Format = True .Wrap = wdFindContinue .MatchWildcards = True .Text = "<[A-Z][A-Z0-9/-]{1,}" .Replacement.Text = "^&" .Execute Replace:=wdReplaceAll .Text = "<[A-Z][A-Za-z0-9/-]@[A-Z]" .Replacement.Text = "^&" .Execute Replace:=wdReplaceAll End With End With Application.ScreenUpdating = True End Sub
Word 2007 Document Automation with VBA and VSTO, Our recommendation is that you use all capital letters for the words to be Sincerely yours, Here, the key benefit of the Find and Replace comes from being able to avoid typing the intended [PREFIXNAME] and [LASTNAME] more than once. Firstly, click the arrow button next to “Find” command under “Home” tab. Then click “Advanced Find” to open the “Find and Replace” box. Next place cursor at the “Find what” text box. Enter “ [A-Z] {2,} ” to find all words with all letter capitalized.
Find uppercase text, of three or more adjacent capital letters in the text of a Word document. I know I by a space, then you CAN search for "words", rather than single characters. One small question, how does VBA know the search string you You should be able to achieve the same with: [A-Z0-9_-]{2,} or, if your acronyms always start with a letter: [A-Z][A-Z0-9_-]{1,} As for the numbers issue, if you mean it doesn't find complete numbers with thousands separators & decimal points, that's because neither of those is provided for in your Find expression.
Access 2007 Programming by Example with VBA, XML, and ASP, To find out what a particular VBA instruction (statement) has returned, you must tell If you want the name of the variable to include more than one word, use the is comprised of more than one word, most programmers capitalize the first letter of Reserved Words Can't Be Used for Variable Names You can use any label Find words with more than one capital letter in word/VBA. I have a piece of VBA code that uses Find to find all the acronyms in a document. It does this by searching for all words consisting of capital letters that are 2 or more characters long using
Search for cell with words containing uppercase and return true or , Search for cell with words containing uppercase and return true or false I know how to do it if all words are in capitals but I'm stuck if just one of the words contains capitals. I want a TRUE if any one word with more than 1 character is detected in cells where there VBA would do this quickly buthmm. You can use Wildcards to select the captial letter at at start of a word, the code for the Find box is <[A-Z] To use Wildcards in the Find/Replace dialog, click on the More button and check the appropraite box on the left of the dialog. This code will find all captials at the start of any word - that includes at the beginning of sentences.
Comments
- You won't be able to do that with a single wildcard pattern. MS "regex" (which is not "regular", in fact) does not support optional patterns. You may use a couple of patterns to find exact patterns you are looking for though.
\b(?:[A-Z0-9]+[a-z\-]?){2,}\b
- @WiktorStribiżew thanks, this seems to be the case. Easiest way I can see is find all entries starting with a capital letter (
<[A-Z]*>
), then test each word found against.+[A-Z]
. This works fine (assuming all acronyms start with a capital letter), but requires additional references which make it more difficult to share. - Thanks, this worked great and doesn't require additional references (which apparently using comparing to regular expressions in VBA does). The only thing I would change is the initial find expression can be
"<[A-Z]*>"
, which is a little neater and doesn't have a character limit. - Glad the approach works for you :-) The problem with trying to use Regular Expressions in Word is that you lose the formatting. If, of course, you use it to compare the "found" term, then it would work, yes...
- Thanks, it does seem to be that unfortunatly it can't be done in one operation. This code doesn't seem to pick out words that include lower case letters (although it did highlight punctuation after them) and also seems to pickup punctuation after words it finds.
- I think you need to include some explanation with this... It's not obvious at all how this solves the problem.