从 Powerpoint 在 VBA 中打开 Excel 文件 [英] Open Excel file in VBA from Powerpoint

查看:75
本文介绍了从 Powerpoint 在 VBA 中打开 Excel 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试借助以下代码在 Powerpoint 2010 中使用 VBA 打开 Excel 文件.

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

但是我收到以下错误.

编译错误未定义用户定义的类型.

Compile Error User Defined type not defined.

我是不是遗漏了什么.任何人都可以共享示例代码段以使用 VBA 在 Powerpoint 2007 和 2010 中打开 Excel 文件、更改单元格值和关闭 Excel 文件.

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. :(

提前致谢.:)

推荐答案

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

Have you added a reference to the Excel Object Model? That would save you having to use the late bound objects (and you get the benefit of having the Intellisense help when you are coding).

您需要转到工具"->参考"并检查Microsoft Excel v.x 对象库"(我认为该数字会因您使用的 Office 版本而异.

You need to go to Tools -> References and check the "Microsoft Excel v.x Object Library" (I think that number changes depending on the version of office you are using.

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

Your code should work if you do that, you should also remove the

CreateObject("Excel.Application") 

行并替换为

Set xlApp = new Excel.Application

然后移动

Set xlApp = nothing

行到子程序的结尾.

你的其余代码在我看来还不错.

The rest of your code looks fine to me.

这篇关于从 Powerpoint 在 VBA 中打开 Excel 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆