Results 1 to 8 of 8

Thread: Open files in vb5

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Posts
    6

    Open files in vb5

    Down below is the coding I have used in VB 5 to open the files through list box selection. Files do appear in List box but when try to open through command button after selecting in list box an error message appears
    Run time error 424 "object not found"
    When debug, file is coming as it has been selected but practically it is not opening as said above. Kindly advice where problem is?

    Private Sub Command1_Click()
    If Form1.List1.ListIndex < 0 Then
    MsgBox ("SELECT A FILE !!!!!!!!......")
    Else
    'do something....like open a file>
    filename = Form1.List1.List(Form1.List1.ListIndex)
    filename = "c:\ users \ hp \ desktop \ " & filename
    workbooks.open (filename)
    filename = "c:\ users \ hp \ desktop \ " & filename
    doc.open (filename)
    End If
    End Sub

    Private Sub Form_Initialize()
    filename = Dir("c:\users\hp\desktop" & "\*.xls ", vbNormal)
    filename = Dir("c:\users\hp\desktop" & "\*.doc ", vbNormal)
    Do While Len(filename) > 0
    Form1.List1.AddItem filename
    filename = Dir()
    Loop
    End Sub

    Secondly I tried to use "shell" to open the folder on command button

    Private Sub CommandButton_Click
    shell("explorer.exe c:\ users\programfiles\")
    End Sub

    Program files folder opened through above command but when tried with this syntax

    Private Sub CommandButton_Click
    shell("explorer.exe c:\ users\hp\desktop\")
    End Sub

    folder opened but stayed un-visible. When "My computer" icon I have at the bottom clicked, Desktop Folder opened and appeared on the screen. I have no idea why it so?

    Awaiting kind advice?

    Regards

    AM

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Open files in vb5

    Your path is a problem. you have a space before and after each \
    Code:
    filename = "c:\ users \ hp \ desktop \ " & filename
    Should be
    Code:
    filename = "c:\users\hp\desktop\" & filename

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Posts
    6

    Re: Open files in vb5

    Thanks for the reply but still I am getting the same error despite of mistake highlighted by you.

    AM

  4. #4
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    868

    Re: Open files in vb5

    Amir

    Looks like there is a space in these as well

    Code:
    Private Sub CommandButton_Click
        shell("explorer.exe c:\ users\programfiles\")
    End Sub
    
    
    Private Sub CommandButton_Click
        shell("explorer.exe c:\ users\hp\desktop\")
    End Sub
    Spoo

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Open files in vb5

    Code:
    filename = "c:\ users \ hp \ desktop \ " & filename
    doc.open (filename)
    Ok two things,
    1: After you set the filename you should add a debug.print or msgbox to show the file name and path to confirm that it is correct.
    2: where is doc defined and as what?

  6. #6
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Open files in vb5

    As to the issue you are seeing with shell not showing the desired item

    Try adding a , after the last quote there and then look at those nice little options that pop up

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Posts
    6

    Re: Open files in vb5

    Thanks for the guidance. Prob în "shell" syntax by inserting "," is done.
    As regard to "object not found" in file opening through listbox still persist.
    I allowed the variable in " command1" as below
    Dim worddoc as object
    Set worddoc= createobject(" word.application")
    Worddoc.documents.open" c:\users\ hp\desktop\"
    Worddoc.visible= true

    But " object not defined" error is coming.
    Also in my syntax I have used two lines for doc n xls files to appear in list box but only doc files are appearing?
    Rgds
    AM

  8. #8

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Posts
    6

    Re: Open files in vb5

    Hi and morning
    Finally I got the coding how to open files through listbox selection. Below code is working fine but another problem has come up. List box only showing doc documents list and not other, for instance excel files. Code is as

    Private Sub Command1_Click()

    'Word example
    Dim oWord As Object
    Dim oDoc As Object
    'Create the Word application object.
    Set oWord = CreateObject("word.application")
    'Open the sample document.

    foldername = Form1.List1.List(Form1.List1.ListIndex)
    foldername = "c:\users\hp\desktop\" & foldername
    Set oDoc = oWord.Documents.Add(foldername)



    If Form1.List1.ListIndex < 0 Then
    MsgBox ("SELECT A FOLDER !!!!!!!!......")
    Else
    'do something....like open a file>
    'Terminate MS-Word.
    oWord.Visible = True
    'docx.open (filename)
    End If
    End Sub

    Private Sub Form_Initialize()
    filename = Dir("c:\users\hp\desktop" & "\*.xls ", vbNormal)
    filename = Dir("c:\users\hp\desktop" & "\*.doc ", vbNormal)
    Do While Len(filename) > 0
    Form1.List1.AddItem filename
    filename = Dir()
    Loop
    End Sub

    Please tell me how to correct this?
    This is different than above.
    1-----I want to have folders list in listbox
    2----- After selecting the desired folder in the list box, the files in folder to visible when click and
    3----- file to open when click.

    below code is showing the directory but not opening or showing the files available in the folder.
    Where is the mistake?

    Private shlShell As Shell32.Shell
    Private shlFolder As Shell32.Folder
    Private Const BIF_RETURNONLYFSDIRS = &H1

    Private Sub Command1_Click()
    If shlShell Is Nothing Then
    Set shlShell = New Shell32.Shell
    End If
    Set shlFolder = shlShell.BrowseForFolder(Me.hWnd, "Select a Directory", BIF_RETURNONLYFSDIRS)
    If Not shlFolder Is Nothing Then
    MsgBox shlFolder.Title
    End If
    End Sub


    Appreciate your help in this.

    AM

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