Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-13-2019, 06:44 AM
scienceguy scienceguy is offline How to return the full string (entire word) when searching with wildcards in word vba Windows 10 How to return the full string (entire word) when searching with wildcards in word vba Office 2016
Advanced Beginner
How to return the full string (entire word) when searching with wildcards in word vba
 
Join Date: Feb 2019
Posts: 46
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 How to return the full string (entire word) when searching with wildcards in word vba Windows 10 How to return the full string (entire word) when searching with wildcards in word vba Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,975
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

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 How to return the full string (entire word) when searching with wildcards in word vba Windows 10 How to return the full string (entire word) when searching with wildcards in word vba Office 2016
Advanced Beginner
How to return the full string (entire word) when searching with wildcards in word vba
 
Join Date: Feb 2019
Posts: 46
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 How to return the full string (entire word) when searching with wildcards in word vba Windows 10 How to return the full string (entire word) when searching with wildcards in word vba Office 2016
Advanced Beginner
How to return the full string (entire word) when searching with wildcards in word vba
 
Join Date: Feb 2019
Posts: 46
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 How to return the full string (entire word) when searching with wildcards in word vba Windows 7 64bit How to return the full string (entire word) when searching with wildcards in word vba Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
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
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 02-14-2019, 03:13 PM
scienceguy scienceguy is offline How to return the full string (entire word) when searching with wildcards in word vba Windows 10 How to return the full string (entire word) when searching with wildcards in word vba Office 2016
Advanced Beginner
How to return the full string (entire word) when searching with wildcards in word vba
 
Join Date: Feb 2019
Posts: 46
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 How to return the full string (entire word) when searching with wildcards in word vba Windows 10 How to return the full string (entire word) when searching with wildcards in word vba Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,975
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

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 How to return the full string (entire word) when searching with wildcards in word vba Windows 10 How to return the full string (entire word) when searching with wildcards in word vba Office 2016
Advanced Beginner
How to return the full string (entire word) when searching with wildcards in word vba
 
Join Date: Feb 2019
Posts: 46
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 How to return the full string (entire word) when searching with wildcards in word vba Windows 7 64bit How to return the full string (entire word) when searching with wildcards in word vba Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
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
[Fmr MS MVP - Word]
Reply With Quote
  #10  
Old 03-20-2019, 05:42 PM
scienceguy scienceguy is offline How to return the full string (entire word) when searching with wildcards in word vba Windows 10 How to return the full string (entire word) when searching with wildcards in word vba Office 2016
Advanced Beginner
How to return the full string (entire word) when searching with wildcards in word vba
 
Join Date: Feb 2019
Posts: 46
scienceguy is on a distinguished road
Default

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
Reply With Quote
  #11  
Old 03-20-2019, 05:59 PM
macropod's Avatar
macropod macropod is offline How to return the full string (entire word) when searching with wildcards in word vba Windows 7 64bit How to return the full string (entire word) when searching with wildcards in word vba Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Change your Find expression to:
"<[23][0-9APS.]@[0-9]>"
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #12  
Old 03-20-2019, 06:11 PM
scienceguy scienceguy is offline How to return the full string (entire word) when searching with wildcards in word vba Windows 10 How to return the full string (entire word) when searching with wildcards in word vba Office 2016
Advanced Beginner
How to return the full string (entire word) when searching with wildcards in word vba
 
Join Date: Feb 2019
Posts: 46
scienceguy is on a distinguished road
Default

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
Reply With Quote
  #13  
Old 03-20-2019, 06:29 PM
macropod's Avatar
macropod macropod is offline How to return the full string (entire word) when searching with wildcards in word vba Windows 7 64bit How to return the full string (entire word) when searching with wildcards in word vba Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try:
"<[23][0-9APS.]{1,}"
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #14  
Old 03-23-2019, 03:27 AM
scienceguy scienceguy is offline How to return the full string (entire word) when searching with wildcards in word vba Windows 10 How to return the full string (entire word) when searching with wildcards in word vba Office 2016
Advanced Beginner
How to return the full string (entire word) when searching with wildcards in word vba
 
Join Date: Feb 2019
Posts: 46
scienceguy is on a distinguished road
Default

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.
Reply With Quote
Reply

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
How to return the full string (entire word) when searching with wildcards in word vba 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
How to return the full string (entire word) when searching with wildcards in word vba 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

Other Forums: Access Forums

All times are GMT -7. The time now is 08:42 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