vba 在 PowerPoint 2010 中静默编辑嵌入的 Excel 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6456236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Edit embedded excel data silently, in PowerPoint 2010
提问by Steven
I am trying to edit embedded excel data silently in PowerPoint 2010. Unfortunately when you use:
我正在尝试在 PowerPoint 2010 中静默编辑嵌入的 excel 数据。不幸的是,当您使用:
gChartData.Activate
It opens the Excel document over the presentation. Is there a way to activate the ChartData without opening Excel?
它会在演示文稿上打开 Excel 文档。有没有办法在不打开 Excel 的情况下激活 ChartData?
Full Code:
完整代码:
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
Thanks in advance.
提前致谢。
回答by brettdj
Steven,
史蒂文,
While the Activate
line is necessary to get access to the underlying Workbook adding a simple gWorkBook.Close
to your code (before setting it to Nothing) will close Excel again rather than leave it on top as your current code does.
虽然该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