Opening a spreadsheet using VB

P

Patrick Molloy

Private Sub cmfRefresh_Click()

Dim xlAPP As Object
Dim xlWB As Object

Set xlAPP = CreateObject("Excel.Application")

Set xlWB = _
xlAPP.workbooks.open("H:\May_Timesheet.xls")

xlWB.activesheet.calculate

xlWB.Close True

Set xlWB = Nothing

xlAPP.quit

Set xlAPP = Nothing

End Sub

Instead of using the createobject method, you could set a
reference to the VB6 object library inj the VB IDE.
Then dim xlApp as Excel.Application and then SET xlAp =
New Excel.Application
dim xlWB as Excel.Workbook

the code above uses "late binding" ie you dim an object
but the code doesn't know what it is until the programme
creates the object.
Setting a reference allows "early binding" as I
described, which is, apart from being better and nmore
efficient in regards to compiling the code, enables the
programmer to use intellisense when writing the code.

HTH
Patrick Molloy
Microsoft Excel MVP

Option Explicit
' prog ref to Excel 10 Object Library
Private Sub cmfRefresh_Click()

Dim xlAPP As Excel.Application
Dim xlWB As Excel.Workbook

Set xlAPP = New Excel.Application

Set xlWB = xlAPP.workbooks.open
("H:\May_Timesheet.xls")

xlWB.ActiveSheet.calculate


xlWB.Close True

Set xlWB = Nothing

xlAPP.quit

Set xlAPP = Nothing


End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top