Question for Greg Maxey on Open for Input statement

L

Larry

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
 
L

Larry

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.



Larry said:
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
 
J

Jezebel

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.


Larry said:
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.



Larry said:
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
 
G

Greg Maxey

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.

Perhaps you've missed the distinction between the input statement and
the input function. ou can also use --

Dim pFileContents as stringpFileContents = 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.


Larry said:
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.



Larry said:
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
 
J

Jezebel

Try --

myFile() = Split(Input(LOF(1), #1), vbCrLF)



Greg Maxey said:
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.

Perhaps you've missed the distinction between the input statement and
the input function. ou can also use --

Dim pFileContents as stringpFileContents = 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.


Larry said:
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
 
G

Greg Maxey

Yes, that works much better.

Thanks.

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Try --

myFile() = Split(Input(LOF(1), #1), vbCrLF)



Greg Maxey said:
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.

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
 
L

Larry

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




Greg Maxey said:
Yes, that works much better.

Thanks.

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Try --

myFile() = Split(Input(LOF(1), #1), vbCrLF)



Greg Maxey said:
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
 
G

Greg Maxey

Larry,

By much better I only meant much better than my first attempt using
Jezebel's suggestion. Here is the code:

Sub OpenListOfFilesOption2()
Dim SettingsFile As String
Dim myFile() As String
Dim i As Long
SettingsFile = Options.DefaultFilePath(wdDocumentsPath) & "\MyFileList.txt"
Open SettingsFile For Input As #1
myFile() = Split(Input(LOF(1), #1), vbCrLf)
Close #1
For i = 0 To UBound(myFile) - 1
Documents.Open myFile(i)
Next
End Sub

I don't know or and can't say if it is any better than what I was using
before Jezebel's suggestion. I just want to learn how to do it that way. I
think in this case where a person is likely working with a dozen or less
open files then one is as good as the other.


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

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




Greg Maxey said:
Yes, that works much better.

Thanks.

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

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
 
J

Jezebel

Split() was introduced with (I think) Word 2000. Is your version older than
that?

In your code, either read the file into a string and then split the string,
or simply split the input

pFileContents = Input(LOF(1), #1)
myFile = Split(pFileContents, vbCrLF)

or

myFile = Split(Input(LOF(1), #1), vbCrLF)




Larry said:
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




Greg Maxey said:
Yes, that works much better.

Thanks.

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

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
 
G

Greg Maxey

Jezebel,

Hmmmm. I know there are lots of Larry's in this world, but if our Larry is
the Larry I suspect, I believe that he is a Word97 man.


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Split() was introduced with (I think) Word 2000. Is your version
older than that?

In your code, either read the file into a string and then split the
string, or simply split the input

pFileContents = Input(LOF(1), #1)
myFile = Split(pFileContents, vbCrLF)

or

myFile = Split(Input(LOF(1), #1), vbCrLF)




Larry said:
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




Greg Maxey said:
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
 
L

Larry

You have a good memory, Greg. :)


Greg Maxey said:
Jezebel,

Hmmmm. I know there are lots of Larry's in this world, but if our Larry is
the Larry I suspect, I believe that he is a Word97 man.


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Split() was introduced with (I think) Word 2000. Is your version
older than that?

In your code, either read the file into a string and then split the
string, or simply split the input

pFileContents = Input(LOF(1), #1)
myFile = Split(pFileContents, vbCrLF)

or

myFile = Split(Input(LOF(1), #1), vbCrLF)




Larry said:
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
 
J

Jezebel

Perhaps we'd better get into Curly and Moe mode.


Greg Maxey said:
Jezebel,

Hmmmm. I know there are lots of Larry's in this world, but if our Larry
is the Larry I suspect, I believe that he is a Word97 man.


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Split() was introduced with (I think) Word 2000. Is your version
older than that?

In your code, either read the file into a string and then split the
string, or simply split the input

pFileContents = Input(LOF(1), #1)
myFile = Split(pFileContents, vbCrLF)

or

myFile = Split(Input(LOF(1), #1), vbCrLF)




Larry said:
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
 

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