VBA Word:更改图表数据 [英] VBA Word: Change Data of charts
问题描述
我想在Word文档中更改图表的数据,但是找不到正确的方法来处理图表.我尝试了几种技术,但没有任何效果. (我很想打开一个ExcelSheet,可以在其中更改数据)
I want to change the data of a chart in a Word Document, but I can't find the right way to address my charts. I tried several techniques, but nothing worked. (I´d love to open a ExcelSheet in which I can just change the Data)
因此,将它们放在一起:我想更改MS Word图表的数据(而不是源数据),它看起来像这样:
So to put it all together: I want to change the data (not the source), of a MS Word chart, which looks like that:
编辑(13.8.): 在请求之后,我尝试为您提供一些参考代码"以供使用.
Edit(13.8.): After request, I try to give you some "reference Code" to work with.
Sub ChangeChart()
Dim aktDocument As Document
Dim chrt As Chart
Dim SourceSheet As Excel.Worksheet
Set aktDocument = ActiveDocument
Set SourceSheet = aktDocument.Shapes(1).Chart.OpenSourceData 'I know it´s not that easy
SourceSheet.Range("B5") = newStuff
aktDocument.Shapes(1).Chart.SetSourceData = SourceSheet
End Sub
我知道这听起来有些荒谬可笑,但我只是不知道如何以正确的方式处理图表,甚至正确使用图表.
I know this may sounds utopic and ridiculous, but I just don´t know, how to address the chart in the right way, or to even work with it properly.
编辑(15.08):
即使在重新创建旧图表之后,以下代码也无法找到具有图表的形状.因此,当索引超出范围时,它将停止.
Even after recreating the old charts, the following code is not able to find a shape which has a chart. And therefore it stops when the index is out of range.
Sub Test()
i = 0
Do While i < 100
i = i + 1
If ActiveDocument.Shapes(i).HasChart Then
MsgBox "found one!"
End If
Loop
End Sub
解决方案(30.08.):
Solution(30.08.):
@Cindy Meister的回答是解决我的问题的方法.在进一步使用它之后,我遇到了一个问题,即在运行代码时,ChartData总是在屏幕上打开.
The answer from @Cindy Meister was the solution to my problem. After further working with it, I came to the problem, that the ChartData always opens on the screen, while running the code.
仅供参考,这是我的解决方法问题.
Just for reference this question was my workaround.
推荐答案
所有Office应用程序都使用Excel引擎来创建和管理图表.在Word中,可以使用文本或文本自动换行格式对图表进行行内格式化.在前一种情况下,图表对象需要通过InlineShapes
集合进行寻址,而在后一种情况下则需要通过Shapes
集合进行寻址.
All Office applications use the Excel engine to create and manage charts. In Word, charts can be formatted in-line with the text or with text wrap formatting. In the former case, a chart object needs to be addressed via the InlineShapes
collection, in the latter via the Shapes
collection.
由于您的示例代码使用了Shapes(1)
,因此我在下面的代码段中使用了它.如果不确定文档中的第一个Shape
是图表,但已为Shape
分配了名称,则可以将其用作索引值(例如Shapes("MyChart")
.收集并检查HasChart
.
Since your sample code uses Shapes(1)
I've used that in the code snippet below. If it's not certain that the first Shape
in the document is the chart, but you've assigned the Shape
a name, you can use that as the index value (for example Shapes("MyChart")
. Or you can loop the Shapes collection and check HasChart
.
HasChart
返回True.然后可以将Shape.Chart
设置为对象变量.可以使用Chart.ChartData.Activate
访问图表的数据-如果您不使用Activate
,则当图表的工作表存储在Word文档中时,将无法访问数据.只有这样,Chart.ChartData.Workbook
才能返回工作簿对象,然后可以使用ActiveSheet
访问该工作表.从那时起,就像使用Excel对象模型一样.
HasChart
returns True if the Shape
(or InlineShape
) is a Chart. It's then possible to set Shape.Chart
to an object variable. The chart's data can be accessed using Chart.ChartData.Activate
- if you don't use Activate
it's not possible to access the data when the chart's worksheet is stored in the Word document. Only then can Chart.ChartData.Workbook
return a workbook object, and through that the worksheet can be accessed using ActiveSheet
. From that point on, it's like working with the Excel object model.
Sub ChangeChart()
Dim aktDocument As Document
Dim shp As Word.Shape
Dim chrt As Word.Chart
Dim wb As Excel.Workbook, SourceSheet As Excel.Worksheet
Set aktDocument = ActiveDocument
Set shp = aktDocument.Shapes(1)
If shp.HasChart Then
Set chrt = shp.Chart
chrt.ChartData.Activate
Set wb = chrt.ChartData.Workbook
Set SourceSheet = wb.ActiveSheet
SourceSheet.Range("B5").Value2 = newData
End If
End Sub
这篇关于VBA Word:更改图表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!