VBA将Excel图表复制到Word,因为图片会更改图表大小 [英] VBA copying Excel chart to Word as picture changes the chart size

查看:516
本文介绍了VBA将Excel图表复制到Word,因为图片会更改图表大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个宏,该宏从Excel复制图表并将其作为图片粘贴到Word中(最好是增强型图元文件)。

I want to create a macro that copies charts from Excel and pastes them into Word as pictures (preferrably Enhanced Metafiles).

我用表创建了一个Word模板文档,该表在应插入图片的特定单元格中包含书签。

I set up a Word template document with a table which contains bookmarks in specific cells where the pictures should be inserted.

但是,根据我当前的代码,插入的图像太大了,整个表弄糟了。
我尝试了不同的图片选项(增强的图元文件,png等),但是它们的结果都相同。

With my current code, however, the inserted image is way too big and screws up the whole table. I tried different picture options (enhanced metafile, png, etc.), but they all have the same result.

当我尝试通过以下方式复制图表时在表中使用 PasteSpecial 手,它保持原始大小,这正是我想要的大小。

When I try to copy the chart by hand using PasteSpecial in the table, it keeps the orginal size which is just how I want it.

要更改代码,我必须更改什么?

What do I have to change in my code to get that?

Sub CopyCharts2Word()

Dim wd As Object
Dim ObjDoc As Object
Dim FilePath As String
Dim FileName As String
FilePath = "C:\Users\Name\Desktop"
FileName = "Template.docx"


'check if template document is open in Word, otherwise open it
On Error Resume Next
Set wd = GetObject (, "Word.Application")    
If wd Is Nothing Then
    Set wd = CreateObject("Word.Application")
    Set ObjDoc = wd.Documents.Open(FilePath & "\" & FileName)
Else
    On Error GoTo notOpen
    Set ObjDoc = wd.Documents(FileName)
    GoTo OpenAlready
notOpen:
    Set ObjDoc = wd.Documents.Open(FilePath & "\" & FileName)
End If
OpenAlready:
On Error GoTo 0

'find Bookmark in template doc 
wd.Visible = True                                              
ObjDoc.Bookmarks("Boomark1").Select  

 'copy chart from Excel        
 Sheets("Sheet1").ChartObjects("ChartA").chart.ChartArea.Copy        

 'insert chart to Bookmark in template doc
 wd.Selection.PasteSpecial Link:=False, _
 DataType:=wdPasteMetafilePicture, _
 Placement:=wdInLine, _
 DisplayAsIcon:=False

 End Sub


推荐答案

是的,就是这样:

我替换了

'insert chart to Bookmark in template doc
wd.Selection.PasteSpecial Link:=False, _
DataType:=wdPasteMetafilePicture, _
Placement:=wdInLine, _
DisplayAsIcon:=False

wd.Selection.PasteSpecial Link:=False, _
DataType:=wdPasteMetafilePicture, _
Placement:=wdTight, _    
DisplayAsIcon:=False

这样,图表的大小与Excel工作表中的大小相同!

This way, the size of the Chart remains the same as in the Excel sheet!

这篇关于VBA将Excel图表复制到Word,因为图片会更改图表大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆