+ Reply to Thread
Results 1 to 19 of 19

open file by clicking selected row in listbox on form

  1. #1
    Forum Contributor
    Join Date
    12-02-2020
    Location
    Asia
    MS-Off Ver
    2010 (Windows 10 64-bit)
    Posts
    856

    open file by clicking selected row in listbox on form

    Hello ,
    I would open file when click selected row in listbox on form .
    I write ID in texbox1 and show data in listbox . when I try to open file by clicking selected row then should match INVOICE no in column (3) in listbox with what is existed within folders and sub folders and sub - sub folders in this directory C:\Users\abgg\Desktop\archive. and when click then should open it .
    the files will be types of files are pdf,excel .
    thanks
    Attached Files Attached Files

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2505
    Posts
    27,272

    Re: open file by clicking selected row in listbox on form

    Opening a file when the row of the listbox is clicked is not hard but we need to know exactly how to map the data in your listbox to the file that is to be opened.

    When I click CommandButton1 the form is shown but there is no data populated. You said, "I write ID in texbox1 and show data in listbox" What is "ID"? There is nothing in the file labeled "ID". You need to give step-by-step instructions with a concrete example.

    How do you determine the file name based on the invoice number in column 3? "the files will be types of files are pdf,excel" Give a specific concrete example of an invoice number and the corresponding file name. Is there only one file per invoice? What if there is both a PDF and an Excel file for the same invoice? What are the extensions for the Excel files?
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Contributor
    Join Date
    12-02-2020
    Location
    Asia
    MS-Off Ver
    2010 (Windows 10 64-bit)
    Posts
    856

    Re: open file by clicking selected row in listbox on form

    There is nothing in the file labeled "ID
    I mean writing brand based on column D.

    How do you determine the file name based on the invoice number in column 3?
    will match the same file name in column 3 with the same file name within directory .
    Is there only one file per invoice?
    yes

    What if there is both a PDF and an Excel file for the same invoice?
    no

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2505
    Posts
    27,272

    Re: open file by clicking selected row in listbox on form

    See those places where I asked for concrete examples? I did not see any in your response.

  5. #5
    Forum Contributor
    Join Date
    12-02-2020
    Location
    Asia
    MS-Off Ver
    2010 (Windows 10 64-bit)
    Posts
    856

    Re: open file by clicking selected row in listbox on form

    in column 3 in listbox =INV-BS-1 and in directory will be excel in folder C:\Users\abgg\Desktop\archive\invoice\INV-BS-1.xlsm
    in column 3 in listbox =INV-BS-2 and in directory will be excel in folder C:\Users\abgg\Desktop\archive\invoice\montly\INV-BS-2.pdf
    actually I don't know if theses examples what you would.

  6. #6
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,301

    Re: open file by clicking selected row in listbox on form

    Does this mean there are only two files total in these directories ?

  7. #7
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2505
    Posts
    27,272

    Re: open file by clicking selected row in listbox on form

    Those are two different folder paths. What dictates which one to use?

    Your other code is using just single letters as all the variable names, which is a very undesirable practice. The code is nearly unreadable. You have also declared only some of your variables.
    I strongly recommend to everyone that they use Option Explicit and declare variables. Doing so prevents a lot of bugs and runtime errors.

    Add the following code to the existing module for UserForm1. This code will work using the first path you gave. If you need to use multiple paths you need to define how to tell which path to use.
    Please Login or Register  to view this content.

  8. #8
    Forum Contributor
    Join Date
    12-02-2020
    Location
    Asia
    MS-Off Ver
    2010 (Windows 10 64-bit)
    Posts
    856

    Re: open file by clicking selected row in listbox on form

    Does this mean there are only two files total in these directories ?
    no this is example . I have multiples files within folders and subfolders and sub-subfolders.

  9. #9
    Forum Contributor
    Join Date
    12-02-2020
    Location
    Asia
    MS-Off Ver
    2010 (Windows 10 64-bit)
    Posts
    856

    Re: open file by clicking selected row in listbox on form

    This code will work using the first path you gave
    thanks
    If you need to use multiple paths you need to define how to tell which path to use.
    the files will be in folders and subfolders and sub-subfolders within main directory.

  10. #10
    Valued Forum Contributor
    Join Date
    05-02-2013
    Location
    Poland
    MS-Off Ver
    Excel 2013
    Posts
    368

    Re: open file by clicking selected row in listbox on form

    Is it so hard to understand what others have written? This is logic!

    You wrote: "and when click then should open it. the files will be types of files are pdf,excel."

    Right now you only have INV-BS-1 and INV-BS-2. In the real file you definitely have e.g. INV-BS-3, ..., INV-BS-10.

    Imagine that I click the line with INV-BS-7. How is the code supposed to know that it should open the file ...invoice\INV-BS-7.XLSM or ...invoice\montly\INV-BS-7.PDF? Flip a coin?

  11. #11
    Valued Forum Contributor
    Join Date
    05-02-2013
    Location
    Poland
    MS-Off Ver
    Excel 2013
    Posts
    368

    Re: open file by clicking selected row in listbox on form

    if "flip a coin" then in post #7 change
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    If there are any other subfolders besides invoice and invoice\month, I will try to provide the code right away
    Last edited by hungt; 05-30-2025 at 04:17 PM.

  12. #12
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2505
    Posts
    27,272

    Re: open file by clicking selected row in listbox on form

    Quote Originally Posted by hungt View Post
    Is it so hard to understand what others have written?
    [Moderating]This is overly harsh. Please be more helpful than critical.[/Moderating]

    How is the code supposed to know that it should open the file ...invoice\INV-BS-7.XLSM or ...invoice\montly\INV-BS-7.PDF? Flip a coin?
    The code I developed (and tested) looked for an Excel file, and if it doesn't find it, it looks for a PDF file. So the code does not have to know a priori which one.

    The harder part is traversing all subfolders.

  13. #13
    Valued Forum Contributor
    Join Date
    05-02-2013
    Location
    Poland
    MS-Off Ver
    Excel 2013
    Posts
    368

    Re: open file by clicking selected row in listbox on form

    Code in UserForm

    Please Login or Register  to view this content.

  14. #14
    Forum Contributor
    Join Date
    12-02-2020
    Location
    Asia
    MS-Off Ver
    2010 (Windows 10 64-bit)
    Posts
    856

    Re: open file by clicking selected row in listbox on form

    The harder part is traversing all subfolders.
    so how avoid this problem?

  15. #15
    Forum Contributor
    Join Date
    12-02-2020
    Location
    Asia
    MS-Off Ver
    2010 (Windows 10 64-bit)
    Posts
    856

    Re: open file by clicking selected row in listbox on form

    Please Login or Register  to view this content.
    I don't want every time change directory when there are files in subfolders.
    should deal with subfolders and sub-subfolder for main directory without change directory every time.

  16. #16
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2505
    Posts
    27,272

    Re: open file by clicking selected row in listbox on form

    Quote Originally Posted by abdo meghari View Post
    so how avoid this problem?
    Quote Originally Posted by abdo meghari View Post
    I don't want every time change directory when there are files in subfolders.
    Please see solution in post #13

  17. #17
    Forum Contributor
    Join Date
    12-02-2020
    Location
    Asia
    MS-Off Ver
    2010 (Windows 10 64-bit)
    Posts
    856

    Re: open file by clicking selected row in listbox on form

    Please see solution in post #13
    doesn't work well !
    sometimes work and sometimes doesn't work especially when change directory !

  18. #18
    Forum Contributor
    Join Date
    01-25-2025
    Location
    Presov, Slovakia
    MS-Off Ver
    2021
    Posts
    297

    Re: open file by clicking selected row in listbox on form

    Hi abdo meghari,
    try replacing "xlsm" with "xls". I didn't find any clear statement in the thread that "excel" files are just "xlsm".
    Please Login or Register  to view this content.
    m.s

  19. #19
    Forum Contributor
    Join Date
    12-02-2020
    Location
    Asia
    MS-Off Ver
    2010 (Windows 10 64-bit)
    Posts
    856

    Re: open file by clicking selected row in listbox on form

    @MSDols
    well done!
    that fixes my problems.
    many thanks for your assistance.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Trying to open a form when one item on a listbox is selected
    By nsharm11 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-15-2020, 03:11 AM
  2. Load an listbox on a form with selected records off a spreadsheet in the same excel file
    By vancouver_ron in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-05-2020, 01:46 PM
  3. [SOLVED] Want to open selected file from listbox (Userform)
    By HaroonSid in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 02-04-2017, 12:34 PM
  4. [SOLVED] Want to open selected file from listbox (Userform)
    By HaroonSid in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-03-2017, 12:47 PM
  5. [SOLVED] Want to open selected file from listbox (Userform)
    By HaroonSid in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-03-2017, 12:45 PM
  6. A file iis found if I open Excel FIRST, but not if I open the file by double-clicking
    By Ed_Collins in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-11-2013, 01:52 AM
  7. Replies: 4
    Last Post: 01-05-2006, 10:25 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1