Welcome to Tech Support Forum home to more then 136,000 problems solved. Issues have included: Spyware, Malware, Virus Issues, Windows, Microsoft, Linux, Networking, Security, Hardware, and Gaming Getting your problem solved is as easy as:
1. Registering for a free account
2. Asking your question
3. Receiving an answer

Registered members:
* Get free support
* Communicate privately with other members (PM).
* Removal of this message
* See fewer ads.
* And much more..

 



Want to know how to post a question? click here Having problems with spyware and pop-ups? First Steps
Go Back   Tech Support Forum > Microsoft Support > Microsoft Office support
User Name
Password
Site Map Register Donate Rules Blogs Mark Forums Read


Microsoft Office support MS Office support forum

Reply
 
LinkBack Thread Tools
Old 07-31-2009, 02:50 PM   #1 (permalink)
Registered User
 
Join Date: Jul 2009
Posts: 10
OS: XP


[SOLVED] Word macro - searching for a string between two strings

I am trying to find a way to search to see if a string (ftFindTag) is present between two other specified strings (ftFindTag and ftOpposite) and have got stuck.

Do While MsgBox("Do you want to continue searching for " + ftFindTag + " ?", vbQuestion + vbYesNoCancel, "Tag Search") = vbYes
Selection.Find.Execute (ftFindTag)
StartRange = Selection.End
Selection.MoveRight
Selection.Find.Execute (ftOpposite)
StopRange = Selection.Start
Selection.Range(StartRange, StopRange)
If Selection.Find.Execute(ftFindTag) = True Then
Selection.Find.Execute (ftFindTag)
End If

Is it possible to search between strings using range?

Cheers, Nic
Nic H is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Important Information
Join the #1 Tech Support Forum Today - It's Totally Free!

TechSupportForum.com is a leading support website for your computer needs. We offer free, friendly and personalized computer support. Why pay to have your computer fixed when you can do it for free.

Join TechSupportforum.com Today - Click Here

Old 07-31-2009, 06:32 PM   #2 (permalink)
TSF Enthusiast
 
Join Date: Apr 2008
Location: Australia
Posts: 559
OS: Vista


Re: Word macro - searching for a string between two strings

Hi Nic,

You could use something along the lines of:
Code:
Sub Demo()
Dim ftFindTag As String, ftOpposite As String, myRng As Range
ftFindTag = InputBox("First String to Find")
ftOpposite = InputBox("Last String to Find")
With Selection.Find
  .ClearFormatting
  .Text = ftFindTag & "*" & ftOpposite
  .Forward = True
  .Wrap = wdFindContinue
  .Format = False
  .MatchCase = False
  .MatchWholeWord = False
  .MatchAllWordForms = False
  .MatchSoundsLike = False
  .MatchWildcards = True
  Selection.Find.Execute
  If .Found = True Then
    Set myRng = Selection.Range
    myRng.MoveStart wdCharacter, Len(ftFindTag)
    myRng.MoveEnd wdCharacter, -Len(ftOpposite)
    If InStr(myRng, ftFindTag) > 0 Then MsgBox "A second occurrence of """ _
      & ftFindTag & """" & " exists within the found string:" & vbCr & _
      ftFindTag & myRng & ftOpposite
  End If
End With
End Sub
__________________
Cheers
macropod
(MS MVP -Word)
macropod is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 08-01-2009, 11:32 AM   #3 (permalink)
Registered User
 
Join Date: Jul 2009
Posts: 10
OS: XP


Re: Word macro - searching for a string between two strings

I'm trying to find if I have missing tags that I then hide in word e.g. <!CustA> and </!CustA> and this doesn't work because it says a pattern match expression is not valid? Will this work using non alphanumeric characters?

Cheers, Nic
Nic H is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 08-01-2009, 11:52 AM   #4 (permalink)
Registered User
 
Join Date: Jul 2009
Posts: 10
OS: XP


Re: Word macro - searching for a string between two strings

Additionally I'd like it to continue down the document if the first tag isn't found between the first occurence of ftFindTag and ftOpposite until if find one where it does, or the end of the document is reached - do you think this is possible?

Cheers, Nic
Nic H is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 08-01-2009, 03:59 PM   #5 (permalink)
Registered User
 
Join Date: Jul 2009
Posts: 10
OS: XP


Re: Word macro - searching for a string between two strings

I've tried something like this and if does search the tags but only the first occurence and it doesn't select the text range in the document like yours did - any ideas?
ftFindTag = InputBox("Enter the missing tag")
ftOpposite = InputBox("Enter the opposite tag")
While Selection.Find.Execute(ftOpposite)
StartRange = Selection.End
Selection.MoveRight
Selection.Find.Execute (ftFindTag)
StopRange = Selection.Start
Selection.MoveRight
Selection.Find.Execute
Set myRng = ActiveDocument.Range(StartRange, StopRange)
If InStr(myRng, ftOpposite) > 0 Then answer = MsgBox("A missing " + ftFindTag + " exists within the selected range", vbExclamation, "Find Missing Tag")
Wend

Thanks for your help.

Cheeers, Nic
Nic H is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 08-01-2009, 04:47 PM   #6 (permalink)
TSF Enthusiast
 
Join Date: Apr 2008
Location: Australia
Posts: 559
OS: Vista


Re: Word macro - searching for a string between two strings

Hi Nic,

The reason <!CustA> and </!CustA> returns 'a pattern match expression is not valid' is that the macro I posted uses a wildcard search and certain characters (eg []{}()<>\!?*@#) are control characters in such a search. If you're going to search for those characters explicitly using wildcards, you need to prefix them with a '\'. So, you need to either encode the input string correctly or get the macro to parse it for you (not too difficult).

I'm not sure what the point would be of continuing the search until no unbalanced sets are found as, having found that they're unbalanced, you'd need to eyeball the found range and work out manually where the error occurs. That error could occur anywhere within the found range - and could even extend beyond it if the flaw is with the positioning of the 'ftOpposite' tags. It would be possible, though, to get a count of how many of the found strings there are in the range.
__________________
Cheers
macropod
(MS MVP -Word)
macropod is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 08-02-2009, 07:53 AM   #7 (permalink)
Registered User
 
Join Date: Jul 2009
Posts: 10
OS: XP


Re: Word macro - searching for a string between two strings

Hi

You're right about running until the end of the document. I can return the number of occassions and then run the macro that many times, fixing the missing tags each time one is found.

Your original coding was much better than mine. What would the parse look like?

Cheers, Nic
Nic H is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 08-02-2009, 05:33 PM   #8 (permalink)
TSF Enthusiast
 
Join Date: Apr 2008
Location: Australia
Posts: 559
OS: Vista


Re: Word macro - searching for a string between two strings

Hi Nic,

try the following:
Code:
Sub FindMisMatches()
Dim ftFindTag As String, ftOpposite As String, myRng As Range, i As Integer
Dim fStart As String, fEnd As String, pStr As String, StrChr As String
pStr = "\,[,],{,},(,),<,>,!,?,*,@,#"
ftFindTag = InputBox("First String to Find")
ftOpposite = InputBox("Last String to Find")
If ftFindTag = "" Or ftOpposite = "" Then End
fStart = ftFindTag
fEnd = ftOpposite
For i = 0 To UBound(Split(pStr, ","))
  StrChr = Split(pStr, ",")(i)
  fStart = Replace(fStart, StrChr, "\" & StrChr)
  fEnd = Replace(fEnd, StrChr, "\" & StrChr)
Next
Selection.HomeKey
With Selection.Find
  .ClearFormatting
  .Text = fStart & "*" & fEnd
  .Forward = True
  .Format = False
  .MatchCase = False
  .MatchWholeWord = False
  .MatchAllWordForms = False
  .MatchSoundsLike = False
  .MatchWildcards = True
  .Wrap = wdFindContinue
  .Execute
  Do While .Found
    Set myRng = Selection.Range
    myRng.MoveStart wdCharacter, Len(ftFindTag)
    myRng.MoveEnd wdCharacter, -Len(ftOpposite)
    If InStr(myRng, ftFindTag) > 0 Then
      i = (Len(myRng.Text) - Len(Replace(myRng.Text, ftFindTag, ""))) / Len(ftFindTag)
      MsgBox i + 1 & " occurrences of """ & ftFindTag & """" & _
      " exists within the found string:" & vbCr & _
      ftFindTag & myRng & ftOpposite
      If MsgBox("Continue", vbYesNo) = vbNo Then End
    End If
    Selection.Collapse (wdCollapseEnd)
    .Execute
  Loop
End With
End Sub
This version of the macro reports each errant string and asks if you want to continue. I've added the parsing code also.
__________________
Cheers
macropod
(MS MVP -Word)
macropod is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 08-03-2009, 11:47 AM   #9 (permalink)
Registered User
 
Join Date: Jul 2009
Posts: 10
OS: XP


Re: Word macro - searching for a string between two strings

Great, that works a gem. Can vb search for the tags within headings? It seems to skip these?

Cheers, Nic
Nic H is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 08-04-2009, 01:13 AM   #10 (permalink)
TSF Enthusiast
 
Join Date: Apr 2008
Location: Australia
Posts: 559
OS: Vista


Re: Word macro - searching for a string between two strings

Hi Nic,

You could simply open the header before running the macro.

The alternative is to re-work the code so that it process all StoryRanges. For example, you could replace:
Selection.HomeKey
with:
For Each oRng In ActiveDocument.StoryRanges
oRng.Select
Selection.Collapse
and add:
Next
before:
End Sub

These changes will also process textboxes etc. Note, though that this will leave the document in 'Normal' view ratr than, say, Print Layout view - and probably with the last-processed header or footer active.
__________________
Cheers
macropod
(MS MVP -Word)

Last edited by macropod; 08-04-2009 at 01:16 AM.
macropod is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -7. The time now is 06:21 AM.



Copyright 2001 - 2009, Tech Support Forum
Home Tips Plus | Outdoor Basecamp | Automotive Support Forum

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85