File Display in VB6

Viki Posted messages 34 Status Member -  
 Anonymous user -
Bonjour,
I am developing a small application and I want to know how we can open and display any file from code in VB.

I was only able to open it with the command:
Open "location" For Output As #0

But I can't manage to display it.

Thank you for your help.

9 answers

Viki Posted messages 34 Status Member 6
 
Thank you, but I don't get anything displayed.
Actually, what I wanted is not to display the content of a file but to display the file itself and see its content.

Otherwise, your code works well but specifies a text area in which we must visualize the content of the file.

Here is your code to which I added 1 line:

Dim Valeur As Variant Dim nomFichier As String Dim numFic As Integer numFic = FreeFile nomFichier = "C:\test1.doc" Open nomFichier For Input Shared As #numFic Do While Not EOF(numFic) Input #numFic, Valeur Text1.Text = Text1.Text & vbCrLf & Valeur Loop Close #numFic

If there was a way to directly display the .doc file, let me know

Thank you.
5
Anonymous user
 
Hello,

Some clarifications:

Open "FileName" For mode [AccessRestriction] [LockType] As # file number
------------------------------------------------------------------------------------------------------------------------------------------
Mode Description

Append Opens a sequential file, starting at the end.
Data will be appended to the end of the existing data. If the file does not exist, Visual Basic creates it.

Binary Opens a file in binary access.

Input Opens a sequential file for reading, starting from the beginning.
Data is read in the order in which it was sent to the file.
If the file does not exist, Visual Basic displays a runtime error message.

Output Opens a sequential file for writing, starting from the beginning.
If the file does not exist, Visual Basic creates it. If it exists, it is overwritten (old data will be erased).

Random Opens a file for random reading and writing.
In this mode, data can be read and written in any order.
------------------------------------------------------------------------------------------------------------------------------------------
Access restrictions:
This optional argument (AccessRestriction) allows you to restrict the execution of the
Open statement to Read (reading), Write (writing), or Read Write (reading-writing) access modes.
This argument is mainly applied to files that circulate over a network. In read access
(Read), you can only read the file but not modify it. Write access
(Write) allows for modification of the file, and read-write access (Read Write) allows for both.
Generally, files that are intended to circulate over a network already have their access rights established.
The network administrator grants these rights. This is why the (AccessRestriction) argument is not mandatory.

File locking:
The other optional argument, LockType, specifies the operations that other processes can
perform on the file. Again, this argument is mainly used in network applications.
It restricts file access to one user at a time. LockType can take
four values: Shared, Lock Read, Lock Write, and Lock Read Write. The Shared value allows
all users to access the file simultaneously. Lock Read locks the file, limiting reading to one user.
Lock Write does the same for writing. Lock Read Write locks the file by preventing all other users from reading or writing to it.

Lupin
--
~What is essential is invisible to the eyes~
~One sees clearly only with the heart~
1
Anonymous user
 
re:

Locate an available file number

In the Access Modes section, there are four different examples for the Open statement.
You have probably noticed that each example opens a file under a different number.
You also know that when the file number is known, Visual Basic does not access the file by its name anymore, but by the file's specific number. Visual Basic supports the simultaneous opening of multiple files, provided that each file is assigned a different number.
If your application needs multiple files, you must be able to determine the next available number, especially if files are opened in a function that has no way of knowing if other functions have opened files. There is a function called FreeFile() which returns the next available number. This function ensures that the number returned is not used in another statement. Its syntax is: FreeFile([intRangeNumber]).

The argument "intRangeNumber" is optional. It specifies the range within which the returned number should be included: 1 to 255 or 256 to 511. In the absence of this argument, the default range is 1 to 255.
It is very rare for a program to need to simultaneously open more than 256 files. This is why this argument is optional, and if not specified, the parentheses are unnecessary. The following statement provides an example of using this function: intFileNum = FreeFile

Open "MyFile.dat" For Output As intFileNum

Thus, you can be assured of having a unique file number. Make it a habit to assign FreeFile to a variable as in the example. This way, you will have a means of knowing the number returned by the function.

Look at the following statement:
Open "MyFile.dat" For Output As FreeFile()

This statement works, but you have no way of knowing the file number for later use in your program. An open file must be closed.

Lupin

--
~What is essential is invisible to the eye~
~One sees clearly only with the heart~
1
hourrrah Posted messages 6976 Status Member 515
 
Hi Viki
read so that it can be read; print so that it can be printed (in VB..)
But if it is about running a program written in VB, you need the appropriate runtimes.
0
Viki Posted messages 34 Status Member 6
 
I don't really see the syntax that needs to be used for the "Read" function

thank you for clarifying.
0
Viki Posted messages 34 Status Member 6
 
Thank you for these clarifications, but:
Open "C:\test1.doc" For Input Read Shared As #1
gives me an error "Expected: As"
and when I do:
Open "C:\test1.doc" For Input As #1 Read Shared
it gives me an error: "Syntax error"

I don't know anymore!!
0
Viki Posted messages 34 Status Member 6
 
Thank you for clarifying, but when I do, I get this error message:

For:
Open "C:\test1.doc" For Input Read Shared As #1


The error message is: Expected: As;

And when I do:
 Open "C:\test1.doc" For Input As #1 Read Shared


The error message is: Syntax error;

Here is what seems strange to me.
0
Anonymous user
 
re:

2 points :

1.) First point

There is a syntax error on the line:

 ' Open "C:\test1.doc" For Input Read Shared As #1 


Reference => Message #3
File Locking:
The other optional argument, LockType, specifies the operations that other processes can perform on the file. Again, this argument is mostly used in network applications. It allows restricting access to the file to a single user at a time. LockType can take four values: Shared, Lock Read, Lock Write, and Lock Read Write. The Shared value allows all users to access the file simultaneously. Lock Read locks the file, limiting reading to a single user. Lock Write does the same for writing. Lock Read Write locks the file, prohibiting all other users from reading or writing to it.

Possible choices are:

 Open "C:\test1.doc" For Input Shared As #1 Open "C:\test1.doc" For Input Lock Read As #1 Open "C:\test1.doc" For Input Lock Write As #1 Open "C:\test1.doc" For Input Lock Read Write As #1 


2.) Second point

Reference => Message #5

It is better to use a variable as the file number.

Example:
 Private Sub cmdLire_Click() Dim Valeur As Variant Dim nomFichier As String Dim numFic As Integer numFic = FreeFile nomFichier = "C:\test1.doc" ' Open "C:\test1.doc" For Input Read Shared As #1 Open nomFichier For Input Shared As #numFic Do While Not EOF(numFic) Input #numFic, Valeur Loop Close #numFic End Sub 


Is it clearer now?

Lupin

--
~What is essential is invisible to the eye~
~One sees clearly only with the heart~
0
Anonymous user
 
Hello again,

Actually, I’ve never had such a need, and I can’t remember anymore,
I remember having implemented an image viewer, but
I’m not sure if I had documentation on this subject. If I find it then
I’ll let you know.

If I were faced with this need, I would code in VB and at the time
to view documents, I would pass it to VBA under Word.

Lupin

--
~The essential is invisible to the eye~
~One sees clearly only with the heart~
0