|
-
Jul 9th, 2004, 12:28 AM
#1
Thread Starter
Junior Member
opening a file
hello there! i'm a novice programmer in vb and i wanna know how to open a file when i click the file list box control? can anyone also help me in loading the file into the notepad? for example, when i opened a C program, it'll be shown in the notepad in text format. please help, i really need this one...
No matter who you are and where you are, remember we are all living under the same sky.
-
Jul 9th, 2004, 01:11 AM
#2
VB Code:
Function OpenFile(sFileName As String) As String
Dim FF As Integer
Dim sText As String
FF = FreeFile
Open sFileName For Input As #FF
sText = Input(LOF(FF), FF)
Close #FF
OpenFile = sText
End Function
I haven't tried this but I think it'll work As for loading a file in notepad, you need to use either Shell or ShellExecute 
VB Code:
Shell "notepad.exe " & YourFileName
And I don't have the ShellExecute code now...
Has someone helped you? Then you can Rate their helpful post. 
-
Jul 9th, 2004, 02:52 AM
#3
Addicted Member
Here is a nifty ShellandWait module that you can use. Dead easy and it's really good.
I'm to a novice programmer and people here are dead helpfull.
If you ask clear questions, you will get them answered better.
put this in a module
VB Code:
'in the Declarations section of the form/module:
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Const PROCESS_QUERY_INFORMATION = &H400
'The function:
Public Function ShellandWait(ExeFullPath As String, _
Optional TimeOutValue As Long = 0, _
Optional WindowStyle As VbAppWinStyle = vbMinimizedNoFocus) As Boolean
'Run a program + wait for it to finish (max TimeOutValue seconds)
Dim lInst As Long
Dim lStart As Long
Dim lTimeToQuit As Long
Dim sExeName As String
Dim lProcessId As Long
Dim lExitCode As Long
Dim bPastMidnight As Boolean
On Error GoTo ErrorHandler
lStart = CLng(Timer)
sExeName = ExeFullPath
'Deal with timeout being reset at Midnight
If TimeOutValue > 0 Then
If lStart + TimeOutValue < 86400 Then
lTimeToQuit = lStart + TimeOutValue
Else
lTimeToQuit = (lStart - 86400) + TimeOutValue
bPastMidnight = True
End If
End If
lInst = Shell(sExeName, WindowStyle)
lProcessId = OpenProcess(PROCESS_QUERY_INFORMATION, False, lInst)
Do
Call GetExitCodeProcess(lProcessId, lExitCode)
DoEvents
If TimeOutValue And Timer > lTimeToQuit Then
If bPastMidnight Then
If Timer < lStart Then Exit Do
Else
Exit Do
End If
End If
Loop While lExitCode <> 0
ShellandWait = True
Exit Function
ErrorHandler:
ShellandWait = False
End Function
Here's how to use the command
VB Code:
'Wait for Shell to finish before processing rest of files
If Not ShellandWait(fullstring, , vbNormalNoFocus) Then
MsgBox "File: " & fname & " failed to process"
End If
-
Jul 9th, 2004, 06:28 AM
#4
Thread Starter
Junior Member
-
Jul 10th, 2004, 11:37 PM
#5
Thread Starter
Junior Member
Hello there again! I think I have to correct what I've said… almost all of the codes are totally new to me! I've tried your given codes at home guys but I cannot make it work. I'm really an empty skull in VB as for now, all that our Profs have taught us were about basic functions of VB buttons/objects and simple database. I made a program for my case study but isn't complete yet. The problem's still opening the file (after pressing a button) in notepad, textbox or in the actual office where the file resides but we are required to use the notepad. I've done opening the notepad but the content of the file isn't there yet, I want to have the content of the file already opened as the notepad is shown. Here is my code:
Private Sub cmdOpen_Click()
'With Form2
' .Show
' .Caption = Me.dirCompiler.Path & "\" & Me.fileCompiler.Path
'End With
Dim a As Double
a = Shell("c:\windows\notepad.exe", vbNormalFocus)
End Sub
Private Sub drvCompiler_Change()
On Error GoTo Drivehandler
Me.dirCompiler.Path = Me.drvCompiler.Drive
Exit Sub
Drivehandler:
MsgBox "Please insert proper disk to the drive." & vbNewLine & vbNewLine & " Drive unavailable", vbCritical, "Case Study no.1"
Me.drvCompiler.Drive = Me.dirCompiler.Path
Exit Sub
End Sub
Private Sub dirCompiler_Change()
Me.fileCompiler.Path = Me.dirCompiler.Path
Me.txtFilename = Me.fileCompiler.FileName
End Sub
Private Sub fileCompiler_Click()
Me.txtFilename = Me.fileCompiler.FileName
End Sub
How can I squeeze in here the codes you gave me? Uhm, one more question, can I make VB count the words/special characters that the notepad contains? I just hope my English didn't get you confused? Hope you bear with me guys.
No matter who you are and where you are, remember we are all living under the same sky.
-
Jul 11th, 2004, 12:18 AM
#6
include the filename argument
Notepad.exe test.txt
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|