Solved

Exec Excel Macro from VB6

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now