Run an Excel Macro

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Run an Excel Macro

Post by Marcus Tettmar » Thu May 10, 2007 9:08 am

Use this code to open an Excel workbook and run an excel macro contained within it:

Code: Select all

//Put this VBScript block at top of script
VBSTART
Sub OpenAndRun(xlFileName, MacroName)
  Set xlApp = CreateObject("Excel.Application")
  'remove next line to make Excel invisible
  xlApp.visible = true
  Set xlBook = xlApp.WorkBooks.Open(xlFileName)
  xlApp.Run(MacroName)
  'Uncomment next line to quit Excel
  'xlApp.quit
End Sub
VBEND

//Use this line when you want to run the Excel macro:
VBRun>OpenAndRun,c:\files\myexcel.xls,MyMacro

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts