Find and Replace a variable in Word

martin_away
martin_away used Ask the Experts™
on
Hi

just wondering if you can help, I am try to work with large files in MS Word doing a find and replace, is there any way I can get the 'tdk no' on each line and for it to change when it finds a new one (ehg goes from tdk123, to
tdk134 etc).

example of problem and whats required below

The id is a fixed lenght of 6 characters.


Problem
----------------------------------

tdk123

home,true,false,100,1222
home,true,false,100,1222
home,true,false,100,1222
home,true,false,100,1222


tdk134

home,true,false,100,1222
home,true,false,100,1222
home,true,false,100,1222
home,true,false,100,1222
home,true,false,100,1222



What the result should be

----------------------------------
tdk123,home,true,false,100,1222
tdk123,home,true,false,100,1222
tdk123,home,true,false,100,1222
tdk123,home,true,false,100,1222


tdk134,home,true,false,100,1222
tdk134,home,true,false,100,1222
tdk134,home,true,false,100,1222
tdk134,home,true,false,100,1222
tdk134,home,true,false,100,1222
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I don't know if this can be done any other way, but I would do it in VBA.

Simply grab all the document text into a single variable., then create an array of the lines.

dim aTextLines() as string
dim sFullText as string
dim sHeaderText as string
dim lLineLoop as long

sFullText = document.selection.all ' I think this is what it is, if not, it should be easy enough to find...If you still need help finding this, let me know I just can't test it right now

aTextLines = split(sFullText,vbcrlf)

for lLineLoop =lbound(aTextLines) to ubound(aTextLines)
    if left$(aTextLines(lLineLoop),3) = "tdk" then
       sHeaderText = aTextLines(lLineLoop)
       aTextLines(lLineLoop)= ""
    else
       if len(trim(aTextLines(lLineLoop)) > 4 then
           if left$(aTextLines(lLineLoop),4 = "home" then
              aTextLines(lLineLoop) = sHeaderText & "," & aTextLines(lLineLoop)
           endif        
       endif
    endif
next
sFullText = join(aTextLines,vbcrlf)
document.selection.all =sFullText

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Recommendation: Accept Comment from twalgrave

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
DominicCronin
EE Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial