保羅,如果我沒有解決您的問題,我會提前道歉。直到現在,我還沒有意識到有兩種類型的圖表,我有興趣瞭解與他們的問題。無論如何,經過一番混戰之後,我能夠創建一個圖表,命名它,讓它持續下去,並對事件做出反應。它不依賴於互操作的圖表,但:
Public Class ThisWorkbook
Dim clsChart As cChart
Private Sub ThisWorkbook_Startup() Handles Me.Startup
Dim coChartObj As Microsoft.Office.Interop.Excel.ChartObject
Dim chtMyChart As Microsoft.Office.Interop.Excel.Chart
Dim i As Int32
With Globals.Sheet1
For i = 1 To .ChartObjects.count
If .ChartObjects(i).Name = "MyChart" Then
chtMyChart = .ChartObjects(i).chart
Exit For
End If
Next i
If chtMyChart Is Nothing Then
chtMyChart = .Shapes.AddChart.Chart
chtMyChart.SetSourceData(.Range("A1:B2"))
coChartObj = chtMyChart.Parent
coChartObj.Name = "MyChart"
End If
clsChart = New cChart
clsChart.chtChart = chtMyChart
End With
End Sub
End Class
Public Class cChart
Public WithEvents chtChart As Microsoft.Office.Interop.Excel.Chart
Private Sub chtChart_Resize() Handles chtChart.Resize
MessageBox.Show("resize")
End Sub
End Class
根據http://msdn.microsoft.com/en-us/library/cc442765.aspx: 要重新創建Microsoft.Office.Tools.Excel .Chart主機控件,您必須先刪除本機Microsoft.Office.Interop.Excel.Chart,然後使用AddChart(範圍,字符串)或AddChart(雙精度)重新創建Microsoft.Office.Tools.Excel.Chart。 Double,Double,Double,String)方法。沒有添加方法,使您可以基於現有的Microsoft.Office.Interop.Excel.Chart創建新的Microsoft.Office.Tools.Excel.Chart。 所以看起來他們必須在_Startup上完全重新創建。 –
Paul
2010-08-05 23:15:34