简体   繁体   English

如何将宏添加到Word 2010或PowerPoint 2010图表?

[英]How can I add a macro to a Word 2010 or PowerPoint 2010 chart?

In PowerPoint 2010 or Word 2010, when I choose Insert -> Chart, it creates a new chart with an Excel worksheet for the data. 在PowerPoint 2010或Word 2010中,当我选择“插入” - >“图表”时,它会创建一个带有Excel工作表的新图表。

If I add a macro to the Excel worksheet, this seems to be discarded when I close the worksheet and re-open it. 如果我将一个宏添加到Excel工作表,当我关闭工作表并重新打开它时,这似乎被丢弃了。

However, it is possible to have a chart with macros, because I also have some Word documents & PowerPoint presentations that I created in Office 2003, which had embedded Excel charts with macros. 但是,它可能有宏的图表,因为我也有一些Word文档和我在Office 2003中,它已经嵌入Excel图表与宏创建PowerPoint演示文稿。 When I converted those to Office 2010, they look just like "normal" Office 2010 charts, but the macros are preserved. 当我将它们转换为Office 2010时,它们看起来就像“普通”Office 2010图表,但宏保留了下来。

It looks to me like the embedded chart that gets created when you choose Insert -> Chart is in the "pptx" format rather than the "pptm" format, and so macros are not saved. 在我看来,当您选择插入时创建的嵌入式图表 - >图表是“pptx”格式而不是“pptm”格式,因此不会保存宏。

(If I query ActiveWorkbook.FileFormat, I get "51", which is "Open Xml Spreadsheet"; this is indeed "pptx", as opposed to "Open Xml Spreadsheet with Macros", which is "52"). (如果我查询ActiveWorkbook.FileFormat,我得到“51”,这是“Open Xml Spreadsheet”;这确实是“pptx”,而不是“用Mac打开Xml电子表格”,即“52”)。

How can I insert a chart with the "pptm" behavior? 如何插入带有“pptm”行为的图表? Or change the behavior of an existing chart? 或者改变现有图表的行为?


NOTE: I do not want to insert a chart "object", since this means that the chart is not editable within the host application (it merely inserts an Excel chart that you need to "open" in order to edit it). 注意:我不想插入图表“对象”,因为这意味着图表在主机应用程序中不可编辑(它只是插入一个需要“打开”以便编辑它的Excel图表)。


Let me give you a head start... I can achieve what I want by the following method: 让我先行一步......我可以通过以下方法实现我想要的目标:

  1. Create a new document in Word (or PowerPoint). 在Word(或PowerPoint)中创建一个新文档。
  2. Save the document as a 97-2003 document (which forces it into compatibility mode). 将文档另存为97-2003文档(强制它进入兼容模式)。
  3. Insert a chart using Insert->Object->Microsoft Excel Chart. 使用“插入” - >“对​​象” - >“Microsoft Excel图表”插入图表。 This embeds an old-style Excel chart. 这嵌入了旧式的Excel图表。
  4. Open the embedded chart by right-click->Open, and add a macro. 右键单击 - >打开打开嵌入式图表,然后添加宏。
  5. Close the chart, then choose File->Info and use the Compatibility Mode "Convert" button to convert the document into an Office 2010 document. 关闭图表,然后选择文件 - >信息并使用兼容模式“转换”按钮将文档转换为Office 2010文档。

Now you have a "native" Word or PowerPoint 2010 chart, which you can edit directly in Word or PowerPoint. 现在您有一个“本机”Word或PowerPoint 2010图表,您可以直接在Word或PowerPoint中编辑它。 If you click on the chart and choose Chart Tools->Design->Edit Data, then the Excel worksheet that opens has the macros you created in step 4. Success! 如果单击图表并选择图表工具 - >设计 - >编辑数据,则打开的Excel工作表将包含您在步骤4中创建的宏。成功!

Now, does anyone have a simpler way? 现在,有没有人有更简单的方法?

In my opinion you could achieve what you need in the following way: 在我看来,您可以通过以下方式实现您的需求:

(unfortunately, I can't present it with pictures and exact English commands as I'm using local-not English-version of Office) (不幸的是,我不能用图片和确切的英文命令来呈现它,因为我使用的是本地 - 而不是英文版的Office)

  • Choose Insert >> Object >> Object... 选择插入>>对象>>对象...
  • In Object window take first page (like Create new ) 对象窗口中取第一页(如新建
  • Search for Microsoft Excel Chart (or something similar) and press OK 搜索Microsoft Excel图表 (或类似的东西),然后按确定
  • You will get Workbook with two sheets as presented on the picture below 您将获得两张工作簿,如下图所示

在此输入图像描述

(there are chart editing feature available on the Ribbon) (功能区上有图表编辑功能)

  • Press Alt+F11 while you are in Chart edition like presented in the picture above. 在图表版本中按Alt + F11 ,如上图所示。 You will get IDE for Excel opened where you will find appropriate workbook. 您将获得IDE for Excel,您将在其中找到合适的工作簿。
  • Add new module and macro there 在那里添加新模块和宏
  • Now you could exit chart edition in Word and save document as *.docm 现在,您可以在Word中退出图表版本并将文档另存为*.docm
  • After you will reopen it , go to Chart Edition (double click) >> Alt+F11 >> you will find you macro there saved within the workbook with *docm extension. 重新打开之后 ,转到Chart Edition(双击)>> Alt + F11 >> 您将在工作簿中找到保存为*docm扩展名的宏。

Now that the bounty has passed (with no better answers), I'll add the partial answer from my question, in order to be able to close the question. 既然赏金已经过去了(没有更好的答案),我会从我的问题中添加部分答案,以便能够结束这个问题。


  1. Create a new document in Word (or PowerPoint). 在Word(或PowerPoint)中创建一个新文档。
  2. Save the document as a 97-2003 document (which forces it into compatibility mode). 将文档另存为97-2003文档(强制它进入兼容模式)。
  3. Insert a chart using Insert->Object->Microsoft Excel Chart. 使用“插入” - >“对​​象” - >“Microsoft Excel图表”插入图表。 This embeds an old-style Excel chart. 这嵌入了旧式的Excel图表。
  4. Open the embedded chart by right-click->Open, and add a macro. 右键单击 - >打开打开嵌入式图表,然后添加宏。
  5. Close the chart, then choose File->Info and use the Compatibility Mode "Convert" button to convert the document into an Office 2010 document. 关闭图表,然后选择文件 - >信息并使用兼容模式“转换”按钮将文档转换为Office 2010文档。

Now you have a "native" Word or PowerPoint 2010 chart, which you can edit directly in Word or PowerPoint. 现在您有一个“本机”Word或PowerPoint 2010图表,您可以直接在Word或PowerPoint中编辑它。 If you click on the chart and choose Chart Tools->Design->Edit Data, then the Excel worksheet that opens has the macros you created in step 4. Success! 如果单击图表并选择图表工具 - >设计 - >编辑数据,则打开的Excel工作表将包含您在步骤4中创建的宏。成功!

I'm still interested in being able to do this more directly... 我仍然有兴趣能够更直接地做到这一点......

The issue is with how the charts data is held within the application. 问题在于如何在应用程序中保存图表数据。 Converting and originating are two different processes. 转换和发起是两个不同的过程。 Your data series is basically an array not an excel sheet. 您的数据系列基本上是一个数组而不是Excel工作表。 It just uses excel to display the data to you to edit. 它只是使用excel来显示要编辑的数据。 You need to keep your code in the module for powerpoint/word and access the chart via the shapes object if you do not want to embed a excel sheet. 如果您不想嵌入Excel工作表,则需要将代码保存在powerpoint / word模块中,并通过形状对象访问图表。

Sub GetChartName()
Dim sSlide As Slide
Dim cChart As Chart
Dim sShape As Shape

Set sSlide = PowerPoint.ActivePresentation.Slides(1)
    For Each sShape In sSlide.Shapes
        If sShape.HasChart Then
            Set cChart = sShape.Chart
                MsgBox cChart.Name
            Set cChart = Nothing
        End If
    Next
Set sSlide = Nothing

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM