troubleshooting Question

macro to selectively delete and insert specific characters and text into MS word 2003

Avatar of ikelw
ikelw asked on
Visual Basic ClassicMicrosoft ExcelMicrosoft Word
4 Comments1 Solution1111 ViewsLast Modified:
I have a macro with many subroutines that helps to accomplish the primary editing I do on documents.  this macro primarly uses two techniques to replace or insert text - either "range.find.replace a word with another word, or with nothing (is how I delete text)", or "range.insertbefore".  It operates with "track changes" enabled, which allows me to scan through the document and see what changes if any, the macro did for me...  often I use Match wildcards, because it allows me the greatest flexibility to find and replace particular patterns in the documents...

I've noticed some idiosyncracies with word find/replace/insert/delete/track-changes/wildcard:

(1) while track changes is enabled, using a wildcard to find and replace text only does a marginal job - for instance, if I ran a wildcard to find and replace, "(^11)([a-z]. Subparagraph.)" with "\1    \2", [which adds four spaces before the a-z bullet for sub paragraphs] it might result in "a.  a.      Subparagraph", even though I told it to put the spaces in between the first and second segment...

(2) while doing a find and replace operation with some word in the 'find' box and nothing in the replace box - doing that to delete words, it often leaves two spaces where there should only be one.  I don't mind going through afterwards and deleting the extra spaces - and it is preferable to #3 explained below, which sometimes deletes extra words!  I use 'matchwholewords' but when I must search for wildcards, I cannot use matchwholewords...  and sometimes the whole words I wish to delete contain hyphens, which strangely enough, are NOT considered "whole words" by MS Word...  (So I think I'd be better off programming my own delete functions).

(3) if I use the built-in feature of "Range.Words(1).Delete" it will on occasion delete the desired word, AND the NEXT word (which I don't want it to do).  For example, I tell it to delete "first.", and it for some reason, will also delete " Second" which is in the next sentence...  Also, if I run the macro more than once, it will continue to delete words after Second - this is likely due to the word "first." only being removed as a "change" or "review" of the document (as in track changes) - which doesn't completely remove the word, but only separates it from the main portion of the document...    so the macro still thinks it's there, and it still deletes the 'word' - but the word is different from the one I want deleted...


Anyways, my question:   is there any way to do like a 'binary-styled' find and replace, or 'binary' insert strings, or 'binary' delete strings (binary meaning extremely basic, like printf) - so I don't have to fumble with the idiosyncracies of word - wanting to delete the following characters, or not wanting to delete spaces before and after a word?

Also, I can't seem to find a help file that will explain to me the ins and outs of ordinary deletion/insertion versus deletion/insertion while "track changes" is enabled... and how it affects the values of the Range value in my macro which the macro operates on...  I know when I insert a string, I sometimes have to go "MyRange.End = MyRange.End + len(insertstr)" to compensate for the additional length of the string...

Sometimes in order to scan and conduct find and replace operations in the entire document, I have to re-set the Range value... And this I am unfamiliar with.  For some find and replace operations, rather than using the very simplistic "find.execute replace:=replaceall" I would rather use the following code, and be able to see one at a time which operations are going on:

with myrange.find.
   .text = "blah"
   .replacement.text = "fixed"
   .matchwholewords = false
   .matchwildcards = true
   .continue = wdFindStop
   .othersettings = etc
end with
while myrange.find.execute
   myrange.select
   msgbox "About to fix this selection"
   myrange.delete                                       ' for sample only; may not work
   myrange.insertbefore "fixed"                  'for sample only; may not work
   set myrange = ActiveDocument.Range
wend

Sometimes I only want to conduct find and replace for portions of the document, so adjusting the range as it changes is necessary...

Anyways - the question is: are there simpler ways to go about finding and replacing text, inserting and deleting text, using wildcards, operating on a range which is only a sub-set of the ActiveDocument.Range... and is there some help file somewhere which talks about this?   Perhaps someone familiar with ms word files on the web knows of a link I might try, or MS Word somewhere has a file of it's own which explains the subtleties (sp)...

I would think MS Word ought to have more robust, basic, string-modification functions to it, right?  If not, definately in visual basic, right?  Or is it possible to use C string functions in a visual basic environment? (or the vb versions of them?)

thanks in advance
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 4 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros