![](/img/trans.png)
[英]Via Excel VBA, opening Word Document to Complete Mail-merge. Is there way to Automatically set Word Doc Source equal to Excel Spreadsheet?
[英]Creating Chart in Word Doc With Spreadsheet Source Data Via VBA
我正在嘗試通過VBA添加條形圖來修改Word文檔模板。 源數據來自現有的Excel電子表格。 我正在Excel的VBE中執行此操作。 但是,此代碼僅在電子表格上創建一個新圖表。 它不會將其添加到我的Word文檔中。 如何將圖表直接納入文檔?
Option Explicit
Sub UpdateChartData()
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim cht As Chart
Dim ws1 As Worksheet
Set wdApp = CreateObject("Word.Application")
Set wdDoc = wdApp.Documents.Add("C:\...\chart_test.docx")
Set cht = Charts.Add
'' Worksheet of the source data.
Set ws1 = ActiveWorkbook.Sheets("Sheet1")
'' Go to the Word bookmark where the chart will be inserted.
wdDoc.GoTo what:=-1, Name:="insert_chart"
With cht
'' Source data range.
.SetSourceData Source:=ws1.Range("A2:C12")
.ChartType = xlColumnClustered
End With
With wdApp
.Visible = True
.Activate
End With
End Sub
得到它了。 只需添加以下內容。
cht.CopyPicture
wdDoc.Bookmarks("insert_chart").Range.Paste
cht.Copy
將不會在文檔上顯示圖表。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.