Run-time error '91':

starbitz
03-23-2003, 03:46 PM
Hi!

I have a program that calls Excel Application. It works perfectly on the first call, but when I tried to use it again, it gives a "Run-time error '91': Object variable or With block variable not set".

The code in calling Excel is in my CreateGraph_click. I have declared my variable in General declarations as

Dim AppExcel As Excel.Application
Dim AppPower As PowerPoint.Application
Dim pPresent As Presentation
Dim oSlides As Slides
Dim wSheet As Worksheet
Dim wBook As Workbook
Dim xcels As Integer

'and I have also Set them in the beginning code of CreateGraph_click:

Set AppExcel = CreateObject("Excel.Application")

If AppExcel.Workbooks.Count = 0 Then
Debug.Print "Adding a new workbook"
Set wBook = AppExcel.Workbooks.Add
End If
txtWindow.Text = txtWindow.Text & "Tabulating data..." & vbCrLf
Set wSheet = AppExcel.Sheets(1)

'and somewhere after these lines I have a code where the error occurs

wSheet.Cells.Select
With Selection.Font 'this is where the error occurs after 2nd call
.Name = "Arial"
.Size = 8
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
Range("A2").Select

'I have also this codes before I exit on the CreateGraph_click

Dim w As Integer
If AppExcel.Workbooks.Count > 0 Then
For w = AppExcel.Workbooks.Count To 0 Step -1
AppExcel.Workbooks(w).Close
Next w
End If
Set wSheet = Nothing
Set wBook = Nothing
Set AppExcel = Nothing
AppExcel.Quit

Now, why does I get "Object variable or With block variable not set" error where in fact I Set these objects everytime I call the CreateGraph_click method?

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum