Consulting

Results 1 to 15 of 15

Thread: getopenfilename path in listbox?

  1. #1
    VBAX Regular
    Joined
    Dec 2021
    Posts
    42
    Location

    getopenfilename path in listbox?

    Hi,

    I'm just trying to get the path of the selected file (by use of getopenfilename) shown in listbox5... why will it not work?

    Sub Get_Data_From_File()
        Dim sFullName As String
        Dim sFileName As String
        
        ChDrive "I:"
        ChDir "I:\_PUR_PURCHASING\_PUR_"
        
    
    
        sFullName = Application.GetOpenFilename("*.pdf,*.pdf")
        sFileName = Dir(sFullName)
    
    
        'Debug.Print sFullName, sFileName
        ListBox5.AddItem sFullName(sFullName + 1)
    
    
    End Sub
    thanks!

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,635
    Sub M_snb()
      with application.filedialog(3)
        .initialfilename= "I:\_PUR_PURCHASING\_PUR_\*.pdf"
        if .show then ListBox5.AddItem .selecteditems(1)
      end with
    End Sub

  3. #3
    VBAX Regular
    Joined
    Dec 2021
    Posts
    42
    Location
    Thanx... it works great, snb! ...but I would never have figured that out!

    One small request,
    would it be possible only to show the filename, BUT still remember the path (and filename)?
    If not, i'll stick with the solution above!

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,635
    Does the ListBox contain any other values as well ?

  5. #5
    VBAX Regular
    Joined
    Dec 2021
    Posts
    42
    Location
    No it does not... it should only load a single pdf file... but this file differs all the time.
    The file always starts with the letters PO and then followed by 6 digits like in PO232542.pdf

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    Why use a Listbox? Seems like a Label control would be better to display it since it doesn't sound like you're selecting something from a one item Listbox?
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  7. #7
    VBAX Regular
    Joined
    Dec 2021
    Posts
    42
    Location
    Im going to attach the file to a mail at the end. So actually no selection is needed in this particular listbox.

    I'd like to pick the file from the server with the path... and the coding for attaching the file from a listbox i've performed several times.


    But other suggestions are welcome ! ��

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    I'd have thought that once you had the file name

    sFullName = Application.GetOpenFilename("*.pdf,*.pdf")
    you could just that for attaching sFullName to an email

    No need to use a ListBox
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  9. #9
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,635
    with application.filedialog(3)
      .initialfilename= "I:\_PUR_PURCHASING\_PUR_\*.pdf"
      if .show then c00= .selecteditems(1)
    end with
    
    With createobject("outlook.application").createitem(0)
       .to = "email@adres"
       if c00 <>"" then .attachments.add c00
       .send
    end with

  10. #10
    VBAX Regular
    Joined
    Dec 2021
    Posts
    42
    Location
    Hi snb,


    I'm a bit confused.
    I tried the code you provided yesterday at home... with a 100% success rate!
    Here at work I showed one of my colleagues the coding you made... but it just bugs out!
    Do you know why?


    the Excel version here at work is Microsoft® Excel® for Microsoft 365 MSO (16.0.14326.20850) 32-bit
    at home its 365 too (but i guess) it's a 64-bit version? ...but does that change anything?


    Or should I activate some kind of VBA (tools) References... to make your splendid code working?


    Thankx, from a newbe

  11. #11
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,635
    Are macros allowed at work ?

    Which line errors out ?
    What message ?
    If you find any 'option Explicit', remove it.

  12. #12
    VBAX Regular
    Joined
    Dec 2021
    Posts
    42
    Location
    yes macros are allowed!

    2022-05-04 12_15_33-Microsoft Visual Basic for Applications - Kopi af Ombygning - SDD v3.xlsm [b.jpg
    2022-05-04 12_16_01-Microsoft Visual Basic.jpg

    cant find any option Explicit in the code..? (dont know if i can search for "option Explicit" somewhere?)

    BTW, tha path is change from
    I:\_PUR_PURCHASING\_PUR_ to I:\_PUR_PURCHASING\_PUR_Indkøbsordre\
    maybe the danish letter "ø" is the reason for the error?

  13. #13
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,635
    No, there is no Listbox5
    But as we have pointed out earlier: you don't need a Listbox.

    So, use the code in http://www.vbaexpress.com/forum/show...l=1#post414781

  14. #14
    VBAX Regular
    Joined
    Dec 2021
    Posts
    42
    Location
    sbn!


    I just realized i placed the code in a module!! !!!
    After i moved the code to under the userform itself it worked just perfect!!
    ...i'm very, very sorry about that huge mistake!!!!

  15. #15
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,635
    Well, I'm glad it is not one of your idiosyncratic characters, nor the Office version.

Posting Permissions

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