Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Error while accessing Word from VB script

Hi ,
I am a beginner in VB script. I am trying to invoke microsoft word from VB.

But while running the code following error is being displayed.

"Compile error

User-defined type not defined "

My code is as below

Sub Create_Doc()

Dim appWD As New Word.Application
Dim docDoc As New Word.Document
Dim rngRange As Word.Range


' Create a new instance of Word & make it visible
Set appWD = CreateObject("Word.Application.10")
appWD.Visible = True

End sub

Help me in this regard.....

Rajesh
[630 byte] By [pvrajesh31] at [2007-11-11 7:59:05]
# 1 Re: Error while accessing Word from VB script
Did you add the Word object library to your project? (Project...References...Microsoft Word Object Library).
pclement at 2007-11-11 17:26:08 >
# 2 Re: Error while accessing Word from VB script
All variables are Variants in VBScript. Try this:

Dim appWD, docDoc, rngRange

' Create a new instance of Word & make it visible
Set appWD = CreateObject("Word.Application")
appWD.Visible = True
Phil Weber at 2007-11-11 17:27:08 >
# 3 Re: Error while accessing Word from VB script
Hi weber,
Thanx for the reply.

But if i remove those 'as new ' statements then VB is failing at the function
'CreateObject'.
pvrajesh31 at 2007-11-11 17:28:17 >
# 4 Re: Error while accessing Word from VB script
The code runs correctly on my system. What error are you getting?
Phil Weber at 2007-11-11 17:29:11 >
# 5 Re: Error while accessing Word from VB script
Hi weber,

The error that displayed on the system is as follows

Run time error '429':
"ActiveX component can't create object".

Anything should be included as library ?
pvrajesh31 at 2007-11-11 17:30:10 >
# 6 Re: Error while accessing Word from VB script
I just created a new VB project, removed Form1 and added a new Module, and posted the above code into a Sub Main. When I run the app, Word is loaded and becomes visible.
Phil Weber at 2007-11-11 17:31:20 >
# 7 Re: Error while accessing Word from VB script
Hi weber,
Now the VB code is working. I had to add 'microsoft word 11.0 object library '
in the references.

Thanx for the support.

Regards,
Rajesh
pvrajesh31 at 2007-11-11 17:32:15 >