Results 1 to 11 of 11
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    bad File name or number (PUT statement) (Word XP)

    About a year ago I questioned why Word97 wouldn't properly write variant arrays with a descriptor; I decided to try again with WordXP, and have stumbled across an apparent anomaly in the use of library code. The attached ZIP file contains two templates from my C:TEMP folder. One template is the application, one is the library of routines. Both templates contain identical code (Copy/Paste).

    When I run the macro "TESTWriteArray" - from either template - using its own copy of the slave routine - "WriteArray", the little text file is created with the three variant strings within, as a quick double-click on the data file will reveal. If I comment out the slave routine "WriteArray" within the appliaction and try to use the copy of the slave routine in the library (Tools, References ....), the macro fails with a "52 Bad file name or number" message.

    Go figure! Why should it fail just because the procedure is in a different library?
    <pre> Sub TESTWriteArray()
    Dim strar()
    ReDim strar(2)
    strar(0) = "delta"
    strar(1) = "gamma"
    strar(2) = "alpha"
    '
    Dim intfile As Integer
    intfile = FreeFile
    Dim strFileName As String
    strFileName = MacroContainer.Path & "" & "erase3.txt"
    Open strFileName For Binary As intfile
    '
    Call WriteArray(intfile, strar)
    Close
    End Sub
    Public Function WriteArray(intfile As Integer, strar())
    ' Dump the array to file in a form readable by ReadArray
    Put #intfile, , strar
    End Function</pre>


  2. #2
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: bad File name or number (PUT statement) (Word XP)

    Just a WAG: Is it possible WriteArray is also a procedure or method in one of the libraries in your References, so that your own WriteArray sub gets called when it's in the same project as the calling procedure but otherwise the WriteArray method in some other library is being run (and generating the error)? (I note that the first Google result for "writearray" is a method in a Microsoft .NET library.)

  3. #3
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: bad File name or number (PUT statement) (Word

    Steve, thanks for the reminder, and yes, it is an ever-present danger (as I have an old Utils.dot with such procedures within), but No, I'm testing a new library here and had made sure it started as a blank new template with no untowardreferences. That's another reason for me attaching the template pair in this example, so that if anyone did try it and duplicate my findings, they'd NOT have the dtritus of my system.

    FWIW I'm now discovering that (a) although the data is written out from the variant array, VBA doesn't seem to be able to recognise the descriptor on disk and ([img]/forums/images/smilies/cool.gif[/img] there seems to be a problem with large (~2,000 element) arrays. I'm still investigating. The (a) bugs me because VBA has everything it needs to know to get the job done, but seems to be too lazy or ignorant to do the job.

  4. #4
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: bad File name or number (PUT statement) (Word

    Chris,
    Your problem is not related to the variant array. If you rewrite your code so that the WriteArray routine contains all the code for opening, writing and closing the file, you can pass it the variant array and it will work just fine either from within the same template or called from a reference library. The issue appears to be passing a reference to the opened file number.
    HTH.
    Regards,
    Rory

    Microsoft MVP - Excel

  5. #5
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: bad File name or number (PUT statement) (Word

    > passing a reference to the opened file numbe

    Rory, thanks for confirming what I was on my way to declaring. There doers seem to be a problem with passing the opened file number. I am in ther process of embracing my read/write with file number with a pair of routines read/write with file name and will see what happens.

  6. #6
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: bad File name or number (PUT statement) (Word

    Why do you want to open the file in one routine and then write to it in another? It seems more sensible (and more modular) to pass the filename and let the called routine handle the details..
    Charlotte

  7. #7
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: bad File name or number (PUT statement) (Word

    > It seems more sensible

    Only if your mind is set on one track.

    It makes sense, at times, to open a file for output, and then issue calls to a routine to append to that file as events occur. One isn't always doing an open-write-and-close. Having the appliaction nominate its own file, and passing the handle of that file to a routine is a valid approach to programming.

    It makes sense to build a library of re-usable code, and that suggests various routines that deal with output, and input, to files. Those routines may be taking care of other housekeeping matters, too, so in general one writes a routine that covers a simple "Put" or "Print". There's no great value in re-opening a file in Append mode several thousand times per job, and that's what I'd be doing if I passed the file identity by string name rather than by handle.

  8. #8
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: bad File name or number (PUT statement) (Word

    > The issue appears to be passing a reference to the opened file number

    I believe it is, as the attached ZIP file shows. I have an application A.dot and a slave S.dot. The slave (library) holds both a write-by-name and a write-by-handle routine. The write-by-name uses the write-by-handle routine.

    On my WinXP/OfficeXP system, the application succeeds in calling the write-by-name, but fails in calling the write-by-handle.

    I've mulled this over, and can't think of a good reason for it failing, excpet a design or coding error. There's no reaon in principle for not allowing the write-by-handle to take place across project boundaries.

    Here's the appliaction: <pre>Sub MyApp()
    ' Write to slave procedure using file name
    Dim strFile As String
    strFile = "C:temperaseme.txt"
    Call slave.WriteToFileName(strFile, "first thing written")
    ' Write to slave procedure using file number
    Dim intFile As Integer
    intFile = FreeFile
    Open strFile For Output As intFile
    Call slave.WriteToFileNumber(intFile, "second thing written")
    Close intFile
    End Sub</pre>


    and here's the slave (library)<pre>Public Function WriteToFileNumber(intFile As Integer, str1 As String)
    Print #intFile, "hello"
    End Function
    Public Function WriteToFileName(strFile As String, str1 As String)
    Dim intFile As Integer
    intFile = FreeFile
    Open strFile For Output As intFile
    Call WriteToFileNumber(intFile, str1)
    Close intFile
    End Function</pre>


  9. #9
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: bad File name or number (PUT statement) (Word

    Just in case you didn't know, the FileSystemObject of the Scripting library has an OpenTextFile method that returns a TextStream object, and the TextStream object has a number of intuitive methods for manipulating the open text file: Close, Read, ReadAll, ReadLine, Skip, etc.

    It might be that TextStream objects aren't subject to the same limitation you've run up against.

  10. #10
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: bad File name or number (PUT statement) (Word

    I suspect that an attempt to pass a numeric index into Project #1's collection of TextStreams over to Project #2 also would fail. However, one could pass an object reference to a particular TextStream, and that has a much better chance of working.

    We used to have a number of posts cautioning that corporate IT might actually remove scripting libraries from PCs to help guard against malware. I'm not sure that's still a likely scenario, but I know that many have shied away from the scripting libraries (and hence the FileSystemObject) for this reason.

  11. #11
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: bad File name or number (PUT statement) (Word

    > all the code for opening,

    I'm still mulling. I suspect that it is the OPEN statement that assigns I/O buffer space, and hence the Open statement "owns" the buffer space, and hence it is the module & project that contains the Open statement that own the buffer space, and hence, a different project, that is, any module in a different project, cannot access that buffer space, hence the problem of passing a file-by-handle rather than file-by-name.

    It still doesn't make a lot of sense to me, because I expect FreeFile to have returned a number that is unique to my session, and that that number - or any number I choose, such as "58", will point to an appropriate block of data that tells all one needs to know about the file, including its buffer space. Maybe it was a programming oversight, or a design limitation, that would not allow the buffer space to be "known" to the data block addressed by the file handle. Hence, "if you tell me only the file handle, it's not enough to get me to the address of the buffer".

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •