The VBScript Network and Systems Administrator's Cafe

Apr 30 2008   2:20PM GMT

Using Vbscript to create Word documents automatically with the Word.Application Object



Posted by: Jerry Lees
Word.application, VBA, DataManagement, VBScript, Developer documentation, Documentation

I recently had a project that required me to create a ton of documentation from data that was already stored in files elsewhere on the network– just not in the format that was required and readable by every day people within IT. (Basically, non-administrators) This project would have required a TON of copy and paste operations or a whole lot MORE reworking of a document, plus I was on a tight deadline. So I decided to use a little VBA (Visual Basic for Applications) knowledge I had picked up in the past to work with the Microsoft Word Application interface and do the job, with only minor reworking after the fact to really tiddy up the document.

First, let me clarify. Most of my scripts I use here require nothing more than the items I mentioned in my first posting (Getting Started Writing VBScript to Administer a Windows 2000 or 2003 Network) on this blog… a Vbscript editor. (Notepad will do… but you’ll likely quickly want something a bit more powerful before long.) The scripts I discuss in this series are one exception, for these scripts we discuss in the next few articles we will need to make sure you have Microsoft Word installed where ever you run the script. However, for the purposes of this type of work your workstation should work fine and will likely already have Microsoft word installed! Bonus!

The “magic” component we need to call and create an object from is called Word.application, as you’ll see below, it’s pretty easy to create a word document from vbscript and then have autogenerating documentation!

The next sections of code will show you how to do various tasks with in word. Many sections are not scripts on themselves, but infact snippets that could be placed into the first script prior to the remark for testing. The first snippet, creates a word document and saves it to the root of your C: drive. When you run this it will run, but create a blank word document– much like if you right clicked somewhere and selected New/Microsoftw Word Document. Here are some common tasks in word I use:

Create a new (empty) Word Document and save it to c:\testdoc.doc
Set objWord = CreateObject(”Word.Application”)
‘note: if you don’t want word popping up and displaying as this is built– set this next line to False

objWord.Visible = True
Set objDoc = objWord.Documents.Add()
’save the Word Document
objDoc.SaveAs(”C:\testdoc.doc”)

Setting the Font type
objSelection.Font.Name = “Arial”

Setting the Font Size
objSelection.Font.Size = “18″

Adding Text
objSelection.TypeText “Add your text here”
objSelection.TypeParagraph()

Setting the typeface to bold
objSelection.Font.Bold = True

Closing the word document (generally done after saving)
objWord.Quit

These should get you started playing with Vbscript to create word documents. For more information on working with microsoft word in vbscript look here at Microsoft’s site, while it’s not related exactly to vbscript– it should give you enough information about the interface to get started.

 Next time, I’ll share a script to document settings in Group policy with a word document through a combination of using Word.Application and Microsoft.XMLDOM.

As always, , this code works perfectly. However, sometimes the formatting of the blog breaks the code if you copy and paste it into your editor. So, if you’d like to not type or troubleshoot any syntax errors due to the copy and paste problems I’ve provided the code for download for you. From here on out I will make the code available for download from my website’s (www.webstemsadministration.com) File Depot under the ITKE Blog Scripts category. Enjoy and happy scripting!

Comment on this Post


You must be logged-in to post a comment. Log-in/Register