The VBScript Network and Systems Administrator's Cafe

May 9 2008   4:28PM GMT

Working with the Excel.Application object in vbscript to create Excel Spreadsheets



Posted by: Jerry Lees
Excel.Application, VBScript

In my previous two postings we discussed the Microsoft Word.Application object (here and here) to use as a logging mechanism and as a documentation mechanism for our scripts we’re writing.

But what about if you wanted to log numbers, like in the performance counter posts I made a few weeks back? I’ve also seen several questions about how to do use excel in vbscript. Well, it’s super easy, once you get the object understood. The next few postings I’ll focus on this great way to save data in a format that is easily shareable and can easily be made into pretty pie charts and such for the boss.

First off I’ll give you a simple example that basically creates the object (not visible), adds a new workbook to it, adds a string to cell 1,1, and finally saves it before it quits. Below is the example code for this scriptlet:

Const xlSaveChanges = 1

Set objExcel = CreateObject(”Excel.Application”)
objExcel.Visible = False
objExcel.Workbooks.Add
objExcel.Cells(1, 1).Value = “Test value”
objExcel.ActiveWorkbook.SaveAs (”C:\excelvbscript.xls”)
objExcel.Quit

I’ll dig deeper right after I go back and explain the Select Case statement I used in a previous posting– which I neglected to mention previously or explain in the posting. (Bad, Jerry, Bad Bad.) Hwever, after that I promis to come back with a more specific example of Excel.Application, but this should give you a little bit to play with in the mean time.

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 XML files and Generated Word Documents for you. You’ll find 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