Hello
I write stuff to a text file…easy enough, but how do I open it so the user can see it using code? I tried shell but I have a feeling it only works with exe files??????
thanks
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Viewing a text file (vb6)
Classic way to input and manipulate a text file….
Public Sub OpenTextFile(sFileName As String)
Dim sWorkString As String
‘// Open the selected file
Open sFileName For Input As #1
‘// Check that the file contains at least one record
If EOF(1) = True Then
MsgBox “The selected file is empty.” & Chr$(13) & “Please return and select another file”, 16, “Data Import”
Exit Sub
End If
Do While Not EOF(1)
‘// Read the first record and check its format
Line Input #1, sWorkString
‘// Now you can get each line from the text file and store it in the sWorkString variable.
‘// Add code to do what you want with the data
Loop
Close #1
End Sub
(Edited by HansV on 29-Aug-02 13:19. replaced reference to Access window handle.)
If you want to view the file, you can place a RichTextBox control on a form, and use the LoadFile method of this control. The RichTextBox is not on the Toolbox by defalt. Right click the Toolbox and select Components… In the dialog that appears, check Microsoft Rich Textbox Control 6.0 and click OK.
Another way is to use the WinAPI function ShellExecute. This opens a file in its own application (*.doc in Word, *.txt in NotePad, *.xls in Excel etc.) Put the following declarations in a standard module:
Public Declare Function ShellExecute Lib “shell32.dll” Alias “ShellExecuteA” _
(ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
Public Const SW_SHOWNORMAL = 1
Public Const SW_SHOWMAXIMIZED As Long = 3
Use code like this to open a document in its own application:
Dim lngResult As Long
lngResult = ShellExecute(Me.hWnd, “Open”, _
“”, 0&, 0&, SW_SHOWNORMAL)
If lngResult <= 32 Then
MsgBox "Can't open document.", vbExclamation
End If
(replace by the full path and filename of the file you want to display)
In regards to Hans response. That is exactly what I wanted to do, unfortunatly I just found my self using cot and paste with very little understanding(ummmmm…none to be exact) of what was going on, could you perhaps put the code in lamens terms. I don’t see half the byval variables even being used and what is the deal with the whole dim function part. It seems to be way above my head that’s all.
Thanks
That is a Windows API call. If you want more explanation, look in http://www.allapi.net[/url%5D, which offers examples and some information on each of the API calls. Since there are nearly 2000 of them now, people don’t keep the explanations in their heads.
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.
Notifications