Browsing for a file

J

John

I'd like to browse for a filename in Word using VBA. I simply want the
path and filename that I can store in a VBA variable. I don't want to
open or save the file.

There are dialogue boxes that I can call up with VBA that open a file
or save a file, and although they allow me to browse the folder
structures to get to a file, they also open or save a file, which is
what I don't want to do.

Any suggestions?
 
J

Jezebel

Simplest is to use the CommonDialog control. You need to create a UserForm
to put the control on, but you don't need to display the form to use the
control.

You can also show the FileOpen dialog without executing it ... this lets the
user select a file, but doesn't do anything with it.
 
J

Jonathan West

John said:
I'd like to browse for a filename in Word using VBA. I simply want the
path and filename that I can store in a VBA variable. I don't want to
open or save the file.

There are dialogue boxes that I can call up with VBA that open a file
or save a file, and although they allow me to browse the folder
structures to get to a file, they also open or save a file, which is
what I don't want to do.

Any suggestions?

This function will do nicely and work in all versions of Word from 97
onwards


Public Function BrowseFile() As String
With Dialogs(wdDialogFileOpen)
If .Display <> -1 Then
BrowseFile = ""
Else
BrowseFile = WordBasic.FileNameInfo$(.Name, 1)
End If
End With
End Function

It returns the full pathname of the selected file, and a blank string if no
selection is made.



--
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
 

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