Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using VBA to Browse for a file using a dialog

2 views
Skip to first unread message

Jan T.

unread,
Dec 4, 2009, 4:30:38 PM12/4/09
to
Hi.
I have Access 2000 and Word 2007 home edition and I want to open or
browse for a file with using a dialog to return the file path.

In Excel, I have used this;
strFilePath = Application.GetOpenFilename("Excel Files (*.xls),*.xls")
or....
Function File_2_Open() As String
Dim strVar As Variant
On Error Resume Next
strVar = Application.GetOpenFilename("Excel Files (*.xls),*.xls")
Workbooks.Open strVar
' deal with it... your code here...
File_2_Open = strVar
End Function

This .GetOpenFilename results in a error;
Compile error: Method or data member not found
or...

Dim strFilePath As String
Dim objWord As Word.Document
Dim V As Variant
On Error Resume Next

'Allow the user to select the Word document
With Application.FileDialog(msoFileDialogOpen)
.Title = "Select Word Document"
.AllowMultiSelect = False
.Filters.Add "Word Documents", "*.docx; *.doc", 1
.Filters.Add "All Files", "*.*", 2

'Show the dialog and if returned True, use the selected file
If .Show = True Then 'The user clicked "OK"
strFilePath = .SelectedItems(1)

The last code snippet returns an error from msoFileDialogOpen; Variable not
defined?

So, neither of the examples above will work. What method can I use here to
return the file path?

Thank you for any help!

Regards Jan T.


Ken Snell

unread,
Dec 4, 2009, 11:20:59 PM12/4/09
to
See the code at http://www.mvps.org/access/api/api0001.htm
--

Ken Snell
http://www.accessmvp.com/KDSnell/


"Jan T." <noreply> wrote in message
news:edJEHkSd...@TK2MSFTNGP05.phx.gbl...

Jan T.

unread,
Dec 5, 2009, 7:51:11 PM12/5/09
to
Thank you very much for your answer. This could be the solution in my
program.

Regards
Jan T.

"Ken Snell" <kthsne...@ncoomcastt.renaetl> skrev i melding
news:uYCkYJWd...@TK2MSFTNGP05.phx.gbl...

Albert D. Kallal

unread,
Dec 15, 2009, 8:27:57 AM12/15/09
to
You can also consider:

Dim f As FileDialog
Set f = Application.FileDialog(msoFileDialogFilePicker)
f.Show
MsgBox "file choose was " & f.SelectedItems(1)

You can late bind if you wish:

above needs: Microsoft Object 11.0 Object library

If you don't want the above reference, then the following
code will work without any references:


Dim f As Object
Set f = Application.FileDialog(3)
f.AllowMultiSelect = True
f.Show

MsgBox "file choosen = " & f.SelectedItems.Count


--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pleaseNOO...@msn.com


David W. Fenton

unread,
Dec 16, 2009, 11:34:21 PM12/16/09
to
"Albert D. Kallal" <PleaseNOOO...@msn.com> wrote in
news:e4i#rpYfKH...@TK2MSFTNGP05.phx.gbl:

> You can also consider:
>
> Dim f As FileDialog
> Set f = Application.FileDialog(msoFileDialogFilePicker)
> f.Show
> MsgBox "file choose was " & f.SelectedItems(1)
>
> You can late bind if you wish:
>
> above needs: Microsoft Object 11.0 Object library
>
> If you don't want the above reference, then the following
> code will work without any references:
>
>
> Dim f As Object
> Set f = Application.FileDialog(3)
> f.AllowMultiSelect = True
> f.Show
>
> MsgBox "file choosen = " & f.SelectedItems.Count

I fail to perceive the utility in declaring a weakly-typed object
variable here as opposed to just using a WITH block, i.e.:

With Application.FileDialog(3)
.AllowMultiSelect = True
.Show

MsgBox "file choosen = " & .SelectedItems.Count
End With

I've never used the FileDialog object, so maybe I'm missing
something here.

--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/

0 new messages