How to execute a Macro from a VB.Net app

R

rsmith

Whats the code for executing a macro contained inside a workbook ?
So far this is what I have :

Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim oRng As Excel.Range

' Start Excel and get Application object.
oXL = CreateObject("Excel.Application")
oXL.Visible = True

oWB = oXL.Workbooks.Open("C:\Documents and _
Settings\Smith\Desktop\Book1.xls")
oSheet = oWB.ActiveSheet

' Add table headers going cell by cell.
oSheet.Cells(1, 1).Value = "First Name"
oSheet.Cells(1, 2).Value = "Last Name"
oSheet.Cells(1, 3).Value = "Full Name"
oSheet.Cells(1, 4).Value = "Salary"

' ??? What will execute Macro1 in object oWB ???

Also, if I want to import a module into a workbook and execute
the Macros contained within, what would be the code for this ?
 
C

Charles Maxson

r...

For your first q, you should be able to use the Run method off of the
application object

Application.Run("macro","parameter1","parameter2",...)

or in your case

oXL.Run("Macro1")
 
T

Thomas Winter

rsmith said:
Also, if I want to import a module into a workbook and execute
the Macros contained within, what would be the code for this ?

This is basically what you are looking for:

Application.ActiveWorkbook.VBProject.VBComponents.Import "filename.txt"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top