Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 02-20-2019, 04:36 AM
MikeForward MikeForward is offline Windows 10 Office 2010
Novice
 
Join Date: Feb 2019
Posts: 10
MikeForward is on a distinguished road
Default Word VBA wildcard search - odd results

Hi there,

I am finding odd results with the following wildcard search in Word 2010 VBA. I am trying to search for special codewords embedded in a Word doc which look like "ZXZABCDEF...". where X is a single character which can be 0-9,A-Z, as are the ABCDEF.... The first and third characters are always Z. The ABCDEF...is a minimum of 3 characters and a maximum of 16 characters.

The following search works correctly in either backwards or forwards direction:
[/code] Selection.Find.ClearFormatting
With Selection.Find
.Text = "<Z[0-9A-Z]Z[0-9A-Z]{3}"
.Replacement.Text = ""
.Forward = bForwards '***where bForwards is a Boolean = true or false
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True


End With
b = Selection.Find.Execute[/code]

However, if I change the {3} in the .Text= line to {3,16} the search then works forwards, but fails backwards! Any ideas? Macropod - this is a simplified form of my first question, and does seem to give genuinely odd results. I promise no more questions for a while after this!

Thanks a lot

Mike
Reply With Quote
  #2  
Old 02-20-2019, 02:57 PM
kilroy kilroy is offline Windows 10 Office 2016
Advanced Beginner
 
Join Date: Sep 2016
Location: Southern Ontario
Posts: 63
kilroy is on a distinguished road
Default

I don't think you can use{3} for your needs. I can't answer your exact question but I use this macro for finding strings that aren't always know.

Code:
Sub WhereIsIt()
    a1 = "[0-9A-z] "
    B2 = "Z[0-9A-z]Z"
    
    ActiveDocument.Range(0).Select
    Selection.Find.ClearFormatting
    While Selection.Find.Execute(a1)
        StartReformat = Selection.Start
        Selection.MoveRight
        Selection.Find.Execute (B2)
        StopReformat = Selection.End
        Selection.MoveRight
        Selection.Select
        Selection.Font.Color = 255
        Selection.Font.Bold = True
        Selection.Font.Size = 20
    Wend
   
End Sub
Reply With Quote
  #3  
Old 02-20-2019, 06:36 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,390
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

You could use something like:
Code:
Sub Demo()
Dim b As Boolean, Rng As Range
Const bForwards As Boolean = False
Set Rng = Selection.Range: b = False
With Selection
  With .Find
    .ClearFormatting
    .Text = "<Z[0-9A-Z]Z[0-9A-Z]@>"
    .Replacement.Text = ""
    .Forward = bForwards
    .Wrap = wdFindStop
    .Format = False
    .MatchWildcards = True
    .Execute
  End With
  Do While .Find.Found
    If .InRange(Rng) = False Then Exit Do
    If Len(.Text) < 17 Then b = True
    If b = True Then Exit Do
    .Collapse bForwards + 1
    .Find.Execute
  Loop
End With
MsgBox b
End Sub
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #4  
Old 02-21-2019, 06:18 AM
MikeForward MikeForward is offline Windows 10 Office 2010
Novice
 
Join Date: Feb 2019
Posts: 10
MikeForward is on a distinguished road
Default

Thanks a lot, that's sorted it.

Mike
Reply With Quote
Reply

Tags
wildcard searches

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Having problems with a Wildcard search Cosmo Word VBA 9 02-12-2016 08:03 PM
Change characters outside a wildcard while keeping wildcard results nymusicman Word VBA 2 04-10-2014 08:17 AM
2013 search results take a long time - they fill in as results in reverse date order themookman Outlook 0 10-11-2013 12:01 PM
Saving search results in word 2010 swami Word 2 09-04-2013 11:34 PM
Wildcard search help. Kempston Word 0 11-13-2009 03:58 AM


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