在 Word、VBA 中粘贴 Excel 图表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17840350/
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
Paste Excel chart in word, VBA
提问by mr.M
I am using Windows XP SP 3. I have written code to paste several charts from Excel 2003 to Word 2003.
我使用的是 Windows XP SP 3。我编写了代码将几个图表从 Excel 2003 粘贴到 Word 2003。
Dim word As Object
Dim doc As Object
On Error Resume Next
Set word = GetObject(, "word.application") 'gives error 429 if Word is not open
If Err = 429 Then
Set word = CreateObject("word.application") 'creates a Word application
Err.Clear
End If
With word
.Visible = True
.Documents.Add
End With
Sheets("Data").Select
For i = 1 To 2
ActiveSheet.ChartObjects(i).Activate
ActiveChart.ChartArea.Copy
With word.Selection
'Paste Chart
.Range.PasteSpecial
End With
Next i
I would like to understand, how can I place the charts from Excel 2003 into created word file in different places? E.x. I would like to place 4 charts in the following order: 1st chart is alligned to the left end of the document (not paragraph), 2nd chart is alligned to the left side of the document, the 3d lies below the 1st, same for the 4th.
我想知道,如何将 Excel 2003 中的图表放入不同位置的创建的 word 文件中?例如,我想按以下顺序放置 4 个图表:第 1 个图表对齐到文档的左端(不是段落),第 2 个图表对齐到文档的左侧,3d 位于第 1 个下方,相同第四。
Thank you for your answers!
谢谢您的回答!
UPD: based on the usefull comments, I have outlined the following solution for my problem. Create template file and insert there a bookmark, named insertHere
. The make changes by using Excel VBA in this file.
UPD:根据有用的评论,我为我的问题概述了以下解决方案。创建模板文件并在其中插入一个名为insertHere
. 在此文件中使用 Excel VBA 进行更改。
Here is the code for this
这是代码
Sub macro()
Dim word As Object
On Error Resume Next
Set word = GetObject(, "word.application") 'gives error 429 if Word is not open
If Err = 429 Then
Set word = CreateObject("word.application") 'creates a Word application
Err.Clear
End If
Set templateFile = word.documents.Add(Template:="C:\Users\PC\Desktop\Doc4.dot")
Sheets("Data").Select
ActiveSheet.ChartObjects(1).Activate
ActiveSheet.ChartObject(1).Select
ActiveChart.ChartArea.Copy
With templateFile.Bookmarks
.Item("insertHere").Range.Paste
End With
End Sub
However, this code doesn't insert the chart. Can you give me a hint why?
但是,此代码不会插入图表。你能给我一个提示为什么吗?
回答by Andy G
The easiest way is to define Bookmarks
in Word for the locations that you wish to paste the charts to. In Word 2003, if I recall correctly, the option is on the Insert menu, Bookmark. Position the cursor firstly, Insert, Bookmark (Ctrl-Shift-F5) give it a name such as bkChart1 (without spaces). Then:
最简单的方法是Bookmarks
在 Word 中定义要将图表粘贴到的位置。在 Word 2003 中,如果我没记错的话,该选项位于“插入”菜单中的“书签”上。首先定位光标,插入,书签(Ctrl-Shift-F5)给它一个名称,例如 bkChart1(不带空格)。然后:
word.ActiveDocument.Bookmarks("bkChart1").Range.PasteAndFormat
' there are other Paste methods, or PasteSpecial
If you prefer to format the document, and the pasted object, using code then you would do well to record a few macros in Word. It won't create perfect code, but it will help you to discover the methods and properties that you need.
如果您更喜欢使用代码格式化文档和粘贴的对象,那么最好在 Word 中记录一些宏。它不会创建完美的代码,但会帮助您发现所需的方法和属性。