Re: Can't Open an Excel File
The problem is that GetObject("c:\MyFile.xls") does not return an Application object.
(hint: you already have an apt variable)
Re: Can't Open an Excel File
Try this instead:
Set wBook = AppExcel.Workbooks.Open("c:\MyFile.xls") 'Open an existing workbook
AppExcel.Visible = True 'set to False to hide from user
Re: Can't Open an Excel File
Quote:
Originally Posted by
si_the_geek
The problem is that GetObject("c:\MyFile.xls") does not return an Application object.
(hint: you already have an apt variable)
Hmmm, your hint doesn't ring any bells :confused:.
todd.kauffman,
When I tried the line that you mentioned I get a new error pointing to the line you suggested
"Run-time error '91': Object variable or With block variable not set"
Any suggestions on how to open a file in excel? Thank you...
Robert
Re: Can't Open an Excel File
This works!
vb Code:
Dim AppExcel As Excel.Application
Dim wSheet As Worksheets
Dim wBook As Workbook
Private Sub Form_Load()
Set AppExcel = New Excel.Application
Set wBook = AppExcel.Workbooks.Open("c:\MyFile.xls") 'Open an existing workbook
wBook.Application.Visible = True 'set to False to hide from user
End Sub
Re: Can't Open an Excel File
Quote:
Hmmm, your hint doesn't ring any bells
getobject(workbook) returns a workbook object, type mismatch to application object (appexcel)
would work fine to wbook, which is a workbook object variable
if you want to work with a workbook that may be already open, getobject(workbook) is good, it will find it in any instance of excel if it is already open, or open it if it is not
to make visible if required
vb Code:
wbook.application.visible = true
wbook.windows(1).visible = true
either or both may be required
Re: Can't Open an Excel File
Quote:
Originally Posted by
Nightwalker83
This works!
vb Code:
Dim AppExcel As Excel.Application
Dim wSheet As Worksheets
Dim wBook As Workbook
Private Sub Form_Load()
Set AppExcel = New Excel.Application
Set wBook = AppExcel.Workbooks.Open("c:\MyFile.xls") 'Open an existing workbook
wBook.Application.Visible = True 'set to False to hide from user
End Sub
Nightwalker83,
Your code works fine.
westconn1,
It makes sence what you are saying. I still don't have a clear understanding of all of this VB6 concepts.
Thank you all for your help. Now, I have a final question. The code from Nightwalker83 would only work if I get an Excel code example that came in a CD with a book and copy paste Nightwalker83's code in there. If I create a blank new project and paste that code it won't even recognize the first line of code "Dim AppExcel As Excel.Application". I get a "Compile error: User-defined type not defined".
Is there any settings that I need to make in order for these Excel applications to works? I think I read about this somewhere, but I can't remember how to set it.
Thanks again for your help.
Robert
Re: Can't Open an Excel File
I recommend taking a look at my Excel tutorial (link in my signature), as it covers that issue - and others you are likely to come across.
Re: Can't Open an Excel File
using getobject or createobject to set variables of type object, require no reference to excel, and will work on any computer with any version of excel installed
using variables of type excel.application or excel.workbook require a reference (to excel) to be added to the project, this will then only work with the specific version of excel that the reference is for, and fail on computers using any other version