开发者

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

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

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

...
开发者_Go百科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:

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

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 HasLegend can be set whithout any error ocurring.

Someone has a hint to solve this issue?


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:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜