I would really appreciate some pointers as I have run into something where I clearly don't understand enough about Excel.
I have now reduced the problem to the following
Private Sub Workbook_Open()
Dim Xbook As Excel.Workbook
Dim Xsheet As Excel.Worksheet
Dim PlanName As String
Dim RC As Long
Xbook = Application.Workbooks.Open(Filename:="G:\GENERAL\Databases\Depot Insights\conf\WarehousePvA" & "\" & "Plans.xls", ReadOnly:=True)
Xsheet = Xbook.Worksheets("Plans")
RC = 2
With Xsheet
While (.Cells(RC, 1).Text <> "")
PlanName = .Cells(RC, 1).Text
Wend
End With
End Sub
Is this too early in Excel startup to start opening another Excel workbook?
---- orignal question ----
I have library code / classes that are successfully used in several Access based projects but one is generating an Error 91 object error when used in Excel.
The code creates a separate Excel instance (via a global variable gXapp - excuse the ugliness) then opens an input file. In Access this works fine, but in Excel I get an error when exiting the OpenXbook function that returns an Excel.Workbook.
If you step through the code, you can see the Excel instance in Windows Task Mgr, then the file contents in the Excel instance that is visible, then error as you exit the function.
I've even tried dropping the gXapp and altering OpenXbook to use Application.Workbook.Open and still the same problem.
Any ideas?
Gordon
ThisWorkbook contains
Private Sub Workbook_Open()
InitialisePerformance
End Sub
A separate module contains
Global Const gcConfPath = "G:\GENERAL\Databases\Depot Insights\conf\WarehousePvA"
' Global variables for Performance
'
Global gXapp As Excel.Application
' InitialisePerformance
'
'
Public Sub InitialisePerformance()
Const cModuleName = "InitialisePerformance"
Dim Plans As ClassFileSystemFile
Dim Xbook As Excel.Workbook
Dim Xsheet As Excel.Worksheet
Dim PlanName As String
Dim RC As Long
Set gXapp = CreateObject("Excel.Application")
gXapp.Application.Visible = True
Set Plans = New ClassFileSystemFile
Plans.Path = gcConfPath
Plans.Filename = "Plans.xls"
Xbook = Plans.OpenXBook
Xsheet = Xbook.Worksheets("Plans")
RC = 2
With Xsheet
While (.Cells(RC, 1).Text <> "")
PlanName = .Cells(RC, 1).Text
Wend
End With
Xit:
If (Not (Xsheet Is Nothing)) Then Xsheet = Nothing
If (Not (Xbook Is Nothing)) Then Xbook = Nothing
If (Not (Plans Is Nothing)) Then Plans = Nothing
Exit Sub
Err:
MsgBox Err.Description, , cModuleName & "()"
Resume Xit
End Sub
ClassFileSystemFile - includes
Private thisPath As String
Private thisFilename As String
Private thisFlagOpenReadOnly As Boolean
Private thisFlagCreateOnOpen As Boolean
Private Sub Class_Initialize()
Set thisDictionary = New Scripting.Dictionary
Set thisFSO = New Scripting.FileSystemObject
thisFlagOpenReadOnly = False
thisFlagCreateOnOpen = False
End Sub
Public Function OpenXBook() As Excel.Workbook
Dim Xbook As Excel.Workbook
Dim FullyQualifiedFilename As String
If (Right(thisPath, 1) = "\") Then
FullyQualifiedFilename = thisPath & thisFilename
Else
FullyQualifiedFilename = thisPath & "\" & thisFilename
End If
If (thisFlagCreateOnOpen) Then
' TBD - Factor out use of local var Xbook
Set Xbook = gXapp.Application.Workbooks.Add
Xbook.SaveAs Filename:=FullyQualifiedFilename
Set OpenXBook = Xbook
Set Xbook = Nothing
Else
Set OpenXBook = gXapp.Application.Workbooks.Open(Filename:=FullyQualifiedFilename, ReadOnly:=thisFlagOpenReadOnly)
End If
End Function ' ERROR OCCURS WHEN DEBUGGER STEPS OFF HERE
Bookmarks