Returning a File Path

P

Pam

How do you return a file path from a file picker box back to a label on
a user form instead of a message box?
 
T

Tony Logan

Hi, Pam. To get the file path (and other file info), I use a File System
Object. To use it, you'll have to set a reference in the VB Editor (Tools >
References) to "Microsoft Scripting Runtime".

I use the file system object a lot when presenting users with a dialog box
for selecting files, and when I want to know what folder that file is in.
Here's a code sample; I use this code specifically to provide users with a
dialog box in Word that lets them open PowerPoint files.

Probably what you're looking for are one of these two lines, which are in
the code:
AbsolutePath = fso.GetAbsolutePathName(vrtItem)
PathNoFile = fso.GetParentFolderName(vrtItem)


Hope this points you in the right direction:

Dim fso As New FileSystemObject ' provides access to the computer's file
system
Dim fd As FileDialog
Dim vrtItem As Variant

With fd
.Title = "Select PowerPoint File"
.AllowMultiSelect = False
.Filters.Add "PowerPoint files", "*.ppt", 1
.FilterIndex = 1
.InitialFileName = sDocPath
.InitialView = msoFileDialogViewList
If .Show = -1 Then
For Each vrtItem In .SelectedItems
AbsolutePath = fso.GetAbsolutePathName(vrtItem)
PathNoFile = fso.GetParentFolderName(vrtItem)
Next vrtItem
Else
Exit Function
End If
End With
 
G

Greg

Tony,

Looks interesting.

Dim fd As FileDialog

causes runtime error "User defined type not defined"

How do you proceed?
 
T

Tony Logan

Hi, Greg.

Here's the complete code sample that will display the dialog box and use the
File Scripting Object code I was talking about:

Sub DialogBox()

Dim fso As New FileSystemObject
Dim fd As FileDialog
Dim vrtItem As Variant
Dim sDocPath As String
Dim AbsolutePath As String ' path plus full filename
Dim PathNoFile As String ' path, no filename

sDocPath = ActiveDocument.Path & Application.PathSeparator
Set fd = Application.FileDialog(msoFileDialogOpen)

With fd
.Title = "Select PowerPoint File"
.AllowMultiSelect = False
.Filters.Add "PowerPoint files", "*.ppt", 1
.FilterIndex = 1
.InitialFileName = sDocPath
.InitialView = msoFileDialogViewList
If .Show = -1 Then
For Each vrtItem In .SelectedItems
AbsolutePath = fso.GetAbsolutePathName(vrtItem)
PathNoFile = fso.GetParentFolderName(vrtItem)
Next vrtItem
Else
Exit Sub
End If
End With
End Sub
 
G

Greg

Tony,

No joy. Still get error User defined object not defined on this line

Dim fd As FileDialog
 
J

Jay Freedman

Hi Greg,

Did you set a reference to the Scripting Runtime, as Tony mentioned in his
first paragraph? That points into a DLL that supplies the definitions of
FileDialog and other scripting objects.

Jay
 
J

Jonathan West

Greg said:
Tony,

No joy. Still get error User defined object not defined on this line

Dim fd As FileDialog

Also, which version of Office are you running? The FileDialog object was
introduced in Office XP IIRC. It certainly isn't in Office 2000.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
G

Greg Maxey

Jay, yes I did.

Jonathan, your answer probably explains it. I was trying at work with
Office2000. Will try here shortly.

Thanks.
 
P

Pam

Tony,

I have tried the code but I still can not see the path on the user
form. I want the file path to show within a label--so does the code go
behind the label or can I refer to it by name? If I do that option how
is it activated? I am working in Word and I have a command button to
activate the code.

Could you help me further.

Thanks

Pam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top