-
Excel Questions
Anyone that follows this forum has figured out that I am doing a project utilizing Excel. I have a few question and figured that I would throw them in one post.
1. I've seen a few posts from RobDog that mentions using
Code:
moApp = DirectCast(CreateObject("Excel.Application"), Excel.Application)
Does this eliminate the need to use a reference? I'm afraid that if I specify a specific reference that I will encounter Excel version problems in the future.
2. I have a template that I'm opening like this
Code:
oApp.Workbooks.Open(Filename:=data.fileIn)
Now I want to fill this template with data from a CSV file. What is the best way to import this data? My initial plan was to create temp worksheet to place the data, copy and paste data to the worksheet with formating then delete the temp worksheet. It seems like there should be an easier way.
3. Is there an easy way to create a new chart and place it behind the other charts? I'm doing this but it seems a little cluncky
Code:
With oCht
.Move(, oWb.Worksheets(oWb.Worksheets.Count))
.Move(, oWb.Charts(oWb.Charts.Count))
End With
-
Re: Excel Questions
If you don't have a reference then you wouldn't be able to specify this part:
Code:
moApp = DirectCast(CreateObject("Excel.Application"), Excel.Application)
I'm no Office guru but I think that the convention when targeting multiple versions of an Office application is to add the reference so you have Intellisense while you develop, then when you're done you remove the reference and go through and fix all the errors that result by enabling late-binding and changing all the Office-specific types to Object. You'd then have to load the appropriate Office library at run time.
-
Re: Excel Questions
Yes, by having a reference you are using Early Binding and the other reason using the DirectCast is to conform with the Option Strict On declaration which helps reduce type casting errors and other stuff.
If you want to Late Bind so you can support multiple versions of Excel then you may not be able to use the Office Primary Interop Assemblies (link in my sig) as only 2002 and 2003 are supported with them. If your supporting Excel 97 or 2000 then you will need to develop on a system with that version etc. to make sure you are using functions and methods thgat are supported.