在PowerPoint 2010中以静默方式编辑嵌入式Excel数据

时间:2011-06-23 15:04:20

标签: excel vba powerpoint

我正在尝试在PowerPoint 2010中静默编辑嵌入式Excel数据。不幸的是,当您使用:

gChartData.Activate

它在演示文稿上打开Excel文档。有没有办法在不打开Excel的情况下激活ChartData?

完整代码:

Private Sub CommandButton1_Click()

Dim myChart As Chart
Dim gChartData As ChartData
Dim gWorkBook As Excel.Workbook
Dim gWorkSheet As Excel.Worksheet

Set myChart = ActivePresentation.Slides(1).Shapes(1).Chart
Set gChartData = myChart.ChartData

gChartData.Activate

Set gWorkBook = gChartData.Workbook

Set gWorkSheet = gWorkBook.Worksheets(1)

gWorkSheet.Range("B2").Value = 1

Set gWorkSheet = Nothing
Set gWorkBook = Nothing
Set gChartData = Nothing
Set myChart = Nothing


End Sub

提前致谢。

1 个答案:

答案 0 :(得分:3)

史蒂芬,

虽然Activate行是访问基础工作簿所必需的,但是在您的代码中添加一个简单的gWorkBook.Close(在将其设置为Nothing之前)将再次关闭Excel,而不是将其保留在顶部作为您的目前的代码。

Private Sub CommandButton1_Click()

    Dim myChart As Chart
    Dim myChartData As ChartData
    Dim gWorkBook As Excel.Workbook
    Dim gWorkSheet As Excel.Worksheet

    Set myChart = ActivePresentation.Slides(1).Shapes(1).Chart
    Set myChartData = myChart.ChartData
    myChartData.Activate

    Set gWorkBook = myChart.ChartData.Workbook
    Set gWorkSheet = gWorkBook.Worksheets(1)
    gWorkSheet.Range("B2").Value = 1

    gWorkBook.Close
    Set gWorkSheet = Nothing
    Set gWorkBook = Nothing
    Set gChartData = Nothing
    Set myChart = Nothing
End Sub