View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
joel joel is offline
external usenet poster
 
Posts: 9,101
Default Error 59 Bad record length

Your file is probably a binary file. I included the VBA help instruction for
the OPEN and INPUT functions. I would try to open the file in BINARY mode.
Ther are tips in the help below on using LEN with Binary files.
----------------------------------------------------------------------------------------------
Open Statement


Enables input/output (I/O) to a file.

Syntax

Open pathname For mode [Access access] [lock] As [#]filenumber [Len=reclength]

The Open statement syntax has these parts:

Part Description
pathname Required. String expression that specifies a file name €” may
include directory or folder, and drive.
mode Required. Keyword specifying the file mode: Append, Binary, Input,
Output, or Random. If unspecified, the file is opened for Random access.
access Optional. Keyword specifying the operations permitted on the open
file: Read, Write, or Read Write.
lock Optional. Keyword specifying the operations restricted on the open file
by other processes: Shared, Lock Read, Lock Write, and Lock Read Write.
filenumber Required. A valid file number in the range 1 to 511, inclusive.
Use the FreeFile function to obtain the next available file number.
reclength Optional. Number less than or equal to 32,767 (bytes). For files
opened for random access, this value is the record length. For sequential
files, this value is the number of characters buffered.



Remarks

You must open a file before any I/O operation can be performed on it. Open
allocates a buffer for I/O to the file and determines the mode of access to
use with the buffer.

If the file specified by pathname doesn't exist, it is created when a file
is opened for Append, Binary, Output, or Random modes.

If the file is already opened by another process and the specified type of
access is not allowed, the Open operation fails and an error occurs.

The Len clause is ignored if mode is Binary.

Important In Binary, Input, and Random modes, you can open a file using a
different file number without first closing the file. In Append and Output
modes, you must close a file before opening it with a different file number.

----------------------------------------------------------------------------------------------
Input Function


Returns String containing characters from a file opened in Input or Binary
mode.

Syntax

Input(number, [#]filenumber)

The Input function syntax has these parts:

Part Description
number Required. Any valid numeric expression specifying the number of
characters to return.
filenumber Required. Any valid file number.



Remarks

Data read with the Input function is usually written to a file with Print #
or Put. Use this function only with files opened in Input or Binary mode.

Unlike the Input # statement, the Input function returns all of the
characters it reads, including commas, carriage returns, linefeeds, quotation
marks, and leading spaces.

With files opened for Binary access, an attempt to read through the file
using the Input function until EOF returns True generates an error. Use the
LOF and Loc functions instead of EOF when reading binary files with Input, or
use Get when using the EOF function.

Note Use the InputB function for byte data contained within text files.
With InputB, number specifies the number of bytes to return rather than the
number of characters to return.


"Per" wrote:


--
Per


"Joel" skrev:

I got the code to work by commented out the chdir. I did not get the same
error you had. I think the problem is with the permnissions of the directory
you are using. I would add the following code to help locate the problem


msgbox(Application.Path) <= find out which path you are using.
ChDir IndataKatalog


For some reason I don't understand it doesn't work on my computer. I have
modified the program code so that I can read the file from the same directory
as the workbook is stored in and also in the same directory as the
msgbox(Application.Path) shows but nothing seems to help.

Per