7

I'm trying to open the Excel file using VBA in Powerpoint 2010 with the help of following code.

Private Sub CommandButton1_Click()
Dim xlApp As Excel.Application
Set xlApp = CreateObject("Excel.Application")

xlApp.Visible = True

xlApp.Workbooks.Open "C:\lol\Book1.xlsx", True, False
Set xlApp = Nothing

Range("A8").Value = "Hello"
End

But I'm getting the following error.

Compile Error User Defined type not defined.

Am I missing something. Can anyone share the sample piece of code to open an excel file, change a cell value and close Excel file in Powerpoint 2007 and 2010 using VBA.

I have searched a lot and tried different pieces of code, but getting the same error everytime. :(

Thanks in advance. :)

4

2 回答 2

9

您是否添加了对 Excel 对象模型的引用?这将使您不必使用后期绑定的对象(并且您可以在编码时获得 Intellisense 帮助的好处)。

您需要转到工具 -> 参考并检查“Microsoft Excel vx 对象库”(我认为该数字会根据您使用的办公室版本而变化。

如果您这样做,您的代码应该可以工作,您还应该删除

CreateObject("Excel.Application") 

行并将其替换为

Set xlApp = new Excel.Application

并移动

Set xlApp = nothing

行到子程序的末尾。

你的其余代码对我来说看起来不错。

于 2012-05-26T04:20:22.220 回答
4

后期绑定代码是这样的

Private Sub test()
Dim xlApp As Object
Dim xlWorkBook As Object

Set xlApp = CreateObject("Excel.Application")

xlApp.Visible = True
Set xlWorkbook = xlApp.Workbooks.Open("C:\lol\Book1.xlsx", True, False)
xlWorkbook.sheets(1).Range("A8").Value = "Hello"

Set xlApp = Nothing
Set xlWorkbook = Nothing


End Sub

不过最好使用早期绑定。

于 2012-05-26T07:02:03.150 回答