Results 1 to 4 of 4

Thread: Opening Excel from VB project

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    Opening Excel from VB project

    I am trying to open an existing EXCEL file that will pop up when the user clicks a label and allow him to edit it and save it as any filename. I have the reference to excel objects checked but can't seem to get it to work. A blank "book1" EXCEL file pops up when I use the following code:

    Private Sub Label4_Click()

    Dim MyXL As Object
    Set MyXL = GetObject("c:\My Documents\admin\start.xls")
    MyXL.Application.Visible = True
    MyXL.Parent.Windows(1).Visible = True

    End Sub

    What am I doing wrong?

    Thanks for any help!

  2. #2
    Hyperactive Member MetallicaD's Avatar
    Join Date
    Feb 2001
    Location
    Tallahassee, FL
    Posts
    488
    well.. here is what ya need to do.. first you need to reference the Excel object library in the VB references.. i think its Excel 9.0

    then, just this code...

    Code:
    Public Function ExcelOpen()
    ' This will open excel and open the proper file in excel
    
    Set goExcel = New Excel.Application
    ExcelOpen = True
    
    If goExcel Is Nothing Then
        MsgBox "Can't Create Excel Object, Please restart program"
        ExcelOpen = False
    End If
    
    'load the proper file
    goExcel.Workbooks.Open "c:\My Documents\admin\start.xls"
    goExcel.Visible = True
       
    End Function
    then to close it.. use the following:
    Code:
     
    Public Sub ExcelKill()
    ' This closes excel and any open workbooks
       
    On Error GoTo ErrorHandler
    
    If Not goExcel Is Nothing Then
        If goExcel.Workbooks.Count >= 1 Then
            Clear_Invoice
            goExcel.ActiveWorkbook.Close (False)
            ' the false tells it NOT to ask to save changes
        End If
        
        goExcel.Quit
        Set goExcel = Nothing
        
    End If
    
    Exit Sub
    
    ErrorHandler:
    MsgBox Err.Description
    Exit Sub
       
    End Sub
    Hope this helps
    -Matt
    [vbcode]
    '*****************************
    MsgBox "MCD :: [email protected]", vbInformation + vbOKOnly, "User"
    '*****************************
    [/vbcode]

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    Matt,

    Just what I needed; works great! Thanks for the help.

    Mark

  4. #4
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    No need to reference the exel 9.0
    Code:
    Private Sub Command1_Click()
    On Error goto errHnd
    Dim XL as Object
    Set XL = CreateObject("Excel.Application")
    XL.Visible = true
    XL.Workbooks.Open "c:\My Documents\admin\start.xls"
    
    Set XL = Nothing
    Exit sub
    ErrHnd:
    Msgbox "Error Opening Excel!"
    End SUb
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

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