Solved

Exec Excel Macro from VB6

Posted on 2000-03-07
3
498 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

803 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