Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 02-13-2019, 06:44 AM
scienceguy scienceguy is offline Windows 10 Office 2016
Novice
 
Join Date: Feb 2019
Posts: 12
scienceguy is on a distinguished road
Default 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
Any coaching would be greatly appreciated! Thanks in advance!
Reply With Quote
  #2  
Old 02-13-2019, 03:55 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 1,042
Guessed is on a distinguished road
Default

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
Reply With Quote
  #3  
Old 02-14-2019, 04:34 AM
scienceguy scienceguy is offline Windows 10 Office 2016
Novice
 
Join Date: Feb 2019
Posts: 12
scienceguy is on a distinguished road
Default

Thank you, Andrew! This is great! I really appreciate your time and efforts! This is such a great service.
Reply With Quote
  #4  
Old 02-14-2019, 10:49 AM
scienceguy scienceguy is offline Windows 10 Office 2016
Novice
 
Join Date: Feb 2019
Posts: 12
scienceguy is on a distinguished road
Default

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!
Reply With Quote
  #5  
Old 02-14-2019, 01:55 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 18,989
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

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
[MS MVP - Word]
Reply With Quote
  #6  
Old 02-14-2019, 03:13 PM
scienceguy scienceguy is offline Windows 10 Office 2016
Novice
 
Join Date: Feb 2019
Posts: 12
scienceguy is on a distinguished road
Default

Thanks, Paul, but unfortunately, it's still showing the same issue (3.2.P.1.2.3 only shows 3.2.P.1).
Reply With Quote
  #7  
Old 02-14-2019, 03:24 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 1,042
Guessed is on a distinguished road
Default

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
Reply With Quote
  #8  
Old 02-14-2019, 03:33 PM
scienceguy scienceguy is offline Windows 10 Office 2016
Novice
 
Join Date: Feb 2019
Posts: 12
scienceguy is on a distinguished road
Default

So sorry! Yes, you're right. I did mistype. Many thanks again!
Reply With Quote
  #9  
Old 02-14-2019, 07:14 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 18,989
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

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
[MS MVP - Word]
Reply With Quote
Reply

Tags
wildcard searches

Thread Tools
Display Modes


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


All times are GMT -7. The time now is 11:04 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft