Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to read a Notepad file using VB6

Status
Not open for further replies.

Hubert38

Programmer
Aug 31, 2013
4
0
0
CA
I have many Notepad files on my computer and intend to open them with a VB code.
Is this possible and if so,how

Your help is appreciated.

Hubert38
 
Like this:
Code:
rtnFileID = FreeFile
Open "C:\My Documents\Notepad File.txt" For Binary Access Read As rtnFileID
Txt$ = Space$(LOF(rtnFileID))
Get #rtnFileID, 1, Txt$
Close rtnFileID
' Txt$ will now contain the contents of the file specified

- Andy
___________________________________________________________________
If a man speaks in a forest and there are no women around to hear him - will he still be wrong?
 
You probably mean a text file.

The way I do it - read it line-by-line:

Code:
Dim strTextLine As String

Open "C:\Temp\MyTextFile.txt" For Input As #1
Do While Not EOF(1)             [green]' Loop until end of file.[/green]
   Line Input #1, strTextLine   [green]' Read line into variable.[/green]

   [green]'do whatever you want with strTextLine here[/green]
Loop
Close #1

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
Or add a richtextbox control and use the LoadFile method
 
All of which can break down, depending on the character encoding and line/paragraph delimiters used.

There really isn't any "Notepad file" format. Notepad handles several encodings, but for your programs to do so you have more work to do.

Double-negatives like [tt]Do While Not EOF()[/tt] are clunky, hard to read, and sort of silly. I'd suggest [tt]Do Until EOF()[/tt] instead.

However in the degenerate case of ANSI or ASCII stream files with CRLF line delimiters any of the approaches above will work. Even ANSI can fail though if the file's codepage isn't the same as your current codepage.

One of the more versatile tools for reading text files is ADO's Stream object.
 
It would be nice to know – instead of how to do it – what do you want to happen, what do you want to accomplish?

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
You can make it really easy on yourself. This will invoke most files
in their default viewer/player.
strDocPath is the full path including file and extension.

Code:
Public Sub OpenDocument(strDocPath As String)

    Dim G As Long
    G = Shell("RUNDLL32.EXE URL.DLL,FileProtocolHandler " & strDocPath, vbNormalFocus)

End Sub






Beir bua agus beannacht!
 
Which isn't what the OP asked for, as far as I can tell.
 
Another version.

On a Form with a Command button, and a TextBox with its Multiline property set to true at design time.

Code:
Private Sub Command1_Click()

    Dim f as integer

    f = FreeFile
    Open "c:\SomeFolder\MyFile.txt" For Input As f
        Text1.Text = Input$(LOF(f), f)
    Close f

End Sub
 
I wonder if the homework question is a bit more specific...

Take Care

Matt
I have always wished that my computer would be as easy to use as my telephone.
My wish has come true. I no longer know how to use my telephone.
 
In my directory I have two kind of files :

1) TextDocuments

2) File

The code Open "c:\MyFile.txt" for input as #1 works for TextDocuments but not for FILE
Text1.Text = Input$( LOF(1),1)
Close #1

However both versions can be opened with Notepad,Wordpad .

I really have no idea what the problem is .

Hubert 38

 
Hello SkipVought

I should mention that I am running VisualBasic 6 on Windows7 and I was reading that VB6 has some issues and problems with
Windows7
Once I have time I will change my O/S back to Windows XP and see if it makes a difference.

My be this is the case since many of my files were created with XP

In the mean time I have another very weird problem :

My Visual Basic program is installed on the C-drive,then I wrote for a command button :

Open "c:\beta" for output as #1
write #1,"Text"
close #1

To my big surprise I did not see the file beta on c but found it on c:\Users\G.Worni\AppData\Local\VirtualStore

How it got there...no idea.

Hubert38
 
You stated...

"The code Open "c:\MyFile.txt" for input as #1 works for TextDocuments but not for FILE"

I asked, "What did you try for FILE that did not work?"

You replied with something that does not seem at all related to your original question in this post. Output vs. Input!
 
>VB6 has some issues and problems with [Windows 7]
>My be this is the case since many of my files were created with XP

The sort of 'problems' that VB6 has on Windows 7 is unrelated to file formats.

Perhaps you could explain what you mean by 'FILE' format documents.


 

Thank you for your help.I converted all "FILE" files to "TEXTDOCUMENTS" and the problem is solved.
I cannot remember what code I used which provided the "FILE" files since it is more than 10 years ago.

 
To my big surprise I did not see the file beta on c but found it on c:\Users\G.Worni\AppData\Local\VirtualStore

How it got there...no idea.

If you don't know then the simple conclusion is that you are not qualified to write programs for Windows.

Microsoft has tried and tried to get programmers up to speed on how things have changed since the DOS days passed, then since the Win9x days passed, and now we're in the Vista era which protects even more of the filesystem and registry from casual tinkering.

The root of the system drive is one such protected location. Don't dump junk files there.

People got away with treating NT as Win9x for a long time by putting all users into the Administrators or Power users groups and relying on appcompat shims. This led to massive criticism for being insecure.

So Microsoft took action by eliminating Power Users, implementing UAC, and expanding System File Protection, among other things. Even this hasn't gone far enough and I suspect we'll see more inter-process isolation when and if NT 7.x ever comes about. The only reason we haven't gotten that yet is the Microsoft faction that wants all programs to be written for the WinRT subsystem.

But the "rules of the road" that we have today have been spelled out many times, beginning back in 2006 when Microsoft's DevReadiness site provided a ton of white papers, presentations, videos, and documentation on the changes that began in Windows Vista.

You snooze, you lose. DevReadiness was taken down years ago.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top