-
VBScript to create Word
Hi,
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:
[VBA]
Dim objWord
Set objWord = CreateObject ( "Word.Basic" )
objWord.FileNew ( "Normal" )
objWord.Insert "WORD TEST"
objWord.FileSaveAs ( "c:\tstWord.doc" )
objWord.FileClose
[/VBA]
-
Hi
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:
http://www.cpearson.com/excel/vbe.htm
-
Are you trying to do something like this?
[vba]
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"
Doc.Close
objword.Quit
[/vba]
-
hi Jake,
exactly but the VBComponents that works on Excel, dont work on WSH :( .
-
-
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.
-
It runs fine for me as a vbs file. You need to allow access to the Visual Basic Project in Word.
- Trust access to Visual Basic Project
-
Hi Jake,
I did that and got the same result.:help
-
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.
-
Hi Jake,
I did it and still getting the same result. Any idea on what may be my problem??