简体   繁体   English

如何使用 VBA 将 Excel 图表复制并粘贴到 Word?

[英]How to copy and paste Excel chart to Word using VBA?

I wish to copy Excel charts into Word using VBA.我希望使用 VBA 将 Excel 图表复制到 Word 中。

I use a bookmark to position the first chart and that works fine.我使用书签来定位第一个图表,效果很好。 I wish to position the next chart alongside the first one (ie in the middle of the page) and I can't find how to do that.我希望将下一个图表与第一个图表放在一起(即在页面中间),但我找不到如何做到这一点。

I've set up a bookmark in the middle of the page but VBA just pastes the chart to the left margin.我在页面中间设置了一个书签,但 VBA 只是将图表粘贴到左边距。 The Word macro recorder generates no useable code for this operation. Word 宏记录器不会为此操作生成任何可用代码。

I have no experience with VBA in Word.我没有在 Word 中使用 VBA 的经验。 Can anyone help?任何人都可以帮忙吗?

I've tried a variety of options, the one below works for the first chart我尝试了多种选择,下面的一个适用于第一个图表

'The first chart copies OK to Bookmark "Change"

ws.ChartObjects(1).Copy
doc.Bookmarks("Change").Range.PasteSpecial _
    Link:=False, _
    DataType:=wdPasteEnhancedMetafile, _
    Placement:=wdFloatOverText, _
    DisplayAsIcon:=False

Bookmark DemandChange is in the middle of the page, the second chart pastes on top of the first chart (ieat left margin). Bookmark DemandChange位于页面中间,第二个图表粘贴在第一个图表的顶部(即左边距)。

ws.ChartObjects(2).Copy
doc.Bookmarks("DemandChange").Select
wd.Selection.PasteSpecial _
    Link:=False, _
    DataType:=wdPasteEnhancedMetafile, _
    Placement:=wdFloatOverText, _
    DisplayAsIcon:=False

The issue you are having relates to the Placement property.您遇到的问题与 Placement 属性有关。 You have set it to wdFloatOverText which means that it will be anchored to the bookmark but not sit next to it.您已将其设置为wdFloatOverText ,这意味着它将锚定到书签但不会坐在它旁边。 To place the two charts side by side you need the chart to be inline.要将两个图表并排放置,您需要将图表内联。

doc.Bookmarks("Change").Range.PasteSpecial _
    Link:=False, _
    DataType:=wdPasteEnhancedMetafile, _
    Placement:=wdInline, _
    DisplayAsIcon:=False

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

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