Windows scripting host and Word

A

anithakp

Hi,
I have written code to open a word document and format it through Windows
Scripting host SendKeys Method by activating word.The code is as below
Dim WshShell
Set objWord = WScript.CreateObject("Word.Application")
Set WshShell = CreateObject("WScript.Shell")
WScript.ConnectObject objWord, "objWord_"

objWord.visible = True
blnWordVisible = True

Set doc = objWord.Documents.Add("c:\\testfile.doc")
WshShell.AppActivate "Word"
WScript.Sleep 1000
WshShell.SendKeys "^a"
WScript.Sleep 1000
WshShell.SendKeys "^b"
WScript.Sleep 1000
WshShell.SendKeys "%o"
WScript.Sleep 1000
WshShell.SendKeys "F"
WScript.Sleep 1000
WshShell.SendKeys "%C"
WScript.Sleep 1000
WshShell.SendKeys "{DOWN}{DOWN}{RIGHT}"
WScript.Sleep 1000
WshShell.SendKeys "~"
WScript.Sleep 1000
WshShell.SendKeys
"{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}"
WScript.Sleep 1000
WshShell.SendKeys "~"
WshShell.SendKeys "{RIGHT}"

For selection of Color and other properties the Font window appears. I
donot want to display the window to the end user and do the formatting.Is
there any way to do all these things by hiding the windows?
 
J

Jay Freedman

Besides showing the dialogs (which cannot be avoided with this method),
using SendKeys is the least efficient and least reliable method of automatic
Word with a script. Forget about SendKeys unless there's absolutely no other
way to do the job.

As soon as you create the objWord object, your script has access to all the
objects, properties, and methods of the Word object model, exactly as if you
were writing a VBA macro.

Note that you don't need the WScript.Shell (because there's no need to use
AppActivate or SendKeys) and you don't need the ConnectObject call (because
you aren't using an external script) or the Sleep calls. If the file
testfile.doc already exists, you need the Documents.Open method instead of
the Documents.Add method (the Add method creates a new document, using the
old one as a template).

Try this script:

---- sample.vbs -------
Dim objWord, objDoc
wdColorRed = 255
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Open("c:\testfile.doc")
objDoc.Range.Bold = True
objDoc.Range.Font.Color = wdColorRed
MsgBox "Everything is now bold and red!"
objDoc.Save
objDoc.Close
objWord.Quit
Set objDoc = Nothing
Set objWord = Nothing
 
A

anithakp

Thanks! Its working properly.
Actually the data from the XML must be read and the styles for the text
will be specified in the attributes of the xml tags. The attribute values
must be fetched and then those attributes which contains the formatting
details must be applied to the word document.
In the previous code I hav used jscript to read the xml and writing the
read data to a word document(testfile.doc) and then applying the styles.
Can we do the reading of xml and writing the contents of xml with
formatting in a single file?
 

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