|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
How to return the full string (entire word) when searching with wildcards in word vba
I need to search a document for "words" that start with "32P". However, the numbering after the "32P" will be different (32P1, 32P2, 32P21, etc.). I can write a macro that finds all the "32P" occurrences by using a wildcard ("32P*"). However, I can't figure out how to get the full string (or full "word"). This is what I have so far:
Code:
Sub FindWords2() sResponse = "32P*" If sResponse <> "" Then Application.ScreenUpdating = False With Selection .HomeKey Unit:=wdStory With .Find .ClearFormatting .Text = sResponse .MatchWildcards = True ' Loop until Word can no longer ' find the search string and Do While .Execute MsgBox Selection.Range.Text 'This only returns "32P" and not the whole string Selection.MoveRight Loop End With End With Application.ScreenUpdating = True End If End Sub |
#2
|
||||
|
||||
Try
Code:
Sub FindWords2() Dim aRng As Range, sResponse As String sResponse = "<32P[0-9]{1,}" Set aRng = ActiveDocument.Range If sResponse <> "" Then With aRng.Find .ClearFormatting .Text = sResponse .MatchWildcards = True Do While .Execute ' Loop until Word can no longer find the search string MsgBox aRng.Text Loop End With Application.ScreenUpdating = True End If End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
Thank you, Andrew! This is great! I really appreciate your time and efforts! This is such a great service.
|
#4
|
|||
|
|||
Hi Andrew, I discovered today that the section numbering has changed. Instead of 32P123, it is now 3.2.P.1.2.3, where the (period + number) after the "P" may repeat one or more times. In addition, the "P" may be a "P" or "S". I was trying to adapt your regular expression to the change. I thought the revised expression would be "<3.2.[PS](\.[0-9])@" but the output only shows the first period and number after the letter (P or S). In other words, 3.2.P.1.2.3 only shows 3.2.P.1. Can you tell me what my regular expression should be, please? Sorry for the trouble!
|
#5
|
||||
|
||||
Change your Find expression to:
"<3.2.[PS].[.0-9]{1,}" Do note that this will pick up terminating periods (e.g. where your string is at the end of a sentence), so you'll need to code for that. See, for example: http://www.vbaexpress.com/forum/show...l=1#post388141
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#6
|
|||
|
|||
Thanks, Paul, but unfortunately, it's still showing the same issue (3.2.P.1.2.3 only shows 3.2.P.1).
|
#7
|
||||
|
||||
scienceguy
Perhaps you haven't implemented Paul's suggestion correctly. Note that there is a dot inside the bracket that says [.0-9]. If you didn't include that dot then you would be getting the result you describe.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#8
|
|||
|
|||
So sorry! Yes, you're right. I did mistype. Many thanks again!
|
#9
|
||||
|
||||
Cross-posted at: https://stackoverflow.com/questions/...vba-assistance
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#10
|
|||
|
|||
Hi All,
My application has evolved. Earlier I was searching for a string, such as 3.2.P.1.2.3, where the (period + number) after the "P" may repeat one or more times. Now, it can be: [2 or 3] + [period or no period] + [1 or 2 or 3] + [period or no period] + [P or S or A] + [period or no period] + [number], where the period (or no period) + number after the P,S or A may repeat any number of times. Some examples would be: 3.2.P.1 2.3.S.1.2 3.2.A.1.2.3.4.5 32P1 23S12 32A12345 I thought the expression would be "<[32].[231].[PSA].[0-9]{1,}"; however, this is only picking up strings that have periods in them. I thought the periods in this expression would represent any (or no) character. Would you help me with the expression, please? Thanks again, Roy |
#11
|
||||
|
||||
Change your Find expression to:
"<[23][0-9APS.]@[0-9]>"
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#12
|
|||
|
|||
Hi Paul,
Thanks! It got close. Here are the sample results: 3.2.S.3.1 resulted in 3.2 3.2.S.5 resulted in 3.2 32S1 gave 32S1 (worked) I also just realized it pulled out 3.2 and 3.1 for 3.2.S.3.1. Can we get the rest that the first two truncated, please? Thanks, Roy |
#13
|
||||
|
||||
Try:
"<[23][0-9APS.]{1,}"
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#14
|
|||
|
|||
Thank you, Paul, for the continued help. After a couple of days of testing, your solution did find the examples I mentioned above. Unfortunately, it found others, too (e.g. 3.5.P). I simply wrote a routine to ignore the ones that we don't want.
|
Tags |
wildcard searches |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Check if a string is part of an array (containing wildcards...) | jodecaesteker | Excel Programming | 3 | 02-02-2018 10:56 AM |
Wildcards: searching for multiple words / expressions close to each other | ballpoint | Word VBA | 7 | 11-09-2017 03:30 PM |
Searching for string on a formula | AMD2800 | Excel | 1 | 12-17-2014 10:41 AM |
Return entire program to default | blockie | Word | 2 | 04-13-2013 07:18 PM |
Return Entire Row | ibrahimaa | Excel | 3 | 01-10-2012 05:44 AM |