Getting INput from a List

M

mcambrose

I want to allow the user to create an input file with a single phrase on each
line (corresponds to a document tab name). I want to write a macro to read in
the user data one line at a time and then use the find/replace to put this
data into my template. For example I will replace the text Tab1 with the
information on line 1. I know how to use the find/replace, but I don't know
how to read the data in and then step to the next data after completing the
find/replace. If I were using excel I would refer to a range and then step
through each cell in the range. I just don't know what would be equivalent in
word. would I have the user put information in a table and then somehow step
through tabl>?

Thanks
 
J

Jay Freedman

If the input file is a Word document (or a text file opened as a document),
and if each line ends with a paragraph mark (¶), then you could do something
like this:

Sub x()
Dim myDoc As Document
Dim listDoc As Document
Dim myPara As Paragraph
Dim myTxt As String
Dim myRg As Range
Dim tabNum As Long

Set myDoc = ActiveDocument
Set listDoc = Documents.Open("C:\path\list.doc")
tabNum = 0

For Each myPara In listDoc.Paragraphs
tabNum = tabNum + 1
myTxt = myPara.Range.Text
' exclude the paragraph mark
myTxt = Left$(myTxt, Len(myTxt) - 1)

' do the replacement in myDoc
Set myRg = myDoc.Range
With myRg.Find
.Text = "Tab" & tabNum
.Replacement.Text = myTxt
.Execute Replace:=wdReplaceAll
End With
Next
End Sub

If the input file is a plain-text file, there are VBA functions for dealing
with such files. Here's an example from the VBA help:
Dim TextLine
Open "TESTFILE" For Input As #1 ' Open file.
Do While Not EOF(1) ' Loop until end of file.
Line Input #1, TextLine ' Read line into variable.
Debug.Print TextLine ' Print to the Immediate window.
Loop
Close #1 ' Close file.
You would replace the Debug.Print statement with your find/replace code.--
Regards,Jay FreedmanMicrosoft Word MVP FAQ: http://word.mvps.orgEmail
cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 

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