Variable Replacement Text

  • Thread starter I''ve a feeling I''m not in Unix Anymore
  • Start date
I

I''ve a feeling I''m not in Unix Anymore

I have 8,000 ASCII records that I have imported into Word and am reformatting
to drop into a database. The information I'm currently working with on each
record is the name which appears in the record header and a list of
instruments. Each record ends with a manual page break. Some of the
instrument lists are so long they've broken across pages. A simplified
version of the record format is:

Record1
Name1: some name
%$%->Instrument1a
%$%->Instrument1b
%$%->Instrument1c
<Manual Page Break>
Record2
Name2: another name
%$%->Instrument2a
%$%->Instrument2b
%$%->Instrument2c
<Automatic page Break>
%$%->Instrument2d
<Manual Page Break>

Can anyone help me figure out how to take the name that's on a given record
and use it to replace the %$% character sequence at the beginning of each
instrument line for that same record only? I'm new to VBA and although I
understand how to code a find & replace, I can't figure out how to get the
replace text to change to the new name when I change records. (Using an
array is out of the question due to the large number of records I'm dealing
with.)

Here are the results I need:
Record1
Name1: some name
some name->Instrument1a
some name->Instrument1b
some name->Instrument1c
<Manual Page Break>
Record2
Name2: another name
another name->Instrument2a
another name->Instrument2b
another name->Instrument2c
<Automatic Page Break>
another name->Instrument2d
<Manual Page Break>

Thank you in advance!
 
H

Helmut Weber

Hi,

the logic is eternal, whether in Unix or somewhere else.

Have a look at this one:
Sub Macro15()
Dim rDcm As Range
Dim sNam As String
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "Name[0-9]{1,}: *^12"
' 12 = manual page break
.MatchWildcards = True
.MatchCase = True ' just in case
While .Execute
' isolate the name
sNam = rDcm.Text
sNam = Right(sNam, Len(sNam) - InStr(sNam, " "))
' from the forst space onwards
sNam = Left(sNam, InStr(sNam, chr(13)) - 1)
' until without the first paragraph
MsgBox "[" & sNam & "]" ' for testing using F8
rDcm.Select ' for testing, too, using F8
rDcm.Text = Replace(rDcm.Text, "%$%", sNam)
rDcm.Collapse direction:=wdCollapseEnd
Wend
End With
End Sub

Take special care of the last record,
as I got a feeling, that the last record
will not be closed by a manual page break.

Though your desciption says, it will be.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
I

I''ve a feeling I''m not in Unix Anymore

Thank you so much, Helmut, your solution works great!

You're absolutely right -- the logic is the same regardless of the
programming language you're using. I knew I was stuck going around in
logical circles in my own head so that's why I decided to ask the newsgroup
for help. I'm the only technical person in my office so I didn't have anyone
here to help me get my thinking "unstuck".

Also, thank you for letting me know that a manual page break is ^12. I've
looked for that information everywhere and couldn't find it. All I could
find was ^m, which wasn't useful to me since it isn't valid with wildcards
turned on.

Since, as I said, I'm the only technical person in my office, and I'm
teaching myself Windows as I go along, I really appreciate you and the
newsgroup being here to pass along such handy information!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top