简体   繁体   中英

How to copy and paste Excel chart to Word using VBA?

I wish to copy Excel charts into Word using VBA.

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. The Word macro recorder generates no useable code for this operation.

I have no experience with VBA in Word. 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).

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. You have set it to wdFloatOverText which means that it will be anchored to the bookmark but not sit next to it. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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