【问题标题】:Using Late binding on MSGraph Chart Object在 MSGraph 图表对象上使用后期绑定
【发布时间】:2020-08-17 21:10:43
【问题描述】:

尊敬的 Stackoverflow 社区, 我不知道这是否正确,并且想要一些关于如何在 MSGraph 对象上使用 Late binding 的指导。 我已经使用了这个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

后期绑定: -Approach

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

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

【问题讨论】:

    标签: vba ms-access


    【解决方案1】:

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

    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...
    

    【讨论】:

    • 又好又快。非常感谢!
    • 我已经修改了我的代码,现在运行良好。抱歉,我无法回答您的问题,因为我不知道更好。
    猜你喜欢
    • 2010-10-21
    • 1970-01-01
    • 2012-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-15
    相关资源
    最近更新 更多