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

Jerry Lees Jerry Lees Profile: Jerry Lees

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 tidy 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 (<a title=”Permanent Link to Getting Started Writing VBScript to Administer a Windows 2000 or 2003 Network” rel=”bookmark” href=”http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/getting-started-writing-vbscript-to-administer-a-windows-2000-and-2003-network/”>Getting Started Writing VBScript to Administer a Windows 2000 or 2003 Network</a>) on this blog… a VBScript editor. <em>(Notepad will do… but you’ll likely quickly want something a bit more powerful before long.) </em>The scripts I discuss in this series are <strong><em>one exception</em></strong>, 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 <em><strong>Word.Application</strong>, </em>as you’ll see below, it’s pretty easy to create a word document from VBScript and then have auto generating 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 in fact 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 <em>New/Microsoft Word Document.</em> Here are some common tasks in word I use:

<strong>Create a new (empty) Word Document and save it to c:\testdoc.doc</strong>
<span style=”color: #15326b”>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</span><span style=”color: #15326b”>
objWord.Visible = True
</span><span style=”color: #15326b”>Set objDoc = objWord.Documents.Add()
‘save the Word Document
objDoc.SaveAs(“C:\testdoc.doc”)
</span>

<span style=”color: #15326b”><span style=”color: #000000″><strong>Setting the Font type
</strong><span style=”color: #15326b”>objSelection.Font.Name = “Arial”
</span></span></span>

<span style=”color: #15326b”><span style=”color: #000000″><strong>Setting the Font Size
</strong><span style=”color: #15326b”>objSelection.Font.Size = “18”
</span></span></span>

<span style=”color: #15326b”><span style=”color: #000000″><strong>Adding Text
</strong><span style=”color: #15326b”>objSelection.TypeText “Add your text here”
objSelection.TypeParagraph()</span></span></span>

<span style=”color: #15326b”><strong><span style=”color: #000000″>Setting the typeface to bold
</span></strong></span><span style=”color: #15326b”>objSelection.Font.Bold = True</span>

<span style=”color: #15326b”><span style=”color: #000000″><strong>Closing the word document (generally done after saving)
</strong><span style=”color: #15326b”>objWord.Quit</span></span></span>

<span><span style=”color: #000000″>These should get you started playing with VBScript to create word documents. For more information on working with Microsoft word in VBScript look <a href=”http://msdn.microsoft.com/en-us/library/78whx7s6(VS.80).aspx” target=”_blank”>here</a> at Microsoft’s site, while it’s not related exactly to VBScript– it should give you enough information about the interface to get started.</span></span>

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

<span style=”color: #0000ff”><span style=”color: #000000″>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, plus example output files  from my final tests for you. You’ll find the code and other files available for download from my website’s <a href=”http://www.websystemsadministration.com/”>www.websystemsadministration.com</a> File Depot under the <span style=”color: #0000ff”><em>ITKE Blog Scripts</em></span> category.<span style=”color: #0000ff”> </span><span style=”color: #0000ff”><span style=”color: #0000ff”><span style=”color: #000000″>Enjoy and happy scripting!</span></span></span></span></span>

&lt;!–a href=”http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/pinging-a-remote-computer-from-another-remote-computer-using-the-wmi-win32_pingstatus-class/trackback/” title=”Trackback URL”&gt;Trackback URL</a>–&gt;

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: