2016-12-15 218 views
-1

我试图从一个excel工作表中复制24个图形到一个特定顺序的word文档。这是我正在使用的代码:从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”等的图表。我怎样才能控制这些复制和粘贴的顺序?

回答

0

如果你有一个名为那么你可以使用类似的图表:

For iCht = 1 to ActiveSheet.ChartObjects.Count 

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

Next iCht 

...假设表中的所有chartobjects你希望他们能够被复制的顺序被命名为“表X”。