PowerPoint VBA: 从已打开的 Excel 文件中获取数据

PowerPoint VBA: get data from already open Excel file

通过单击 PowerPoint 中的按钮,我想从当前打开的 Excel 工作簿中获取一些单元格值(单击按钮时打开的工作簿)。

在 PowerPoint VBA 中,Microsoft Excel 16.0 对象库已启用。

但是运行:

sss = ActiveWorkbook.Sheets(1).Range("A1")
MsgBox sss

出现此错误:

Run-time error '91': Object variable or With block variable not set

如何从活动工作簿中请求单元格值?

您需要限定您的工作簿参考。首先,按照@Cindy Meister 的建议,使用 GetObject 获取 Excel 的现有实例。

Dim excelApplication As Excel.Application
Set excelApplication = GetObject(, "Excel.Application")

不过,对于后期绑定,您可以放弃对 Microsoft Excel 对象库的引用,并将应用程序变量声明为 Object.

Dim excelApplication As Object
Set excelApplication = GetObject(, "Excel.Application")

然后,按如下方式限定您的工作簿参考...

Dim ss As String
ss = excelApplication.ActiveWorkbook.Sheets(1).Range("A1").Value

所以你会有以下...

Dim excelApplication As Object
Set excelApplication = GetObject(, "Excel.Application")

Dim ss As String
ss = excelApplication.ActiveWorkbook.Sheets(1).Range("A1").Value

MsgBox ss, vbInformation

Set excelApplication = Nothing