Open a Word document using Access

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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
 
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
 
Back
Top