Access another file from Word without opening the file

R

Ryan Jamison

I have probably a simple question, but I am not quite sure how to go about it.

I am using two documents, one to be edited and one that supplies the
information to edit the first. I'm trying to figure out how to run the file
browser dialog box allowing users to select the second file, but I do not
want to open the file, i.e. I want to return the file path and name of the
second file to strings without actually seeing/opening the second file.

Thanks!

Ryan
 
J

Jay Freedman

Ryan said:
I have probably a simple question, but I am not quite sure how to go
about it.

I am using two documents, one to be edited and one that supplies the
information to edit the first. I'm trying to figure out how to run
the file browser dialog box allowing users to select the second file,
but I do not want to open the file, i.e. I want to return the file
path and name of the second file to strings without actually
seeing/opening the second file.

Thanks!

Ryan

Instead of calling Dialogs(wdDialogFileOpen).Show, which does open the
selected file, do this:

Dim sFileName As String

With Dialogs(wdDialogFileOpen)
If .Display = -1 Then
sFileName = .Name
End If
End With

If Len(sFileName) > 0 Then
' user didn't cancel

' get full path and name
sFileName = WordBasic.FileNameInfo$(sFileName, 1)

' use sFileName to access file
End If

The Display method shows the dialog and lets the user select a file, but it
doesn't do anything other than return that information to the macro. The
method returns the value -1 if the user clicked the Open button or
double-clicked the file in the dialog, or 0 if they clicked Cancel or press
Esc.

The dialog's .Name property returns the selected file's name, but not its
folder path. To get that, use the FileNameInfo method -- see
http://www.word.mvps.org/FAQs/MacrosVBA/WordBasicCommands.htm.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
D

Doug Robbins - Word MVP

Use

Set fd = Application.FileDialog(msoFileDialogFilePicker)
Dim filetouse as string
fd.Title = "Select the File"
fd.Filters.Add "Word Documents", "*.doc; *.docx", 1
Dim vrtSelectedItem As Variant
With fd
If .Show = -1 Then
filetouse = .SelectedItems(1)
End If
End With
Set fd = Nothing
MsgBox filetouse

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

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