Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run-time error '91': Object variable or With block variable not set 1

Status
Not open for further replies.

KellyK

Programmer
Mar 28, 2002
212
US
Greetings. I am getting a peculiar error (in my simple mind) sporatically with some code. I am attempting to take an Excel report, via Access VBA and format it according to a customer's specifications. The code seems to work fine when Excel is not open previously, but if the user has an Excel document open and then runs the Access process, I get the error in the subject line. Here is my code:

Code:
Public Sub FormatDailyShipmentReport(strPath As String)
Dim obj_XL_App As Excel.Application
Dim obj_XL_Wkbk As Excel.Workbook
Dim obj_XL_Sheet As Excel.Worksheet
Dim blnSpawned As Boolean

On Error Resume Next
Set obj_XL_App = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
  Err.Clear
  Set obj_XL_App = CreateObject("Excel.Application")
  blnSpawned = True
End If
On Error GoTo 0

'Hide the opening workbook if grabbing an open instance of Excel
If blnSpawned Then
  obj_XL_App.ScreenUpdating = False
  obj_XL_App.DisplayAlerts = False
End If

Set obj_XL_Wkbk = obj_XL_App.Workbooks.Open(strPath)

Set obj_XL_Sheet = obj_XL_Wkbk.Sheets(1)

'Format LATE rows in red colored text
obj_XL_Sheet.Range("$A:$Z").AutoFilter
obj_XL_Sheet.Range("$A:$Z").AutoFilter Field:=21, Criteria1:="LATE"
obj_XL_Sheet.Range("$A2:$Z2").Select
obj_XL_Sheet.Range(Selection, Selection.End(xlDown)).Select

    With obj_XL_App.Selection.Font
        .Color = -16776961
        .TintAndShade = 0
    End With

There is more code following this but the error occurs in the line "obj_XL_Sheet.Range(Selection, Selection.End(xlDown)).Select" However it runs successfully all the way through if Excel is no opened before. Arg. Any ideas why this might be happening and how to fix it?


Kelly
 
Have you tried
[tt]Dim obj_XL_App As New Excel.Application[/tt] ?

[purple]If we knew what it was we were doing, it would not be called
research [blue]database development[/blue], would it? [tab]-- Albert Einstein[/purple]​
 
Thanks, just tried it.. didn't work. Great idea though. Got my hopes up. :)

Kelly
 
obj_XL_Sheet.Range(obj_XL_App.Selection, obj_XL_App.Selection.End(xlDown)).Select

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV you are wonderful and amazing! Thank you!

Kelly
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top