I gather one step is to put all the separate lines in the file into
one string variable, and then break that string into the separate
paths of each Word document and run each one. Could you put
together all the steps that need to be done for this?
Also, I thought an advantage of Greg's code was that it avoided
using an array, which I got the impression is supposed to make it
faster or more economical. But now this new code has an array again
and is longer. Anyway, this seems to be the code, thought I get an error
message
saying that the function of sub for Split has to be defined and how
do I do that?
Dim SettingsFile As String
Dim pFileContents As String
Dim myFile() As String
Dim i As Long
SettingsFile = Options.DefaultFilePath(wdDocumentsPath) &
"\MyFileList.txt"
Open SettingsFile For Input As #1
pFileContents = Input(LOF(1), #1)
Close #1
myFile() = Split(Input(LOF(1), #1), vbCrLf)
'myFile() = Split(pFileContents, vbCr)
i = 0
Documents.Open myFile(i)
For i = 1 To UBound(myFile) - 1
Documents.Open Right(myFile(i), Len(myFile(i)) - 1)
Next
Larry
Yes, that works much better.
Thanks.
--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
Jezebel wrote:
Try --
myFile() = Split(Input(LOF(1), #1), vbCrLF)
Jezebel,
For this specific usage, it doesn't seem beneficial to read the
entire file contents in as a single string. Unless I am
overlooking
something, it seems to make opening the listed files more
complicated. When I use "Split" to create the array of file names
"myFile()", the second and each subsequent file name is preceeded
with the delimiter character (in this case the end of line
marker). The first file opens normally, but without addition
manipulation of the string myFile(i), an error is generated
trying to open the second file in the list. Sub OpenListOfFiles()
Dim SettingsFile As String
Dim pFileContents As String
Dim myFile() As String
Dim i As Long
SettingsFile = Options.DefaultFilePath(wdDocumentsPath) &
"\MyFileList.txt"
Open SettingsFile For Input As #1
pFileContents = Input(LOF(1), #1)
Close #1
myFile() = Split(pFileContents, vbCr)
i = 0
Documents.Open myFile(i)
For i = 1 To UBound(myFile) - 1
Documents.Open Right(myFile(i), Len(myFile(i)) - 1)
Next
End Sub
The original method seems simpler:
Sub OpenListOfFiles()
Dim myFile As String
Dim SettingsFile As String
SettingsFile = Options.DefaultFilePath(wdDocumentsPath) &
"\MyFileList.txt"
Open SettingsFile For Input As #1
While Not EOF(1)
Input #1, myFile
Documents.Open (myFile)
Wend
Close #1
End Sub
--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
Jezebel wrote:
Perhaps you've missed the distinction between the input statement
and the input function. ou can also use --
Dim pFileContents as string
pFileContents = Input(LOF(1),#1)
to read the entire file into a string. It's much quicker to do
that,
then process the string, than to read the file character by
character or line by line.
My mistake, I had found the VBA Help file on the Input statement
instead of the Input # statement, which is different. Here's
Help's sample of an Input # statement:
Dim MyString, MyNumber
Open "TESTFILE" For Input As #1 ' Open file for input.
Do While Not EOF(1) ' Loop until end of file.
Input #1, MyString, MyNumber ' Read data into two variables.
Debug.Print MyString, MyNumber ' Print data to Debug window.
Loop
Close #1 ' Close file.
So it seems to be set up so that the code
Input #1, MyString
simply means by default that the first line of the file is being
assigned to the variable My String.
Greg,
I was trying to understand the syntax of the Open for Input
statement you provided in an earlier thread for the
OpenListOfFiles macro, and I don't get how the line "Input #1,
myFile" works. The syntax and
sample
code given in VBA Help are different, like this:
Input(number, [#]filenumber)
MyChar = Input(1, #1)
where the number, i.e. the first "1" in the parentheses,
represents the number of characters to be returned. VBA Help
doesn't say how to return an entire line as distinct from a
certain number of characters. Your code does return an entire
line, by leaving out the number altogether, but I don't know
how a user would derive that from what is provided in Help. Could
you "translate" the below line? Input #1, myFile
Sub OpenListOfFiles()
Dim myFile As String
Open "e:\Junk\MyFileList.txt" For Input As #1
While Not EOF(1)
Input #1, myFile
Documents.Open (myFile)
Wend
Close #1
End Sub