Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


April 07, 2003

Formatting the Reports


RSS
View this exclusive article with VIP access -- click here to join |
See More Reporting Articles Here | Reprints | Or sign up for our VIP Monthly Pass!
Main Article    AD and WMI Reporting

How did I determine what lines to add to the VBScript to automate Microsoft Excel? The trick is simple. Open Excel and select Tools, Macro, Record New Macro from the menu bar, then perform the operations that you want your script to automate (e.g., saving, opening, autofitting columns and rows, coloring cells). Stop the macro, then open and edit it to see the Visual Basic for Applications (VBA) code that it generates.

You can't copy this code exactly. First, constants such as xlNormal and xlSolid are already defined in VBA, so you'll need to find out their values and define them in your script. To do so, press F2 from within the Microsoft Visual Basic (VB) macro editor (or select View, Object Browser from the editor's menu bar) to open the Object Browser. Select the <globals> class from the Classes pane, then scroll through the Members of <globals> pane until you find the constant you want to identify. When you select the constant, its definition appears in the box at the bottom of the Object Browser. Second, VBA is more verbose than VBScript when a subroutine, function, or method has parameters, so you'll want to edit the code. Consider the following example, which is the record of my use of the SaveAs method:

ChDir "C:\ "
ActiveWorkbook.SaveAs Filename:= _
  "C:\Book2.xls", FileFormat:= _
  xlNormal, Password:="", _
  WriteResPassword:="", _
  ReadOnlyRecommended:=False, _
  CreateBackup:=False

The macro shows that Excel popped up a Save dialog box, from which I navigated to the C: root drive, then saved the spreadsheet as Book2. Because the SaveAs command includes the path, I can delete the first line of this code. The second line defines six parameters, each beginning with the parameter name, followed by ":=", then the parameter assignment. To convert this code into VBScript, I remove the parameter names and ":=" and preface the line of code with "appExcel." (or whatever variable name you're using for your Excel Application object). The result looks like the following: . . .

Reader Comments

You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
No Jobs, No Excitement at Apple's Last Macworld Keynote

Apple CEO Steve Jobs made the right move in skipping out on his company's last appearance at Macworld: In a Tuesday keynote address at the conference, Apple had no interesting new products to sell, opting instead to spend mind-numbing amounts of time on ...

Where is Microsoft NetMeeting in Windows XP?

...

The Memory-Optimization Hoax

Don't believe the hype. At best, RAM optimizers have no effect. At worst, they seriously degrade performance. ...


Related Events Virtualization for Mission-Critical BI with SQL Server

Introduction to Identity Lifecycle Manager "2"

Protecting Mobile Users' Data

Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2009 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing