![]() |
#1
|
|||
|
|||
![]()
I'm having a problem with a wildcard search/replace.
I'm looking for a specific string that can have an optional few characters in the middle, but Microsoft Word doesn't seem to have an way to include optional characters in the wildcard search (this would be super easy with Regular Expressions, which I understand much better than Word wildcard coding). The string could be either 'BRAND® (sub brand text)' or 'BRAND® XXL (sub brand text)' So I tried using a single wildcard character ('?'), land imiting it to a range of 1-5 characters using the following: BRAND®?{1,5}\(sub brand text\) But, this matches an unlimited number of characters if the sub brand text doesn't exist directly after the first occurrence of BRAND (it matches all of the text between 'BRAND®' and the next occurrence of '(sub brand text)' in the entire paragraph) The following does work, but doesn't allow me for the instances where the optional characters don't exist: BRAND®?????\(sub brand text\) This is related to the issues I had in a previous thread (https://www.msofficeforums.com/word/...nces-each.html). I need to search for the first instance of either string on each page, so I can't run this as 2 separate searches. I don't understand why my first attempt isn't working, I am specifying that I want 1-5 single character(s). I would expect using '*' instead of '?' to give me this result. I'm assuming I'm doing something wrong with the range to indicate 1-5 occurrences, but I don't see how to fix it. I haven't found anything indicating that you can't use the single character '?' wildcard with the '{n,m}' limits, but this is my only guess as to what is going wrong. Any assistance/explanation would be gratefully appreciated. |
#2
|
||||
|
||||
![]()
You could use a wildcard Find where:
Find = <BRAND®[!\(]{1,5}\([!\)]@\)
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
![]()
Excellent. That seems to work perfectly, thanks!
I did make one small change, since I need to use that optional part in a replace function (I added parentheses around the optional 1-5 range), and I have that working. My requirement is to remove the '®' and the parentheses text so that 'BRAND® XXL (sub brand text)' becomes 'BRAND XXL' The only issue is that it ends up with an extra space in the replaced value (since that is part of the captured value), but since this will be run in a scripted find/replace, I should be able to remove that extra space using another replace on the range. (e.g. 'BRAND® XXL (sub brand text) more text...' becomes 'BRAND XXL more text...' with 2 spaces) I don't see any way around that, and I don't foresee any problems using another find/replace on the found range, so hopefully I should be able to accomplish this. Thanks again! |
#4
|
|||
|
|||
![]()
Got everything working, thanks again. Had a few issues trying to get the scripted find/replace to work, but a bit of online searching found the information that helped me figure it out (not surprisingly, in forum posts you made).
Your help is once again greatly appreciated! |
#5
|
|||
|
|||
![]()
Here's the final code I am using:
Code:
Dim oDoc As Document Dim rng As Word.Range Dim rngISIDrugName As Word.Range Dim pgNumber As Integer Dim currentPageNumber As Integer Set oDoc = ActiveDocument Set rng = oDoc.Range With rng With .Find .ClearFormatting .Replacement.ClearFormatting .MatchWildcards = True .Replacement.Text = "BRAND\1" .Forward = True .Text = "BRAND®([!\(]{1,5})\([!\)]@\)" .Execute End With While .Find.Found = True pgNumber = .Information(Word.WdInformation.wdActiveEndAdjustedPageNumber) If pgNumber = currentPageNumber Then .Find.Execute Replace:=wdReplaceOne ' NOTE - Need to remove extra space here! If (Right(.Text, 1) = " ") Then .Text = Left(.Text, Len(.Text) - 1) End If .Collapse wdCollapseEnd Else currentPageNumber = pgNumber End If .Find.Execute Wend End With |
#6
|
||||
|
||||
![]()
Try:
Code:
Sub Demo() Application.ScreenUpdating = False Dim i As Long, Rng As Range With ActiveDocument For i = 1 To .ComputeStatistics(wdStatisticPages) Set Rng = .GoTo(What:=wdGoToPage, Which:=wdGoToAbsolute, Name:=i) Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="\Page") With Rng With .Find .ClearFormatting .Replacement.ClearFormatting .Text = "<BRAND®[!\(]{1,5}\([!\)]@\)" .Replacement.Text = "" .Forward = True .Wrap = wdFindStop .Format = False .MatchWildcards = True .Execute End With If .Find.Found Then .Text = Trim(Split(.Text, "(")(0)) End If End With Next End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
![]()
Thanks, I haven't had a chance to check that out just yet. Unfortunately, they have just thrown yet another monkey wrench into the deal.
I now have an optional couple of words which will appear AFTER the parentheses, and although I know what the 2 words will be, I don't know what will be following, so I cannot use what worked above by looking for characters that are not '(' or ')'. So, now I'm looking for the following: 'BRAND® XXL (sub brand text) EXACT TEXT' continued sentence text... Where both 'XXL' and 'EXACT TEXT' are variable (and will always be used together; either they are both included or both excluded). It looks like i may very likely need to use Regular Expressions, which I have used once or twice before in VBA, but for parsing text in variables, not in a word document find/replace. My biggest concern is how reliably can I know/assume that the vbscript Regular Expressions library that I am using is installed on the user's system? Is this installed with the Windows OS, and is it something that their IT could have omitted from their install? ![]() There's also another wrinkle to the problem that I just found out about, in that the first mention of each version on each page needs to be the full text. I don't believe that should be a problem as I can check each found set, and keep track of whether that version has been shortened for each page. Edit: I got this part working properly, so this is no longer an issue. Last edited by Cosmo; 02-12-2016 at 02:47 PM. |
#8
|
|||
|
|||
![]()
After a little digging, I don't even know if I will be able to use the Regular Expressions to do what I need. The native Find/Replace in Word can't use the regular expression, and I don't think that I can use the Regular Expression replace to step through each found value like I am currently. I only see a .Replace method, and I haven't found any example code online that doesn't do a 'replace all'.
So, my questions are boiled down to these: Am I able to step through each found string using a Regular Expression object and decide in my VBA when to do the replace? If so, can you point me to an example of how to do this? Is the Regular Expressions library definitely installed in every target machine, or is there a possibility that it isn't available? |
#9
|
|||
|
|||
![]()
I think I found the solution!!
http://stackoverflow.com/questions/1...-and-then-ital I have been working with the code I found on this link, and I was able to highlight all of the instances of the text in teal. I should (hopefully) now be able to use this to do the replacements that I need. If this is the case, then this issue should be solved, aside from knowing if the user will have the proper libraries installed. I probably won't be able to finish this until next week, I'll post up if I get it working, or need any more assistance. EDIT: IT WORKS!! I'll post the code I'm using on Monday, there may be tweaks to make to make it more efficient, or better coded, but for now, it looks like I have a working proof-of-concept, provided the end user has the proper libraries installed. |
#10
|
||||
|
||||
![]() Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Issue with wildcard search | mysterytramp | Word | 0 | 05-13-2015 10:40 AM |
Where is the error in my wildcard search? | Ulodesk | Word | 10 | 06-30-2014 01:46 PM |
![]() |
NobodysPerfect | Word VBA | 4 | 06-11-2014 11:02 AM |
![]() |
NobodysPerfect | Word | 10 | 03-19-2014 04:29 AM |
Wildcard search help. | Kempston | Word | 0 | 11-13-2009 03:58 AM |