View Full Version : VBScript to create Word

01-27-2005, 07:22 AM

I wonder if its possible to programmaticaly create a Word document with macros in it, using only WSH (Windows Script Hosting) and VBScript.

I have created it already, but cant insert the macro. Sure I could do this by using a model with the macro in it, but my doubt is if its possible to create it all by code.

My code is:

Dim objWord

Set objWord = CreateObject ( "Word.Basic" )

objWord.FileNew ( "Normal" )

objWord.Insert "WORD TEST"

objWord.FileSaveAs ( "c:\tstWord.doc" )


02-09-2005, 12:55 AM

I am fairly sure this must be possible (well it is in Excel so I am guessing it will be in Word too) Take a look at this article which takes you all round the VBE objects:


Jacob Hilderbrand
02-09-2005, 01:19 AM
Are you trying to do something like this?

Dim objWord
Dim Doc
Dim Modul
Dim MyText

Set objWord = CreateObject ( "Word.Application" )
Set Doc = Objword.Documents.Add
Set Modul = Doc.VBProject.VBComponents.Add(1)

MyText = "Line1" & vbNewLine & _
"Line2" & vbNewLine & _
"Line3" & vbNewLine & _
"Line4" & vbNewLine

Modul.CodeModule.AddFromString (MyText)

Doc.SaveAs "c:\tstWord.doc"

02-11-2005, 09:16 PM
hi Jake,

exactly but the VBComponents that works on Excel, dont work on WSH :( .

Jacob Hilderbrand
02-11-2005, 09:30 PM
It works with VBScript.

02-11-2005, 09:55 PM
Hi Jake,

not when using Windows Script Host (WSH). It generates the error 800A17B4: Access by programming to Visual Basic not trusted.

There is nothing I can do to avoid it, not even lowering security to low. Try it by pasting it in a file and calling it word.vbs, then run it.

Jacob Hilderbrand
02-11-2005, 10:58 PM
It runs fine for me as a vbs file. You need to allow access to the Visual Basic Project in Word.




Trusted Publishers

Trust access to Visual Basic Project

02-12-2005, 05:40 AM
Hi Jake,

I did that and got the same result.:help

Jacob Hilderbrand
02-12-2005, 06:05 AM
Make sure the setting is saved. Open Word and allow access to the VB project. Save and Close. Re-open and make sure that the setting was saved. Then try to run the vbs file.

02-13-2005, 04:49 PM
Hi Jake,

I did it and still getting the same result. Any idea on what may be my problem??