我有代码打开Word文档并浏览图表,更新数据。
问题是我连续多次调用此宏。即使我关闭Word应用程序,图表数据窗口仍保持打开状态。
Excel崩溃而没有告诉我为什么,但E认为问题是图表数据窗口没有关闭。因为如果我只运行一次宏,它就会起作用。
但是,如果图表不支持此属性,如何关闭chartdata窗口?
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim wdShape As InlineShape
Dim wdChart As Word.Chart
Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True
'Opening the document
Set wdDoc = wdApp.Documents.Open("path_here")
'Opening the chartdata window
Set wdShape = wdDoc.InlineShapes(1)
Set wdChart = wdShape.Chart
wdChart.ChartData.Activate
'Changing the data
Range("B2").Value = 120
Range("B3").Value = 155
'Closing the app
wdApp.Quit SaveChanges:=wdSaveChanges
Set wdShape = Nothing
Set wdChart = Nothing
Set wdApp = Nothing
Set wdDoc = Nothing
答案 0 :(得分:1)
此代码将更改数据中的值而不激活chartdata窗口。由于某种原因,wdChart变量抛出了一个常量赋值错误,因此我将其更改为wdCh。
Set wdShape = wdDoc.InlineShapes(1)
Set wdCh = wdShape.Chart
With wdCh.ChartData.Workbook.Sheets(1)
.Range("B2").Value = 120
.Range("B3").Value = 155
End With