vba 将多个图表复制到word文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15636713/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:15:16  来源:igfitidea点击:

copy multiple charts to word document

excel-vbams-wordword-vbavbaexcel

提问by Jakob

I'm trying to copy a series of charts in one sheet to one document in word, but for some reason I only get the latest paste (meaning the last chart on the sheet). I know that the iteration goes through all charts, becausewhen I modofiy the code to print a single word doc for each chart it does so, but I want the charts together, so please help me out

我正在尝试将一张工作表中的一系列图表复制到 Word 中的一个文档中,但由于某种原因,我只能获得最新的粘贴(即工作表上的最后一个图表)。我知道迭代会遍历所有图表,因为当我修改代码以为每个图表打印一个 word doc 时,它会这样做,但我希望将这些图表放在一起,所以请帮助我

The code:

编码:

Sub ChartsToWord()

Dim WDApp As Word.Application
Dim WDDoc As Word.Document
Dim iCht As Integer
Dim Msg As String

Set WDApp = CreateObject("Word.Application")
Set WDDoc = WDApp.Documents.Add

For iCht = 1 To ActiveSheet.ChartObjects.Count
    ' copy chart as a picture
    ActiveSheet.ChartObjects(iCht).Chart.CopyPicture _
        Appearance:=xlScreen, Size:=xlScreen, Format:=xlPicture


    WDDoc.Content.PasteSpecial Link:=False, DataType:=wdPasteMetafilePicture, _
        Placement:=wdInLine, DisplayAsIcon:=False

    WDDoc.Content.InsertParagraphAfter
Next
WDDoc.SaveAs ("C:\Users\confidential\Documents\charts.doc")
    WDDoc.Close ' close the document

' Clean up
    Set WDDoc = Nothing
    Set WDApp = Nothing

End Sub

回答by Kazimierz Jawor

Please replace beginning of PasteSpecialline into:

请将行首替换PasteSpecial为:

WDApp.Selection.Range.PasteSpecial ... 'and so on

In your situation you paste chart into whole document instead of current paragraph.

在您的情况下,您将图表粘贴到整个文档而不是当前段落中。

One more suggestion. You could use the following to insert new paragraph:

还有一个建议。您可以使用以下内容插入新段落:

WDApp.Selection.MoveEnd wdStory
WDApp.Selection.Move