How to fully execute excel vba code / how to get around a runtime error?

I am writing VBA code that dynamically sets the properties of some charts, such as a data range, after opening my excel file. This is because the library that I use to export Excel from my .Net project does not fully support chart properties.

I have 4 chart sheets and 1 data sheet in this file.

However, after the first opening of the file, the following error is displayed:

Run-Time Error: '-2147417848 (80010108)':

Automation
The object invoked has disconnected from its clients.

(also the first time you open a warning that the file is probably unsafe and I’m allowed to open manually, but I'm not sure if this has anything to do with this problem)

Subsequent openings of this file do not cause an error.

I searched stackoverflow and forums and found this Microsoft KB article

Based on the recommendations given there, I tried to make my code fully qualified (for example, using Dim app As Applicationand Dim wb As Workbook). However, this did not solve my problem.

Insult line marked **

My questions:

  • Am I really unable to fully execute part of my code?
  • Are there other possible reasons for this error, and if so, how can they be addressed?

My code (in the ThisWorkbook object):

Option Explicit
Private Sub Workbook_Open()

Dim app As Application
Set app = Excel.Application
Dim wb As Workbook
Set wb = app.ThisWorkbook

Dim lastRow As Long, lastRowString As String
lastRow = wb.Sheets("NameOfDatasheet").UsedRange.Row - 1 + Sheets("NameOfDatasheet").UsedRange.Rows.Count 'Worksheets("NameOfDatasheet").Range("A2:G41").AutoFilter field:=1, Criteria1:="<>"

With wb.Charts("NameOfChart1")
.SetSourceData Source:=wb.Sheets("NameOfDatasheet").Range("A2:A" & lastRow & ",D2:E" & lastRow)
'Styling type 1
.SeriesCollection(1).Border.Color = RGB(255, 0, 0)
.SeriesCollection(1).MarkerForegroundColor = RGB(255, 0, 0)
.SeriesCollection(1).MarkerBackgroundColor = RGB(255, 0, 0)
.SeriesCollection(1).MarkerStyle = xlMarkerStyleCircle
.SeriesCollection(1).MarkerSize = 5
'Styling type 2
.SeriesCollection(2).Border.Color = RGB(0, 0, 255)
.SeriesCollection(2).MarkerForegroundColor = RGB(0, 0, 255)
.SeriesCollection(2).MarkerBackgroundColor = RGB(0, 0, 255)
.SeriesCollection(2).MarkerStyle = xlMarkerStyleNone
.SeriesCollection(2).MarkerSize = 5
End With

With wb.Charts("NameOfChart2")
.SetSourceData Source:=wb.Sheets("NameOfDatasheet").Range("A2:A" & lastRow & ",H2:I" & lastRow)
'Styling type 1
.SeriesCollection(1).Border.Color = RGB(255, 0, 0)
.SeriesCollection(1).MarkerForegroundColor = RGB(255, 0, 0)
.SeriesCollection(1).MarkerBackgroundColor = RGB(255, 0, 0)
.SeriesCollection(1).MarkerStyle = xlMarkerStyleCircle
.SeriesCollection(1).MarkerSize = 5
'Styling type 2
.SeriesCollection(2).Border.Color = RGB(0, 0, 255)
.SeriesCollection(2).MarkerForegroundColor = RGB(0, 0, 255)
.SeriesCollection(2).MarkerBackgroundColor = RGB(0, 0, 255)
.SeriesCollection(2).MarkerStyle = xlMarkerStyleNone
.SeriesCollection(2).MarkerSize = 5
End With

Dim MaxVal As Variant, MinVal As Variant

With wb.Charts("NameOfChart3")
.SetSourceData Source:=wb.Sheets("NameOfDatasheet").Range("A2:A" & lastRow & ",F2:F" & lastRow)
MaxVal = app.Max(wb.Sheets("NameOfDatasheet").Range("G2:G" & lastRow))
MinVal = app.Min(wb.Sheets("NameOfDatasheet").Range("G2:G" & lastRow))
If (MinVal = MaxVal) Then
    MinVal = 0
End If
MaxVal = MaxVal + 0.1
MinVal = MinVal - 0.1
.Axes(xlValue).MinimumScale = MinVal
.Axes(xlValue).MaximumScale = MaxVal
End With

With wb.Charts("NameOfChart4")
**.SetSourceData Source:=wb.Sheets("NameOfDatasheet").Range("A2:A" & lastRow & ",B2:B" & lastRow)**
MaxVal = app.Max(wb.Sheets("NameOfDatasheet").Range("C2:C" & lastRow))
MinVal = app.Min(wb.Sheets("NameOfDatasheet").Range("C2:C" & lastRow))
If (MinVal = MaxVal) Then
    MinVal = 0
End If
MaxVal = MaxVal + 0.1
MinVal = MinVal - 0.1
.Axes(xlValue).MinimumScale = MinVal
.Axes(xlValue).MaximumScale = MaxVal
End With

End Sub
+5
source share
2 answers

, ... Worksheets, , ? , "", , . ( Excel , ), :

wb.Sheets("nameOfWorksheet").Activate

Worksheets("nameOfWorksheet").Activate

, . , . collection.get(), , , . , .

+1

, . 1 () , End , , Me.Hide.

, , , ( , ), Nothing , .

:

If app Is Nothing Then
    Set app = Excel.Application
End If

, , wb. , , , , , .

Microsoft .

, .

PS. Excel 2003, .

0

All Articles