MS Office Forum / Word / Programming / February 2006
Question for Greg Maxey on Open for Input statement
|
|
Thread rating:  |
Larry - 05 Feb 2006 11:41 GMT 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 Larry - 05 Feb 2006 12:06 GMT 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, > [quoted text clipped - 24 lines] > > Close #1 > > End Sub Jezebel - 05 Feb 2006 12:31 GMT 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 [quoted text clipped - 46 lines] >> > Close #1 >> > End Sub Greg Maxey - 05 Feb 2006 19:40 GMT 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
 Signature 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 -- [quoted text clipped - 58 lines] >>>> Close #1 >>>> End Sub Jezebel - 05 Feb 2006 20:46 GMT Try --
myFile() = Split(Input(LOF(1), #1), vbCrLF)
> Jezebel, > [quoted text clipped - 102 lines] >>>>> Close #1 >>>>> End Sub Greg Maxey - 05 Feb 2006 20:55 GMT Yes, that works much better.
Thanks.
 Signature Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
> Try -- > [quoted text clipped - 108 lines] >>>>>> Close #1 >>>>>> End Sub Larry - 05 Feb 2006 23:58 GMT 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. > [quoted text clipped - 112 lines] > >>>>>> Close #1 > >>>>>> End Sub Greg Maxey - 06 Feb 2006 00:53 GMT 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.
 Signature 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 [quoted text clipped - 151 lines] >>>>>>>> Close #1 >>>>>>>> End Sub Jezebel - 06 Feb 2006 01:00 GMT 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)
>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 [quoted text clipped - 147 lines] >> >>>>>> Close #1 >> >>>>>> End Sub Greg Maxey - 06 Feb 2006 01:16 GMT 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.
 Signature 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? [quoted text clipped - 165 lines] >>>>>>>>> Close #1 >>>>>>>>> End Sub Larry - 06 Feb 2006 01:40 GMT You have a good memory, Greg. :-)
> Jezebel, > [quoted text clipped - 170 lines] > >>>>>>>>> Close #1 > >>>>>>>>> End Sub Jezebel - 06 Feb 2006 01:45 GMT Perhaps we'd better get into Curly and Moe mode.
> Jezebel, > [quoted text clipped - 170 lines] >>>>>>>>>> Close #1 >>>>>>>>>> End Sub
|
|
|