按顺序从excel复制并粘贴到word

时间:2016-12-15 00:27:39

标签: excel vba charts ms-word

我正在尝试将24个图表从一个Excel工作表复制到特定顺序的word文档。这是我正在使用的代码:

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

WDApp.Selection.Range.PasteSpecial Link:=False, DataType:=wdPasteMetafilePicture, _
    Placement:=wdInLine, DisplayAsIcon:=False

WDDoc.Content.InsertParagraphAfter
Next

WDDoc.SaveAs ("C:\Users\Brooklyn.Bodnarchuk\Desktop\Shell I&C Mini Pilot Results\Test 0 - Nov 23 2016\charts.doc")
WDDoc.Close ' close the document


Set WDDoc = Nothing
Set WDApp = Nothing

End Sub

我的问题是它似乎以完全随机的顺序将图表添加到文档中。我将图表命名为“图表1”,“图表2”等。如何控制复制和粘贴这些文件的顺序?

1 个答案:

答案 0 :(得分:0)

如果您有图表,那么您可以使用以下内容:

For iCht = 1 to ActiveSheet.ChartObjects.Count

    ActiveSheet.ChartObjects("Chart " & iCht).Chart.CopyPicture _
        Appearance:=xlScreen, Size:=xlScreen, Format:=xlPicture
    ''etc

Next iCht

...假设工作表上的所有图表对象都命名为"图表x"按照您希望复制的顺序。