#1
|
|||
|
|||
VBA find keyword and move to location then add symbol
I need to use VBA in word to firstly match (find) a word in a given document, from a external text file, then move to the right 5 words from the matched word to the end of that word and then at that point insert a symbol and then resume with the next word in the text file, running this all from a macro. The purpose of this is to use a text file to find key words in a document, then in the document move a few words further along and enter a tick before reading the next key word and repeating until the EOF. I can read the file, find the word but cannot then move the cursor to the right and add the symbol only once. It moves the required amount of words but then I seem to get multiple ticks rather than just one for each key word. I just want one tick for each key word 5 words away, reading from a text file. |
#2
|
||||
|
||||
You don't need to move the cursor. All you need do is tell Word how many words after the found one to insert the symbol. Without seeing the relevant part of your code, tbough (and there are plenty of examples on the forum for using content in one file to Find/Replace in another), I can't say which approach to that is best suited to what you're using. It could, for example, be achieved by a carefully crafted Find/Replace expression without the need to change anything else about or code or, perhaps, by some other change to your code.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Your response
Thank you for your help. I have the file opening and reading the words, even find them and adding the tick directly after the found word but have not managed to work out how to put the tick 5 words away. What I have not mastered I guess is the "carefully crafted find and replace expression" that will move the tick 5 words away. Is it possible you could explain the code to me further? I could put the code I have here if that would help. The examples I have found so far are for finding and replacing the word in the document with the word in the file which I do not need as the original word needs to remain in the document not be replaced with anything. I need only to insert the tick when the word is found but not directly next to it. Down a line would be ok, just not immediately next to it. At the moment the code uses the replace statement to replace the word found (with itself) and concatenate the tick immediately after as part of the replace statement and then move on to the next word in the file within the read loop. I located an example to move right 5 words but ended up with multiple ticks all in the same place after the first word was found.
|
#4
|
||||
|
||||
You could, for example, use a wildcard Find/Replace, using:
Code:
With .Find .ClearFormatting .Replacement.ClearFormatting .Text = StrTxt & "[,. ]@<*>[,. ]@<*>[,. ]@<*>[,. ]@<*>[,. ]@" .Replacement.Text = "^&" & Chr(123) .Forward = True .Wrap = wdFindcontue .Format = False .MatchWildcards = True .Execute Replace:=wdReplaceAll End With
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
how to find and replace a symbol with another symbol | wondermuse | Word | 5 | 10-09-2014 06:30 AM |
Move shape to bookmark location | Byron Polk | Word VBA | 4 | 08-07-2014 03:21 AM |
How to either move document display to right or left; or reduce size of find/replace window. | pmstock | Word | 2 | 08-02-2014 08:34 PM |
Find keyword in section & delete section | donaldadams1951 | Word VBA | 5 | 12-03-2013 10:08 AM |
Move pst location? | markg2 | Outlook | 0 | 06-29-2013 11:00 AM |