首页 > 解决方案 > 在 MSGraph 图表对象上使用后期绑定

问题描述

亲爱的 Stackoverflow 社区,我不知道这是否正确,并且想要一些关于如何Late bindingMSGraph对象上使用的指导。我已经使用了它Early binding并且它可以工作,但现在我想使用后期绑定,这样我就可以避免添加Microsoft Graph 16.0 Object Library. 以下代码有效,但需要Microsoft Graph 16.0 Object Library

早期绑定:

Private Sub Form_Open(Cancel As Integer)
Dim myChart As Graph.Chart
Dim myChartSeries As Graph.Series
Dim mySeriesDataLabel As Graph.DataLabel

Set myChart = Me.myGraph.Object

For Each myChartSeries In myChart.SeriesCollection

For Each mySeriesDataLabel In myChartSeries.DataLabels
mySeriesDataLabel.Font.Name = "Times New Roman"
mySeriesDataLabel.Font.FontStyle = "Normal"
mySeriesDataLabel.Font.Size = 8
Next mySeriesDataLabel
Next myChartSeries

With Me.myGraph.Axes(1).TickLabels.Font
.Name = "Times New Romans"
.FontStyle = "Normal"
.Size = 8
End With

 With Me.myGraph.Axes(2).TickLabels.Font
.Name = "Times New Romans"
.FontStyle = "Normal"
.Size = 8
End With

End Sub

后期绑定: -方法

我已经尝试过这段代码并且似乎正在工作,但我不确定这是否是正确的做法。有人可以请我指导正确的方法吗?

Private Sub Form_Open(Cancel As Integer)
Dim myChart As Object
Set myChart = Me.myGraph.Object
    
Dim myChartSeries As Variant
Set myChartSeries = New VBA.Collection

Dim mySeriesDataLabel As Variant
Set myChartSeries = New VBA.Collection

For Each myChartSeries In myChart.SeriesCollection

For Each mySeriesDataLabel In myChartSeries.DataLabels
mySeriesDataLabel.Font.Name = "Times New Roman"
    mySeriesDataLabel.Font.FontStyle = "Normal"
mySeriesDataLabel.Font.Size = 8
Next mySeriesDataLabel
Next myChartSeries

With Me.myGraph.Axes(1).TickLabels.Font
    .Name = "Times New Romans"
    .FontStyle = "Normal"
    .Size = 8
End With

With Me.myGraph.Axes(2).TickLabels.Font
    .Name = "Times New Romans"
    .FontStyle = "Normal"
    .Size = 8
End With

End Sub

标签: vbams-access

解决方案


这种方法很好。这里有两个奇怪的地方:

Dim myChartSeries As Variant '<- Why a Variant? We use Object for late-bound objects
Set myChartSeries = New VBA.Collection 'Why? It's not a collection, and you overwrite this in the For Each

如果我们重写它,我们将得到:

Dim myChart As Object
Set myChart = Me.myGraph.Object
    
Dim myChartSeries As Object
Dim mySeriesDataLabel As Object
'No More Set ... As Collection

For Each myChartSeries In myChart.SeriesCollection
       'Etc...

推荐阅读