Automation Acess to Word

B

Bob W

I am using Access 2000 9.0.6926-SP3 and Word2000 9.0.6926 SP3 with Reference
to Microsoft Word 10.0 Object library running in Windows XP
D:\AC_Probation\Test.doc exists.and opens under word

I wrote the following code in an Access module attempting to open the word
document.

Dim objWord As Word.Application
Set objWord = GetObject("D:\AC_Probation\Test.doc")
objWord.Visible = True

When I run this I get a run time error13 Type mismatch. The same error
occurs whether Word is running or not. Something obvious must be missing
but I am new to Automation and can make no sense of the error message. Any
help appreciated
 
T

Tom Wickerath

Hi Bob,

Try this subroutine.

Tom


Sub Test()

Dim objWord As Word.Application
Dim doc As Word.Document

'set objWord to current Word instance or create new one
On Error Resume Next
Set objWord = GetObject(, "Word.application")
If Err = 429 Then
Set objWord = New Word.Application
End If

On Error GoTo ProcError

Set doc = objWord.Documents.Open("D:\AC_Probation\Test.doc")

objWord.Visible = True

ExitProc:
' Cleanup
On Error Resume Next
Set doc = Nothing
Set objWord = Nothing
Exit Sub
ProcError:
MsgBox Err & Err.Description
Resume ExitProc
End Sub

_________________________________________

"Bob W" <mwund at kcbx . net> wrote in message
I am using Access 2000 9.0.6926-SP3 and Word2000 9.0.6926 SP3 with Reference
to Microsoft Word 10.0 Object library running in Windows XP
D:\AC_Probation\Test.doc exists.and opens under word

I wrote the following code in an Access module attempting to open the word
document.

Dim objWord As Word.Application
Set objWord = GetObject("D:\AC_Probation\Test.doc")
objWord.Visible = True

When I run this I get a run time error13 Type mismatch. The same error
occurs whether Word is running or not. Something obvious must be missing
but I am new to Automation and can make no sense of the error message. Any
help appreciated
 
D

david epsom dot com dot au

You have returned a Document object, not an Application object.

Declare a document object, or get an application object, or
do like I do: use late bound objects, and declare everything
as 'object'

(david)
 
B

Bob W

I am not sure of the distinction. Could you write the sample code for this
example? - Bob
 
A

Arvin Meyer

Here is an example of some early and late binding with Excel. Remember, with
early binding, you must set a reference. That isn't necessary with late
binding. In the early binding, I am calling the Excel 360() function from
Access.

Early:
Function XL360(Arg1, Arg2) As Double
Dim objXL As New Excel.Application
XL360 = objXL.WorksheetFunction.Days360(Arg1, Arg2)
Set objXL = Nothing
End Function

Late:
Sub TestXL()
Dim objXL As Object
Dim objActiveWkb As Object

Set objXL = GetObject(, "Excel.Application")
objXL.Application.Workbooks.Add
Set objActiveWkb = objXL.Application.ActiveWorkbook

With objActiveWkb
.Worksheets(1).Cells(1, 1) = "Hello World"
End With
objXL.Visible = True

Set objXL = Nothing
Set objActiveWkb = Nothing

End Sub
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access downloads:
http://www.datastrat.com
http://www.mvps.org/access
 
D

david epsom dot com dot au

The advantage of late bound objects, as demonstrated by Arvin Meyer,
is that no reference is required, and your other code, including
your start-up and shut-down code, will still work OK even if Word
is not installed. This is not the case with early bound objects.

The disadvantage is that you don't get the help and auto-completion
from the referenced object. Your code used a small mixture: using
GetObject (late bound) instead of New meant that you did not see
the Type Mismatch until runtime.

One option is to write and test as much as possible of your code
using a library reference and early binding, then go through and
change all to generic 'object' references and late binding before
distribution.

Obviously, this is of negligible advantage if you only deploy to
one desktop (your own), but it's good if you plan to widely distribute.

(david)
 
B

Bob W

Tom Wickerath said:
Hi Bob,

Try this subroutine.

Tom


Sub Test()

Dim objWord As Word.Application
Dim doc As Word.Document

'set objWord to current Word instance or create new one
On Error Resume Next
Set objWord = GetObject(, "Word.application")
If Err = 429 Then
Set objWord = New Word.Application
End If

On Error GoTo ProcError

Set doc = objWord.Documents.Open("D:\AC_Probation\Test.doc")

objWord.Visible = True

ExitProc:
' Cleanup
On Error Resume Next
Set doc = Nothing
Set objWord = Nothing
Exit Sub
ProcError:
MsgBox Err & Err.Description
Resume ExitProc
End Sub

_________________________________________

"Bob W" <mwund at kcbx . net> wrote in message
I am using Access 2000 9.0.6926-SP3 and Word2000 9.0.6926 SP3 with Reference
to Microsoft Word 10.0 Object library running in Windows XP
D:\AC_Probation\Test.doc exists.and opens under word

I wrote the following code in an Access module attempting to open the word
document.

Dim objWord As Word.Application
Set objWord = GetObject("D:\AC_Probation\Test.doc")
objWord.Visible = True

When I run this I get a run time error13 Type mismatch. The same error
occurs whether Word is running or not. Something obvious must be missing
but I am new to Automation and can make no sense of the error message. Any
help appreciated
 
B

Bob W

I tried your code and got an error msg. "-2147417851 Automation Error The
server threw an exception" Any ideas as the error msg, gives me no hint- Bob
 
B

Bob W

I tried the code and got an error msg "-2147417851 Automation Error the
server threw an exception" any ideas what it is trying to tell me- Bob
 
T

Tom Wickerath

Hi Bob,

Check to make sure that you don't have any missing references. Missing references can cause
strange errors.

http://members.rogers.com/douglas.j.steele/AccessReferenceErrors.html

http://members.iinet.net.au/~allenbrowne/ser-38.html

I tested the code on my PC before sending it, and it worked fine.

Tom
_________________________________________

"Bob W" <mwund at kcbx . net> wrote in message
I tried your code and got an error msg. "-2147417851 Automation Error The
server threw an exception" Any ideas as the error msg, gives me no hint- Bob
_________________________________________



Hi Bob,

Try this subroutine.

Tom


Sub Test()

Dim objWord As Word.Application
Dim doc As Word.Document

'set objWord to current Word instance or create new one
On Error Resume Next
Set objWord = GetObject(, "Word.application")
If Err = 429 Then
Set objWord = New Word.Application
End If

On Error GoTo ProcError

Set doc = objWord.Documents.Open("D:\AC_Probation\Test.doc")

objWord.Visible = True

ExitProc:
' Cleanup
On Error Resume Next
Set doc = Nothing
Set objWord = Nothing
Exit Sub
ProcError:
MsgBox Err & Err.Description
Resume ExitProc
End Sub

_________________________________________

"Bob W" <mwund at kcbx . net> wrote in message
I am using Access 2000 9.0.6926-SP3 and Word2000 9.0.6926 SP3 with Reference
to Microsoft Word 10.0 Object library running in Windows XP
D:\AC_Probation\Test.doc exists.and opens under word

I wrote the following code in an Access module attempting to open the word
document.

Dim objWord As Word.Application
Set objWord = GetObject("D:\AC_Probation\Test.doc")
objWord.Visible = True

When I run this I get a run time error13 Type mismatch. The same error
occurs whether Word is running or not. Something obvious must be missing
but I am new to Automation and can make no sense of the error message. Any
help appreciated
 

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