INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

How to read a Notepad file using VB6

How to read a Notepad file using VB6

(OP)
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

RE: How to read a Notepad file using VB6

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?

RE: How to read a Notepad file using VB6

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)             ' Loop until end of file.
   Line Input #1, strTextLine   ' Read line into variable.

   'do whatever you want with strTextLine here
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.

RE: How to read a Notepad file using VB6

Or add a richtextbox control and use the LoadFile method

RE: How to read a Notepad file using VB6

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 Do While Not EOF() are clunky, hard to read, and sort of silly. I'd suggest Do Until EOF() 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.

RE: How to read a Notepad file using VB6

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.

RE: How to read a Notepad file using VB6

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 --> VB6

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!

RE: How to read a Notepad file using VB6

Which isn't what the OP asked for, as far as I can tell.

RE: How to read a Notepad file using VB6

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 

RE: How to read a Notepad file using VB6

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.

RE: How to read a Notepad file using VB6

(OP)
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

RE: How to read a Notepad file using VB6

What did you try for FILE that did not work?

RE: How to read a Notepad file using VB6

(OP)
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

RE: How to read a Notepad file using VB6

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!

RE: How to read a Notepad file using VB6

>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.


RE: How to read a Notepad file using VB6

(OP)

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.

RE: How to read a Notepad file using VB6

Quote:

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close