2016-12-15 126 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」。