PDA

View Full Version : [SOLVED:] getopenfilename path in listbox?



c_skytte
05-03-2022, 06:49 AM
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? :think:



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! : pray2:

snb
05-03-2022, 07:13 AM
Sub M_snb()
with application.filedialog(3)
.initialfilename= "I:\_PUR_PURCHASING\_PUR_\*.pdf"
if .show then ListBox5.AddItem .selecteditems(1)
end with
End Sub

c_skytte
05-03-2022, 09:51 AM
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!

snb
05-03-2022, 12:35 PM
Does the ListBox contain any other values as well ?

c_skytte
05-03-2022, 01:06 PM
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

Paul_Hossler
05-03-2022, 01:26 PM
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?

c_skytte
05-03-2022, 02:07 PM
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 ! ��

Paul_Hossler
05-03-2022, 05:52 PM
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

snb
05-04-2022, 01:31 AM
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

c_skytte
05-04-2022, 02:54 AM
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

snb
05-04-2022, 03:11 AM
Are macros allowed at work ?

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

c_skytte
05-04-2022, 03:22 AM
yes macros are allowed!

29710
29711

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?

snb
05-04-2022, 04:10 AM
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/showthread.php?69862-getopenfilename-path-in-listbox&p=414781&viewfull=1#post414781

c_skytte
05-04-2022, 04:34 AM
sbn!


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

snb
05-04-2022, 04:44 AM
Well, I'm glad it is not one of your idiosyncratic characters, nor the Office version.