在 MS Access 中设置 VBA Excel 图表图例位置属性不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7769659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 14:16:34  来源:igfitidea点击:

Setting VBA Excel Chart Legend Position Property in MS Access not working

excelms-accessvba

提问by Benjamin Brauer

This is a very strange issue I am facing for a while now, when creating some Excel worksheets programmatically from MS Access 2003.

这是我现在面临的一个非常奇怪的问题,当从 MS Access 2003 以编程方式创建一些 Excel 工作表时。

Using this VBA-Code snippet I am not able to set the Positionproperty of an Excel's Legendobject (variable definitions and so on are left out to ease understanding).

使用此 VBA 代码片段,我无法设置Excel 的Legend对象的Position属性(为了便于理解,省略了变量定义等)。

...
Set ChartObject = myWorksheet.ChartObjects.Add(myRange.Left, myRange.Top, myRange.width, myRange.Height)
Set Chart = ChartObject.Chart
Chart.HasLegend = True
'This line raises an error:
Chart.Legend.Position = -4107 '=xlLegendPositionBottom
...

MS Access always raises the Error 1004:

MS Access 总是引发错误 1004:

"Unable to set the Position property of the Legend class"

“无法设置 Legend 类的 Position 属性”

It confuses me that I can use the exact same code from within Exel VBA and it just works. What confuses me even more is that the property HasLegendcan be set whithout any error ocurring.

让我感到困惑的是,我可以在 Exel VBA 中使用完全相同的代码,而且它可以正常工作。更让我困惑的是,可以在不发生任何错误的情况下设置属性HasLegend

Someone has a hint to solve this issue?

有人有解决这个问题的提示吗?

采纳答案by Benjamin Brauer

After building a generic version of my code I found that in Office/Excel 2003 I have to populate my Series of data to the chart before changing the Legend's position. Probably the object hasn't been initiated before. My Code should therefore look like this:

在构建我的代码的通用版本后,我发现在 Office/Excel 2003 中,我必须在更改图例位置之前将我的数据系列填充到图表中。可能该对象之前尚未启动。因此,我的代码应如下所示:

...
Set ChartObject = myWorksheet.ChartObjects.Add(myRange.Left, myRange.Top, myRange.width,  myRange.Height)
Set Chart = ChartObject.Chart
Chart.HasLegend = True
...
Chart.SeriesCollection.Add myRange, 1 '=xlRows
'Now this works:
Chart.Legend.Position = -4107 '=xlLegendPositionBottom