![]() |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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
|
|
#3
|
||||
|
||||
|
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 [Fmr MS MVP - Word] |
|
#4
|
|||
|
|||
|
Thanks a lot, that's sorted it.
Mike |
|
| Tags |
| wildcard searches |
|
|
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 |