Find words with more than one capital letter in word/VBA










4















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?










share|improve this question



















  • 2





    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.

    – Wiktor Stribiżew
    Nov 14 '18 at 12:34











  • b(?:[A-Z0-9]+[a-z-]?)2,b

    – omegastripes
    Nov 14 '18 at 13:33











  • @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.

    – Kagekiba
    Nov 14 '18 at 16:11
















4















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?










share|improve this question



















  • 2





    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.

    – Wiktor Stribiżew
    Nov 14 '18 at 12:34











  • b(?:[A-Z0-9]+[a-z-]?)2,b

    – omegastripes
    Nov 14 '18 at 13:33











  • @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.

    – Kagekiba
    Nov 14 '18 at 16:11














4












4








4


0






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?










share|improve this question
















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?







regex vba ms-word find acronym






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 12:38







Kagekiba

















asked Nov 14 '18 at 12:30









KagekibaKagekiba

357




357







  • 2





    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.

    – Wiktor Stribiżew
    Nov 14 '18 at 12:34











  • b(?:[A-Z0-9]+[a-z-]?)2,b

    – omegastripes
    Nov 14 '18 at 13:33











  • @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.

    – Kagekiba
    Nov 14 '18 at 16:11













  • 2





    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.

    – Wiktor Stribiżew
    Nov 14 '18 at 12:34











  • b(?:[A-Z0-9]+[a-z-]?)2,b

    – omegastripes
    Nov 14 '18 at 13:33











  • @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.

    – Kagekiba
    Nov 14 '18 at 16:11








2




2





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.

– Wiktor Stribiżew
Nov 14 '18 at 12:34





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.

– Wiktor Stribiżew
Nov 14 '18 at 12:34













b(?:[A-Z0-9]+[a-z-]?)2,b

– omegastripes
Nov 14 '18 at 13:33





b(?:[A-Z0-9]+[a-z-]?)2,b

– omegastripes
Nov 14 '18 at 13:33













@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.

– Kagekiba
Nov 14 '18 at 16:11






@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.

– Kagekiba
Nov 14 '18 at 16:11













3 Answers
3






active

oldest

votes


















2














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





share|improve this answer























  • 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.

    – Kagekiba
    Nov 14 '18 at 15:38











  • 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...

    – Cindy Meister
    Nov 14 '18 at 16:00


















0














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





share|improve this answer























  • 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.

    – Kagekiba
    Nov 14 '18 at 16:17



















-1














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





share|improve this answer


















  • 1





    I think you need to include some explanation with this... It's not obvious at all how this solves the problem.

    – Cindy Meister
    Nov 15 '18 at 13:04










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300311%2ffind-words-with-more-than-one-capital-letter-in-word-vba%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














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





share|improve this answer























  • 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.

    – Kagekiba
    Nov 14 '18 at 15:38











  • 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...

    – Cindy Meister
    Nov 14 '18 at 16:00















2














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





share|improve this answer























  • 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.

    – Kagekiba
    Nov 14 '18 at 15:38











  • 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...

    – Cindy Meister
    Nov 14 '18 at 16:00













2












2








2







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





share|improve this answer













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






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 14:03









Cindy MeisterCindy Meister

15.3k102235




15.3k102235












  • 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.

    – Kagekiba
    Nov 14 '18 at 15:38











  • 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...

    – Cindy Meister
    Nov 14 '18 at 16:00

















  • 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.

    – Kagekiba
    Nov 14 '18 at 15:38











  • 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...

    – Cindy Meister
    Nov 14 '18 at 16:00
















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.

– Kagekiba
Nov 14 '18 at 15:38





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.

– Kagekiba
Nov 14 '18 at 15:38













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

– Cindy Meister
Nov 14 '18 at 16:00





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

– Cindy Meister
Nov 14 '18 at 16:00













0














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





share|improve this answer























  • 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.

    – Kagekiba
    Nov 14 '18 at 16:17
















0














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





share|improve this answer























  • 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.

    – Kagekiba
    Nov 14 '18 at 16:17














0












0








0







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





share|improve this answer













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






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 15:29









Rich MichaelsRich Michaels

536248




536248












  • 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.

    – Kagekiba
    Nov 14 '18 at 16:17


















  • 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.

    – Kagekiba
    Nov 14 '18 at 16:17

















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.

– Kagekiba
Nov 14 '18 at 16:17






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.

– Kagekiba
Nov 14 '18 at 16:17












-1














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





share|improve this answer


















  • 1





    I think you need to include some explanation with this... It's not obvious at all how this solves the problem.

    – Cindy Meister
    Nov 15 '18 at 13:04















-1














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





share|improve this answer


















  • 1





    I think you need to include some explanation with this... It's not obvious at all how this solves the problem.

    – Cindy Meister
    Nov 15 '18 at 13:04













-1












-1








-1







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





share|improve this answer













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






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 22:28









macropodmacropod

2,616239




2,616239







  • 1





    I think you need to include some explanation with this... It's not obvious at all how this solves the problem.

    – Cindy Meister
    Nov 15 '18 at 13:04












  • 1





    I think you need to include some explanation with this... It's not obvious at all how this solves the problem.

    – Cindy Meister
    Nov 15 '18 at 13:04







1




1





I think you need to include some explanation with this... It's not obvious at all how this solves the problem.

– Cindy Meister
Nov 15 '18 at 13:04





I think you need to include some explanation with this... It's not obvious at all how this solves the problem.

– Cindy Meister
Nov 15 '18 at 13:04

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300311%2ffind-words-with-more-than-one-capital-letter-in-word-vba%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto