Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CreateObject fails but GetObject works from Access code to Word doc...

Status
Not open for further replies.

BoulderRidge

Programmer
Mar 18, 2002
107
US
I am pushing data to Word from Access 2003.

This code and subsequent document manipulation all works IF I have Word already open with any document (the 'GetObject'piece works).

If I DON'T have Word open, it drops to the 'CreateObject' code and always fails on this line with Err.Number = -2147024770 which says 'Automation error. The specified module could not be found.'

Here is the code:
Public Function RunAddChangeRpt(datTargetDate As Date, lngEmployeeID As Long, Optional strReportDoc As String = "") As Boolean
RunAddChangeRpt = True
On Error GoTo ErrMe
Dim strFilePathTemplate As String
Dim strFilePathReport As String
Dim strMyReportDoc As String
'Dim objWord As Object
Dim blnCloseAPP As Boolean 'if we open it, then we should close it
Dim APP As Object
Dim Doc As Object
'Dim APP As Word.Application
'Dim Doc As Word.Document
Dim Conn As ADODB.Connection
Dim rsData As ADODB.Recordset

'*******Open Doc***** (Code from multiple Tek-Tips postings combined)

blnCloseAPP = False
On Error Resume Next
Set APP = GetObject(, "word.application")
'declare the error default statement
On Error GoTo ErrMe

If TypeName(APP) = "Nothing" Then
'Word was not open -- create a new instance
Set APP = CreateObject("Word.Application")
If APP Is Nothing Then
'does not have word on their system
MsgBox "The Word application does not exist on this system!", vbCritical, "ERROR"
GoTo ErrMe 'the exit routine
End If

blnCloseAPP = True

End If

APP.Visible = True 'use false to hide your app in the background, and true to display it.

...continue from here to manipulate document....

My references are set like this:
- Visual Basic for Applications
- Microsoft Access 11.0 Object Library
- Microsoft Word 11.0 Object Library
- OLE Automation
- Microsoft ActiveX Data Objects 2.8 Library
- Microsoft ADO Ext. 2.8 for DDL and Security
- Microsoft DAO 3.6 Object Library

Why would GetObject work but CreateObject fail in the same environment???

Any clues would be much appreciated. Trying to wrap this up today. Thanks!
-- BoulderRidge
 
I have tried both versions of these object variables and get the same result:
Dim APP As Object
Dim Doc As Object

and

Dim APP As Word.Application
Dim Doc As Word.Document

I started with the Word. objects but most recently tried generic ones in hopes of making it work. In both versions the code worked when a different Word document was already open and not when it was closed.

THanks for asking. --BoulderRidge
 
Wierd, that code works just fine in an Excel spreadsheet (Minus the ADODB)

When you debug and compiile, does it error out anywhere ?

I did a quick Google for you, and found two similair situations where the same thing was happening. In both cases, they said unintaliing office and re-installing solved the problem.



Tyrone Lumley
SoCalAccessPro
 
Thanks very much for doing the search, I think I will put this on another PC and see whether it behaves differently or not--I wouldn't have thought of that before.

It does not error when you compile, but when it runs it always errors on the GetObject line.

Will post back after I can try it in another environment...I agree it doesn't really make sense. I am loath to uninstall Office but we'll see...

--BoulderRidge
 
BoulderRidge
Try:
If IsEmpty(APP) Then
Or
If TypeName(APP) = "Empty" Then

Instead of:
If TypeName(APP) = "Nothing" Then

Tyrone
This is an Access Forum, not Office.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top