vba - 在 MSGraph 图表对象上使用后期绑定
问题描述
亲爱的 Stackoverflow 社区,我不知道这是否正确,并且想要一些关于如何Late binding
在MSGraph
对象上使用的指导。我已经使用了它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
解决方案
这种方法很好。这里有两个奇怪的地方:
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...
推荐阅读
- html - 获取 td 标题 nodeJS
- reactjs - 让 redux-auth-wrapper 等到会话被检查
- ios - 我可以在没有 Apple Developer 帐户的情况下使用 TestFairy 共享 iOS 调试应用程序吗?
- pandas - 为了熊猫数据框应用几个条件
- maven - 依赖项未添加到原型中
- highcharts - 如何在 highstocks 中创建自定义工具提示
- python - 使用 softmax(1D Array 到 2D Array)在 AI 上测试数据
- google-cloud-platform - 在 BigQuery 中使用 EXCEPT 关键字和非表值
- javascript - 将 useRef 与旧版本的反应组件一起使用?
- python - Django 搜索表单,消歧页面