Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-09-2016, 11:50 AM
Cosmo Cosmo is offline Having problems with a Wildcard search Windows Vista Having problems with a Wildcard search Office 2007
Competent Performer
Having problems with a Wildcard search
 
Join Date: Mar 2012
Posts: 240
Cosmo is on a distinguished road
Default Having problems with a Wildcard search


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.
Reply With Quote
  #2  
Old 02-09-2016, 05:18 PM
macropod's Avatar
macropod macropod is offline Having problems with a Wildcard search Windows 7 64bit Having problems with a Wildcard search 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

You could use a wildcard Find where:
Find = <BRAND®[!\(]{1,5}\([!\)]@\)
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 02-10-2016, 06:51 AM
Cosmo Cosmo is offline Having problems with a Wildcard search Windows Vista Having problems with a Wildcard search Office 2007
Competent Performer
Having problems with a Wildcard search
 
Join Date: Mar 2012
Posts: 240
Cosmo is on a distinguished road
Default

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!
Reply With Quote
  #4  
Old 02-10-2016, 09:03 AM
Cosmo Cosmo is offline Having problems with a Wildcard search Windows Vista Having problems with a Wildcard search Office 2007
Competent Performer
Having problems with a Wildcard search
 
Join Date: Mar 2012
Posts: 240
Cosmo is on a distinguished road
Default

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!
Reply With Quote
  #5  
Old 02-10-2016, 01:59 PM
Cosmo Cosmo is offline Having problems with a Wildcard search Windows Vista Having problems with a Wildcard search Office 2007
Competent Performer
Having problems with a Wildcard search
 
Join Date: Mar 2012
Posts: 240
Cosmo is on a distinguished road
Default

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
Reply With Quote
  #6  
Old 02-10-2016, 02:47 PM
macropod's Avatar
macropod macropod is offline Having problems with a Wildcard search Windows 7 64bit Having problems with a Wildcard search 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:
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]
Reply With Quote
  #7  
Old 02-12-2016, 12:15 PM
Cosmo Cosmo is offline Having problems with a Wildcard search Windows Vista Having problems with a Wildcard search Office 2007
Competent Performer
Having problems with a Wildcard search
 
Join Date: Mar 2012
Posts: 240
Cosmo is on a distinguished road
Default

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.
Reply With Quote
  #8  
Old 02-12-2016, 02:23 PM
Cosmo Cosmo is offline Having problems with a Wildcard search Windows Vista Having problems with a Wildcard search Office 2007
Competent Performer
Having problems with a Wildcard search
 
Join Date: Mar 2012
Posts: 240
Cosmo is on a distinguished road
Default

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?
Reply With Quote
  #9  
Old 02-12-2016, 03:31 PM
Cosmo Cosmo is offline Having problems with a Wildcard search Windows Vista Having problems with a Wildcard search Office 2007
Competent Performer
Having problems with a Wildcard search
 
Join Date: Mar 2012
Posts: 240
Cosmo is on a distinguished road
Default

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.
Reply With Quote
  #10  
Old 02-12-2016, 08:03 PM
macropod's Avatar
macropod macropod is offline Having problems with a Wildcard search Windows 7 64bit Having problems with a Wildcard search 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

Quote:
Originally Posted by Cosmo View Post
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).
...

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.
Coding for both these isn't especially difficult, but your specs are ambiguous. Whereas before it seemed you wanted to change only the first instance on each page, it now seems you want to leave the first instance alone - and change what? All remaining instances, or just the second instance?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
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
Having problems with a Wildcard search Wildcard search fails in table NobodysPerfect Word VBA 4 06-11-2014 11:02 AM
Having problems with a Wildcard search Tricky wildcard search NobodysPerfect Word 10 03-19-2014 04:29 AM
Wildcard search help. Kempston Word 0 11-13-2009 03:58 AM

Other Forums: Access Forums

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