Results 1 to 6 of 6

Thread: opening a file

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2004
    Location
    Beneath the mystical Sky...
    Posts
    17

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

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    VB Code:
    1. Function OpenFile(sFileName As String) As String
    2.  Dim FF As Integer
    3.  Dim sText As String
    4.  FF = FreeFile
    5.  Open sFileName For Input As #FF
    6.   sText = Input(LOF(FF), FF)
    7.  Close #FF
    8.  OpenFile = sText
    9. 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:
    1. Shell "notepad.exe " & YourFileName

    And I don't have the ShellExecute code now...


    Has someone helped you? Then you can Rate their helpful post.

  3. #3
    Addicted Member Chrispybee's Avatar
    Join Date
    Sep 2003
    Location
    North Wales, UK
    Posts
    217
    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:
    1. 'in the Declarations section of the form/module:
    2. Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
    3.       ByVal dwProcessId As Long) As Long
    4. Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
    5. Private Const PROCESS_QUERY_INFORMATION = &H400
    6.  
    7. 'The function:
    8. Public Function ShellandWait(ExeFullPath As String, _
    9.                 Optional TimeOutValue As Long = 0, _
    10.                 Optional WindowStyle As VbAppWinStyle = vbMinimizedNoFocus) As Boolean
    11. 'Run a program + wait for it to finish (max TimeOutValue seconds)
    12.    
    13.     Dim lInst As Long
    14.     Dim lStart As Long
    15.     Dim lTimeToQuit As Long
    16.     Dim sExeName As String
    17.     Dim lProcessId As Long
    18.     Dim lExitCode As Long
    19.     Dim bPastMidnight As Boolean
    20.    
    21.     On Error GoTo ErrorHandler
    22.  
    23.     lStart = CLng(Timer)
    24.     sExeName = ExeFullPath
    25.  
    26.     'Deal with timeout being reset at Midnight
    27.     If TimeOutValue > 0 Then
    28.         If lStart + TimeOutValue < 86400 Then
    29.             lTimeToQuit = lStart + TimeOutValue
    30.         Else
    31.             lTimeToQuit = (lStart - 86400) + TimeOutValue
    32.             bPastMidnight = True
    33.         End If
    34.     End If
    35.  
    36.     lInst = Shell(sExeName, WindowStyle)
    37.    
    38. lProcessId = OpenProcess(PROCESS_QUERY_INFORMATION, False, lInst)
    39.  
    40.     Do
    41.         Call GetExitCodeProcess(lProcessId, lExitCode)
    42.         DoEvents
    43.         If TimeOutValue And Timer > lTimeToQuit Then
    44.             If bPastMidnight Then
    45.                  If Timer < lStart Then Exit Do
    46.             Else
    47.                  Exit Do
    48.             End If
    49.     End If
    50.     Loop While lExitCode <> 0
    51.    
    52.     ShellandWait = True
    53.    
    54. Exit Function
    55.  
    56. ErrorHandler:
    57.   ShellandWait = False
    58.  
    59. End Function

    Here's how to use the command

    VB Code:
    1. 'Wait for Shell to finish before processing rest of files
    2.      If Not ShellandWait(fullstring, , vbNormalNoFocus) Then
    3.          MsgBox "File: " & fname & " failed to process"
    4.      End If



  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jul 2004
    Location
    Beneath the mystical Sky...
    Posts
    17
    just back from school. oh, thank you so much guys! i haven't tried this at home yet but thanks in advance! some of the codes are new to me. thanks again! muahhh!
    No matter who you are and where you are, remember we are all living under the same sky.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jul 2004
    Location
    Beneath the mystical Sky...
    Posts
    17
    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.

  6. #6
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    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
  •  



Click Here to Expand Forum to Full Width