VBA Word: How to point to a specific dir when a file dialog opens up?

S

sharon3874

in VBA word, when I right click on some text, I can add hyperlinks upon
the text. It will open up a file dialog(window) allowing me to navigate
and pick a file. My question is that how I can force the dialog box to
point to a certain directory when it opens up?

Thanks a lot.
 
D

Doug Robbins - Word MVP

Create a macro with the name InsertHyperlink() and it will then run when you
right click and select Hyperlink.

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

Greg Maxey

Doug,

I think what Sharon is trying to do is create a macro that displays the
InsertHyperlink dialog with a specific "look in" folder selected.

If it is possible, I assume it would start out something like:

Sub test()
With Dialogs(wdDialogInsertHyperlink)
.Name = "C:\" 'generates error
 
D

Doug Robbins - Word MVP

Hi Greg,

There is something strange here.

While the following code does intercept the command that is run by selecting
Hyperlink from the Right Click Menu, as the Message Box is displayed, when
it is run that way, the ChangeFileOpenDirectory command is ignored. If
however the macro is run from Tools>Macro>Macros, then the
ChangeFileOpenDirectory command is taken into account:

Sub InsertHyperlink()
Dim BrowseFile As String
MsgBox "OK"
ChangeFileOpenDirectory "c:\Contract Documents\"
With Dialogs(wdDialogFileOpen)
If .Display <> -1 Then
Exit Sub
Else
BrowseFile = WordBasic.FileNameInfo$(.Name, 1)
End If
End With
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, _
Address:=BrowseFile, TextToDisplay:=Selection.Range.Text
End Sub

It's also a bit flaky in other ways.

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

Jonathan West

Doug Robbins - Word MVP said:
Hi Greg,

There is something strange here.

While the following code does intercept the command that is run by
selecting Hyperlink from the Right Click Menu, as the Message Box is
displayed, when it is run that way, the ChangeFileOpenDirectory command is
ignored. If however the macro is run from Tools>Macro>Macros, then the
ChangeFileOpenDirectory command is taken into account:

That may well be down to a timing issue. Word VBA occasionaly exhibits this
kind of problem for no immediately obvious reason.

It can often be addressed by putting a couple of DoEvents commands at a
strategic point in the macro, in this case probably immediately below the
ChangeFileOpenDirectory line

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