Solved

Exec Excel Macro from VB6

Posted on 2000-03-07
3
505 Views
Last Modified: 2008-02-01
Is there a way to kick off a
macro resident in a Excel 2000 spreadsheet (other than the auto ones) from a visual basic .exe?

Thanks a bunch.
0
Comment
Question by:jogmen
  • 2
3 Comments
 
LVL 4

Expert Comment

by:wesleystewart
ID: 2593270
I'm a little fuzzy on this (it has been a while . . .) but I think it goes like this.  Declare and instantiate a reference to an Excel object, then invoke the procedure with the .Run method:

Dim objExcel as object
Set objExcel = GetObject(, "Excel.Application")
objExcel.Run("sub as string", arg1, arg2)

You need to reference the Excel library.

Wes

0
 
LVL 3

Accepted Solution

by:
MTroutwine earned 50 total points
ID: 2593868
Here is what I normally use:


    ' Declare Excel object:
    Dim objApp As Excel.Application
   
    ' Instantiate the Excel object:
    Set objApp = New Excel.Application
       
    With objApp
        ' Open the spreadsheet:
        .Workbooks.Open "D:\ExcelSpreadsheetName.xls"
        ' Make Excel visible to the user (the default is False):
        .Visible = True
        ' Runt the macro (This supports up to 30 arguments):
        .Run "MacroName"
    End With
   
    ' Do whatever you are going to do...
   
    ' If you want to end Excel programmatically and save changes to do this:
    ' Close has three optional parameters here they are in order:
    ' 1) SaveChanges: Boolean - True = yes save the changes
    '                           False = no don't save the changes
    ' 2) Filename: String - A new file name (if you don't want to save as the old file).
    ' 3) Routeworkbook: Boolean - I have never used this, has something to do with sending
    '                             the workbook to another user.
    objApp.Workbooks("ExcelSpreadsheetName.xls").Close True

:>)
0
 
LVL 3

Expert Comment

by:MTroutwine
ID: 2593871
One thing as mentioned by wesleystewart, you will need to add a reference to the following:

Microsoft Excel 9.0 Object Library
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question