Browse for a specific path

T

tjtjjtjt

Is there a way to capture a directory location for use later in a macro?

I'm looking for the ability to create a button similar to the MODIFY...
button on the File Locations tab of the Options dialog box. I will be the
only person who uses this code.

Basically, I want to create a UserForm that I will use to Save my documents
to 3 separate locations simultaneously.
I have a macro that does this already, but I can't change the locations for
different documents. I want to make it more flexible by giving myself the
option to override the default save locations.

Ultimately, I hope to use a naming convention to have documents with
specific prefixes save to specific directories, but I've got a lot of work
and learning to do to get there.

Thanks,
 
T

tjtjjtjt

I suspect I am missing something obvious...
How do I call this in my word procedure and have the file path I browse to
copy into a textbox?
 
D

Doug Robbins - Word MVP

I use the following to load a textbox (txtFldrPath) on a userform with the
folder selected by the user when they click on a Browse button
(btnBrowseforFolder):

Private Sub btnBrowseforFolder_Click()
Dim SH As Shell32.Shell
Dim Fldr As Shell32.Folder
Set SH = New Shell32.Shell
Set Fldr = SH.BrowseForFolder(0, "Select folder in which to save the files",
&H400)
If Not Fldr Is Nothing Then
txtFldrPath.Text = Fldr.Items.Item.Path & "\"
End If
Set Fldr = Nothing
End Sub

It is necessary to have a reference set to the Microsoft Shell Controls and
Automation object library.

Or you can use:

Dim MyPath As String

'let user select a path
With Dialogs(wdDialogCopyFile)
If .Display() <> -1 Then Exit Sub
MyPath = .Directory
End With

'strip quotation marks from path

If Len(MyPath) = 0 Then Exit Sub

If Asc(MyPath) = 34 Then
MyPath = Mid$(MyPath, 2, Len(MyPath) - 2)
End If
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
T

tjtjjtjt

Thanks, Doug. I'll give it a shot.


--
tj


Doug Robbins - Word MVP said:
I use the following to load a textbox (txtFldrPath) on a userform with the
folder selected by the user when they click on a Browse button
(btnBrowseforFolder):

Private Sub btnBrowseforFolder_Click()
Dim SH As Shell32.Shell
Dim Fldr As Shell32.Folder
Set SH = New Shell32.Shell
Set Fldr = SH.BrowseForFolder(0, "Select folder in which to save the files",
&H400)
If Not Fldr Is Nothing Then
txtFldrPath.Text = Fldr.Items.Item.Path & "\"
End If
Set Fldr = Nothing
End Sub

It is necessary to have a reference set to the Microsoft Shell Controls and
Automation object library.

Or you can use:

Dim MyPath As String

'let user select a path
With Dialogs(wdDialogCopyFile)
If .Display() <> -1 Then Exit Sub
MyPath = .Directory
End With

'strip quotation marks from path

If Len(MyPath) = 0 Then Exit Sub

If Asc(MyPath) = 34 Then
MyPath = Mid$(MyPath, 2, Len(MyPath) - 2)
End If
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
T

tjtjjtjt

I have a simple version of this working. Thanks, again - you saved me quite a
headache.
 
J

Jay Freedman

First download the zip and extract it into any folder of your choice. The
programs in the \demo subfolder, especially BDSvr6Demo.exe, show some of the
fancy things it can do.

To get just the basic functionality working on a userform:

- In the VBA editor, go to Tools > References, click the Browse button, and
select ccrpbds6.dll wherever you unzipped it. Then go down the alphabetized
part of the list and check the box next to CCRP BrowseDialog Server. That
will enable both the context-sensitive F1 help in the editor and the
"IntelliSense" popups.

- Put a textbox and a Browse button in your userform, and put some code like
this behind it:

Private Sub cmdBrowse_Click()
Dim BrowseDlg As BrowseDialog
Set BrowseDlg = New BrowseDialog
With BrowseDlg
.RootFolder = "C:\"
.SelectedFolder = "C:\Documents and Settings"
If .Browse Then
TextBox1.Text = .SelectedFolder
End If
End With
End Sub
 
T

tjtjjtjt

Thanks. After I unzipped it (before my follow-up post to you), I could only
get the BDSvr6Demo.exe to run. The Help Files, etc. just gave me errors.
I'll play with it on my home computer and find out what the restrictions are
at work.
 

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