Open Word from Access

G

Guest

Office 2003 Pro. What is the command to open a Word Document ( test.doc)
from inside Access? The document is in the same folder as the database.
Have looked in the Help and KB files, no luck.
Please help.
Thanks.
 
G

Guest

This works for me.

Dim RetVal
Dim stDocName as String

stDocName = "[Word Doc path and filename]"
RetVal = Shell("winword.exe " & stDocName, 1)

Notes: 1) there is no info after RetVal in the Dim statement (don't know why
- someone else gave this to me); 2) note the space after "exe" before the
quotation mark. This command line is exactly what you would type in the
Target line of a shortcut to open a specific Word document from your desktop.

If you want to give the user the option to select a Word document (perhaps
in setting up merge documents with a database export text file), then
substitute explorer.exe for winword.exe and then change stDocName to
represent only a path/directory name with no specific file.

Have fun!
 
D

Douglas J. Steele

The Shell function officially returns a Variant (Double), so you could use

Dim RetVal As Variant

What you've shown will only work if there are no embedded spaces in the
path. To be able to handle paths with embedded spaces, you should use:

RetVal = Shell("winword.exe """ & stDocName & """", 1)

That's three double quotes after winword.exe, and four double quotes after
stDocName.

To allow the user to choose a file and work with it, see
http://www.mvps.org/access/api/api0001.htm at "The Access Web". Copy
everything between Code Start and Code End and paste it into a new module
(not a class module, nor a module associated with a form or report). When
you save the module, make sure its name isn't the same as any routines saved
in that module (or any other module). Once that's done, you can then use:

Dim RetVal As Variant
Dim stDocName as String
Dim strFilter As String

strFilter = ahtAddFilterItem(strFilter, "Word Files (*.DOC)", "*.DOC")
stDocName = ahtCommonFileOpenSave( _
Filter:=strFilter, OpenFile:=True, _
DialogTitle:="Please select an input file...", _
Flags:=ahtOFN_HIDEREADONLY)

If Len(stDocName) > 0 Then
RetVal = Shell("winword.exe """ & stDocName & """", 1)
End If

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Pendragon said:
This works for me.

Dim RetVal
Dim stDocName as String

stDocName = "[Word Doc path and filename]"
RetVal = Shell("winword.exe " & stDocName, 1)

Notes: 1) there is no info after RetVal in the Dim statement (don't know
why
- someone else gave this to me); 2) note the space after "exe" before the
quotation mark. This command line is exactly what you would type in the
Target line of a shortcut to open a specific Word document from your
desktop.

If you want to give the user the option to select a Word document (perhaps
in setting up merge documents with a database export text file), then
substitute explorer.exe for winword.exe and then change stDocName to
represent only a path/directory name with no specific file.

Have fun!


Max said:
Office 2003 Pro. What is the command to open a Word Document ( test.doc)
from inside Access? The document is in the same folder as the database.
Have looked in the Help and KB files, no luck.
Please help.
Thanks.
 
G

Guest

Works like a charm!!! Thanks so much!!!

Pendragon said:
This works for me.

Dim RetVal
Dim stDocName as String

stDocName = "[Word Doc path and filename]"
RetVal = Shell("winword.exe " & stDocName, 1)

Notes: 1) there is no info after RetVal in the Dim statement (don't know why
- someone else gave this to me); 2) note the space after "exe" before the
quotation mark. This command line is exactly what you would type in the
Target line of a shortcut to open a specific Word document from your desktop.

If you want to give the user the option to select a Word document (perhaps
in setting up merge documents with a database export text file), then
substitute explorer.exe for winword.exe and then change stDocName to
represent only a path/directory name with no specific file.

Have fun!


Max said:
Office 2003 Pro. What is the command to open a Word Document ( test.doc)
from inside Access? The document is in the same folder as the database.
Have looked in the Help and KB files, no luck.
Please help.
Thanks.
 
J

John Nurick

Hi Max,

if all you want to do is to open the document for the user, try
Application.FollowHyperlink CurrentProject.Path & "\test.doc"
 

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