calling the "browse" dialog from with Word Macro

B

BRC

I have a word dialog that I wrote in Vb that displays a dialog box where
user can set the path(s) to different form letters they use. Currently the
dialog box has 5 textboxes that user can type paths into. What i would like
to do is call the "browse" dialog and return the selected path to the
textbox. Can anyone steer me to something that might help. Thanks in
advance for any ideas. BRC
 
L

Lars-Eric Gisslén

You can use the following code to display the BrowseForFolder dialog:
(Include a reference to 'Microsoft Shell Controls and Automation' in your
VBA project)

Dim oShell As Shell32.Shell
Dim oFolder As Shell32.Folder

Set oShell = New Shell32.Shell
Set oFolder = oShell.BrowseForFolder(0, "Select a folder for your
files", 0)

MsgBox oFolder.Self.Path

Set oFolder = Nothing
Set oShell = Nothing

Regards,
Lars-Eric
 
B

BRC

This seems to be a pretty easy way to do this but i am getting an error it
the message box line
"Run-time error 438 object doesn't support this property or method" The
browse function seems to work fine but i can't return a reference to the
folder i select. thanks again for your help
BRC
 
L

Lars-Eric Gisslén

Ok,

That is the AxtiveX interfaces to the underlying Win API functions in a
nutshell. Not 100% reliable! Ok, no reason to give up. Next step would be to
call Win API directly, but, try this code first:
'-------------------------------------
Sub Test()
MsgBox GetFolderName("Choose a folder")
End Sub

Function GetFolderName(sCaption As String) As String
Dim oShell As Shell32.Shell
Dim oFolder As Shell32.Folder
Dim oItems As Shell32.FolderItems
Dim Item As Shell32.FolderItem

On Error GoTo CleanUp

Set oShell = New Shell ' ActiveX interface to shell32.dll
Set oFolder = oShell.BrowseForFolder(0, sCaption, 0)
Set oItems = oFolder.Items
Set Item = oItems.Item

GetFolderName = Item.Path

CleanUp:
Set oShell = Nothing
Set oFolder = Nothing
Set oItems = Nothing
Set Item = Nothing

End Function
'-------------------------------------

Regards,
Lars-Eric
 
B

BRC

I havn't tried your 2nd suggestion but I did find that by simply removing
the ".Self.Path" from the "oFolder.Self.Path" in the first example you gave
me, I was able to return the path in a msgbox. so i am sure i can make that
work once i have time to play with it a little. Thanks for the great input.
 
L

Lars-Eric Gisslén

BRC,

Are you on Windows 98?

Self is only supported in shell32.dll version 5.0 or later - Win 2000/ME and
later.
My second example should work with no problems.

Regards,
Lars-Eric
 

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