Running Word from Access

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to click on a button, run word and have word open a specific file
when it runs. Any suggestions?
 
saminks said:
I am trying to click on a button, run word and have word open a specific file
when it runs. Any suggestions?

One way:

Private Sub cmdWord_Click()
On Error GoTo Err_cmdWord_Click

Dim WordTemplate As String
Dim strFullName As String
Dim objWord As Word.Application

Set objWord = CreateObject("Word.Application")

WordTemplate = Application.CurrentProject.Path & "\Letter.dot"

With objWord
.Visible = True
.Documents.Add (WordTemplate)

' Do stuff

.Activate
.ActiveDocument.Close (wdDoNotSaveChanges)

End With

Exit_cmdWord_Click:
objWord.Quit
Set objWord = Nothing
Exit Sub

Err_cmdWord_Click:
MsgBox Err.Number & ": " & Err.Description, vbInformation, "Error"
Resume Exit_cmdWord_Click

End Sub
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access downloads
http://www.datastrat.com
http://www.mvps.org/access
 
The first example below uses early binding. This requires a reference
(Tools, References in the VBA IDE) to a specific version of the Word object
library, and will break on a PC that has a different version of Word
installed. The second example uses late binding, and will work on a PC that
has any version of Word installed, provided of course you don't use a new
feature that wasn't supported by the installed version of Word.

Private Sub Command0_Click()

Dim objApp As Word.Application

Set objApp = New Word.Application
objApp.Documents.Open ("c:\usenet\test.doc")
objApp.Visible = True
objApp.Activate

End Sub

Private Sub Command1_Click()

Dim objApp As Object

Set objApp = CreateObject("Word.Application")
objApp.Documents.Open ("c:\usenet\test.doc")
objApp.Visible = True
objApp.Activate

End Sub
 
Check Access VB Help on the FollowHyperlink method or the Shell method.
 
saminks said:
I am trying to click on a button, run word and have word open a
specific file when it runs. Any suggestions?

Application.FollowHyperlink "Path to word file"

The above line would accomplish the same thing as double-clicking the file in
explorer.
 
I have used this procedure in Access 2000 and Access 2002, you didn't say
what version you are running, but . . .

Open a form in Design View | Check that the Wizard tool is selected | Select
the Command Button Tool | Place the Command Button on your form.
The Command Button Wizard dialog window will appear.
Select 'Application' | Select 'Run Application' | Next
In the Command line: Type path and file name or Browse for your Word
application, i.e. "C:\Program Files\Microsoft Office\Office10\WinWord.exe"
Enter a space after the last character of the path and file name in the
command line box.
Enter the path and file name of the file you wold like Word to open. i.e.
"D:\mydocs\worddocs\2001reunion.doc | Next
Select Picture or text | Next
Enter the 'meaniful name' | Finish

This will write the VBA code in the 'On Click' event for the command button
you have created. You can visit there and see how Access has done the work
for you. If you are very new to writing code, like me, this is a good way to
learn.
 
Is there a reason you have to access an external Word document as
opposed to placing the text from the Word document in a form inside the
database?

The problem with having Access open an external file is that you have
to make sure that if the database is ever moved or copied to a new
location (another PC or a different place on a network server) that the
external file is also copied over...and then you still have to go into
the database and change the command to point to the new location of the
external file.
 
There are many ways to accomplish what the original question asked. I only
suggested one approach that is built into Access. There may be more than one
reason to have the document linked as opposed to imbedded and vice versa, but
that was not part of the question. Surely you could post the imbedded
procedure and the person asking could pick their poison best suited to their
situation.
 

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

Back
Top