|
-
May 9th, 2001, 01:11 PM
#1
Thread Starter
PowerPoster
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!
-
May 9th, 2001, 03:02 PM
#2
Hyperactive Member
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]
-
May 9th, 2001, 03:33 PM
#3
Thread Starter
PowerPoster
Matt,
Just what I needed; works great! Thanks for the help.
Mark
-
May 9th, 2001, 04:55 PM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|