#vba #ms-access
#vba #ms-access
Вопрос:
Уважаемое сообщество Stackoverflow, я не знаю, правильно ли это, и хотел бы получить некоторые рекомендации о том, как использовать Late binding
on MSGraph
object. Я использовал это 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
Ответ №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...
Комментарии:
1. Красиво и быстро. Большое вам спасибо!
2. Я внес изменения в свой код, теперь он работает хорошо. Извините, я не могу ответить на ваши вопросы, поскольку не знаю ничего лучшего.