Access automation error calling a Word template

K

Kevin B

Hopefully someone has had this issue and can help, but I’m stumped at this
point.

Both Word and Access are Office 2003 applications.

I’m launching Word from an Access form and the application launches just
fine, using either GetObject if there’s an active session or CreateObject it
there isn’t.

If I open a document I do not have any problems, however, when I attempt to
use a template I get an error 5151 “Word was unable to read this document.
It may be corrupt.â€

If I launch Word manually the template opens just fine, but the code cannot
seem to do this. I’ve gotten a very functional error routine going, so not
all is lost, but my original goal ass not been met.

All variables in the code sample below have been explicitly declared and
when I step through the code using watch the string variables have all the
correct values
----------------------------------------------------------------------------------
strPath = DocPath() 'UDF

strTemplate = conAppPath & conDocDot

strDocName = frm.txtPlogID.Value & ".doc"

Set objWord = GetObject(, "Word.Application")

'intErr is assigned an error number in the error trap

If intErr = 429 Then
Set objWord = CreateObject("Word.Application")
End If

With objWord
.Activate
Set objDoc = .Documents.Add(Template:=strTemplate)
.Visible = True
End With
With objDoc
.SaveAs strPath & strDocName
.Activate
End With
objWord.Visible = Tru
 
R

ryguy7272

There are many ways to export data from Access to Word. Here is one:
Private Sub cmdExportToWord_Click()

Dim objWordApp As Word.Application
Dim objWord As Word.Document
Dim oSel As Word.Selection

DoCmd.OutputTo acOutputReport, "Samples", acFormatRTF, "E:\SamplesReport2.rtf"

Set objWordApp = CreateObject("Word.Application")
Set objWord = objWordApp.Documents.Open("E:\SamplesReport2.rtf")

' Make Word visible.
objWord.Application.Visible = True

Set objWord = Nothing
Set objWordApp = Nothing


End Sub

Also, look here:
http://www.members.shaw.ca/AlbertKallal/msaccess/msaccess.html
http://www.members.shaw.ca/AlbertKallal/wordmerge/page2.html

Ryan---
 
K

Kevin B

Thanks for the reply, however, I'm not exporting report data to Word. What
I"m doing is opening a new Word document where the user can enter whatever
they want.

As I said in my earlier post, the code works fine if I present the user with
an existing document, or even a new one, but fails if I create a new document
and specify a template to apply to the new document.
 
R

ryguy7272

That sounds pretty weird. Maybe something in the Word Normal template got
corrupted. You can easily delete the Normal template and rebuild it.
http://word.mvps.org/faqs/apperrors/CorruptDoc.htm

http://www.officearticles.com/word/about_normal_dot_in_microsoft_word.htm

So, see if that's the issue. If so, please resolve, then look here:
http://www.databasedev.co.uk/open_word_doc.html

Or this:
http://www.techonthenet.com/access/modules/word_doc.php

It can be frustrating to deal with Word, but rewarding too. Try the
suggestions above. I think you'll figure it out pretty soon.

Good luck,
Ryan---
 

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