修改MS Access现代图表字体遇错:.Object属性不存在
Access现代图表VBA操作报错:找不到
.Object属性 我想要修改MS Access中「现代图表」的X轴和Y轴标签的字体样式,但该图表没有对应的设置属性。于是搜索到一段VBA代码,但执行Set myChart = Me.myGraph.Object时,Access提示图表没有.Object方法或属性。我已经添加了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
问题原因
你用的是Access的现代图表(Modern Chart),而原代码是针对旧版的**传统图表控件(Microsoft Graph Chart)**编写的。两种图表的对象模型完全不同,现代图表没有.Object属性,直接调用会报错。
解决方法
现代图表需要通过Chart属性访问底层对象,同时调整代码适配新的对象模型,修改后的代码示例:
Private Sub Form_Open(Cancel As Integer) Dim myChart As Object ' 用后期绑定避免版本依赖,或引用Microsoft Office 16.0 Object Library Dim axis As Object ' 现代图表用.Chart属性获取对象 Set myChart = Me.myGraph.Chart ' 设置数据标签字体 If myChart.SeriesCollection.Count > 0 Then With myChart.SeriesCollection(1).DataLabels.Font .Name = "Times New Roman" .Style = msoFontStyleNormal .Size = 8 End With End If ' 设置X轴(类别轴)刻度标签字体 Set axis = myChart.Axes(xlCategory) With axis.TickLabels.Font .Name = "Times New Roman" .Style = msoFontStyleNormal .Size = 8 End With ' 设置Y轴(值轴)刻度标签字体 Set axis = myChart.Axes(xlValue) With axis.TickLabels.Font .Name = "Times New Roman" .Style = msoFontStyleNormal .Size = 8 End With End Sub
额外注意事项
- 若使用早期绑定,需添加Microsoft Office 16.0 Object Library引用,而非仅Microsoft Graph库
- 现代图表的对象模型更贴近Excel图表,可参考Excel VBA的图表操作语法
- 原代码里的
"Times New Romans"是拼写错误,正确应为"Times New Roman"
内容的提问来源于stack exchange,提问作者CrimsonGhost
相关产品推荐
相关产品推荐

