Results 1 to 3 of 3

Thread: Launching Excel from VB

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 1999
    Location
    London, England
    Posts
    213

    Launching Excel from VB

    Is it possible to launch excel from VB and execute a macro ?

  2. #2
    Hyperactive Member SjR's Avatar
    Join Date
    Jul 2001
    Location
    Birmingham, UK
    Posts
    336
    Select Microsoft Excel 8.0 Object Library in the Project references.

    Do something like...

    Dim objExcel As Excel.Application
    Dim objBook As Excel.Workbook

    Set objExcel = New Excel.Application
    Set objBook = objExcel.Workbooks.Add

    objExcel.Visible = True
    objBook.Sheets(1).Cells(1,1).Value = "HELLO"
    objBook.Sheets(1).Cells(1,1).Font.Bold = True

    Etc...

    Remember to Quit Excel after use and set objects to nothing to avoid sessions being left open in the background as VB gets upset. Not sure about executing a pre-written macro as I've always programmed what I need to do in VB.

    PS I have a question.

    Has anybody tried to close down an Excel application when a chart object has been created in a workbook which has been saved and closed.

    I found out that the chart object keeps a link to the cell range that is the chart source data. This prevents Excel from closing properly.

    The way I got round this was to use GetObject and a loop, and close any applications that were lingering - any other ideas??

  3. #3
    Fanatic Member Gaffer's Avatar
    Join Date
    Nov 2000
    Location
    London
    Posts
    828
    That certainly shows you how to openexcel. As for running Excel Macros, try:

    Code:
    Dim xls As New Excel.Application
    Dim wrk As Excel.Workbook
    
    Set wrk = xls.Workbooks.Open("c:\MySheet.xls")
    
    xls.Run "MyMacro"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width