Open a Word document using Access

G

Guest

I am trying to open a Word template from Access Command Button but I get an
error message saying cannot find the file
 
J

Jon Lewis

This works for me:

Private Sub cmdWrite_Click()
On Error GoTo Err_cmdWrite_Click

Dim mobjWordApp As Word.Application
Dim docs As Word.Documents

On Error Resume Next
Set mobjWordApp = GetObject("Word.Application")
If Err Then
Set mobjWordApp = CreateObject("Word.Application")
End If

On Error GoTo Err_cmdWrite_Click

Dim strTemplate As String
Dim strText As String

strTemplate = "Your Template path"
strText = "Your text"

Set docs = mobjWordApp.Documents


docs.Add strTemplate
With mobjWordApp
.Visible = True
.Activate
.Selection.TypeText text:=strText
End With

Exit_cmdWrite_Click:
Set docs = Nothing
Set mobjWordApp = Nothing
Exit Sub
Err_cmdWrite_Click:
MsgBox "Operation failed (Error No: " & Err.number & ": " &
Err.Description & "). Please try again.", vbInformation
Resume Exit_cmdWrite_Click
End Sub
 
A

Arvin Meyer

Wayne Viles said:
I am trying to open a Word template from Access Command Button but I get an
error message saying cannot find the file

If everything else is right, the message will happen when you've either
typed the wrong path, moved the file, or the Word file is corrupt and can't
be opened.
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access downloads:
http://www.datastrat.com
http://www.mvps.org/access
 

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