Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-11-2022, 01:34 PM
AlexCummins AlexCummins is offline Problem with macro searching for overlapping strings and inserting a comment Windows 10 Problem with macro searching for overlapping strings and inserting a comment Office 2007
Novice
Problem with macro searching for overlapping strings and inserting a comment
 
Join Date: Jan 2022
Posts: 2
AlexCummins is on a distinguished road
Default Problem with macro searching for overlapping strings and inserting a comment


Hi all, I'd be grateful for your help on a Word macro problem that I'm facing. I'm not a VBA programmer (just an ordinary Word user) and so I don’t know all the tips and tricks for writing macros: I'd really appreciate your experience here.

I'm writing macros that search for all occurrences of a string, and then include a comment bubble on that string. The problem I've realised is that Word search does not appear to work properly if a string already has an existing comment marked on part of that string.

For example, imagine (using random search terms):

1. (First string) I search in a document for all occurrences of the string "without," and apply a comment to each of those (e.g. "This is string: without").

2. (Second, overlapping, string) I then search in a document for all occurrences of the string "without you," and try to apply a comment to each of those (e.g. "This is string: without you").

Step 2 does not work. The find functionality in Word can't seem to identify "without you" in step 2 where "without" already has a comment applied to it from step 1.

I tried to solve for this by changing comments to footnotes, but I get the same problem: if you have a footnote applied after "without" in step 1, Word search cannot find "without you" in step 2.

I thought wildcards would solve this, but they don't: e.g. the search term "without*you" does not identify either "without you" already marked with a comment, or "without1 you" where a footnote has been applied to "without" previously.

If it helps, here is the (clunky and inefficient I'm sure) macro code I've been using:

Sub CallMacros()
Call 001Without
Call 002WithoutYou
End Sub

Sub 001Without()
Dim range As range
Set range = ActiveDocument.Content
Do While range.Find.Execute("without", False) = True
ActiveDocument.Comments.Add range, "This is string: without"
Loop
End Sub

Sub 002WithoutYou()
Dim range As range
Set range = ActiveDocument.Content
Do While range.Find.Execute("without you", False) = True
ActiveDocument.Comments.Add range, "This is string: without you"
Loop
End Sub

The only solution I've found is a manual workaround: to change the call order so that larger strings are called before shorter ones (e.g. here, calling 002WithoutYou before 001Without). But it seems wrong to use this manual workaround: it relies on getting the call order right, which seems inefficient.

It looks like LibreOffice doesn't have this search issue (e.g. even if a comment is applied to "without", a search will find "without you"), but I don't know how to code macros in LibreOffice and so would prefer not to switch processors.

If there's a much better way of doing this, I'd really appreciate hearing about it, thank you all!!
Reply With Quote
  #2  
Old 01-11-2022, 03:04 PM
Guessed's Avatar
Guessed Guessed is offline Problem with macro searching for overlapping strings and inserting a comment Windows 10 Problem with macro searching for overlapping strings and inserting a comment Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

This is dodgy and there is a mixed bag of results possible here. In my testing I can get a hit on the second search if I:
1. Change to Draft View
2. Set Review Markup to show all markup
3. Include a ^? where the first comment ends

I prefer your simpler suggestion of searching the larger strings first but perhaps you can experiment with the above preconditions to arrive at a solution which is independent of that.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 01-11-2022, 04:30 PM
AlexCummins AlexCummins is offline Problem with macro searching for overlapping strings and inserting a comment Windows 10 Problem with macro searching for overlapping strings and inserting a comment Office 2007
Novice
Problem with macro searching for overlapping strings and inserting a comment
 
Join Date: Jan 2022
Posts: 2
AlexCummins is on a distinguished road
Default

Many thanks for this!

I now see from applying your item 3 above that Word is inserting some sort of hidden special character at the boundaries of the string marked with a comment. Item 3 above on its own solves it.

This also allows me to work out a (still clunky, but workable) solution to where strings are not nested (where I can use the system of calling the longer strings before the shorter ones), but overlap, such as for the strings:
  • going without
  • without you
  • you are

The best way I can think of to search for "without you" with the possibility of hidden special characters after "without" (after a comment has been applied to "going without") and/or before "you" (after a comment has been applied to "you are") is to have three variants of the "without you" macro (01 in the case where there is no special character in the string, 02 where there is one plus a space, and 03 where there are two plus a space):

Sub CallMacros()
Call Without
Call WithoutYou01
Call WithoutYou02
Call WithoutYou03
End Sub

Sub Without()
Dim range As range
Set range = ActiveDocument.Content
Do While range.Find.Execute("without", False) = True
ActiveDocument.Comments.Add range, "This is string: without"
Loop
End Sub

Sub WithoutYou01()
Dim range As range
Set range = ActiveDocument.Content
Do While range.Find.Execute("without you", False) = True
ActiveDocument.Comments.Add range, "This is string: without you"
Loop
End Sub

Sub WithoutYou02()
Dim range As range
Set range = ActiveDocument.Content
Do While range.Find.Execute("without^?^?you", False) = True
ActiveDocument.Comments.Add range, "This is string: without you"
Loop
End Sub

Sub WithoutYou03()
Dim range As range
Set range = ActiveDocument.Content
Do While range.Find.Execute("without^? ^?you", False) = True
ActiveDocument.Comments.Add range, "This is string: without you"
Loop
End Sub

I couldn't find a cleaner way to do this (e.g. ignoring special characters in a search string, or using wildcards within a string), but this does work.

I'll work with this for now as it does the job. If there's a better way of doing it, grateful for any further input. Thanks again!
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with searching at outlook tomer Outlook 1 11-27-2018 06:21 AM
Office Word problem Comment author name jbln Word 1 08-23-2016 04:40 PM
Problem with macro searching for overlapping strings and inserting a comment Inserting comment bubbles in MSWord? amitrus Word VBA 2 02-07-2013 07:53 AM
Comment-Balloon Textsize Problem aerzteohnegrenzen Word 7 05-04-2011 10:45 AM
Problem with macro searching for overlapping strings and inserting a comment update style of all strings available between two specific strings vikrantkale Word 1 03-28-2011 06:13 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:46 PM.


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