在 Windows 7 上打开 PowerPoint 演示文稿时 VBA 失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20282386/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 00:43:49  来源:igfitidea点击:

VBA fails when opening PowerPoint presentation on Windows 7

vbaexcel-vbawindows-7powerpoint-vbaexcel

提问by Andrew Kew

I wrote a VBA macro in Excel 2007 on Windows XP to copy data from an excel spreadsheet into a powerpoint presentation. When this macro enabled spreadsheet was run on a fresh install of Windows 7 it fails. So I pulled out the code that fails to pin point the problem and it seems to fail when trying to open an existing powerpoint file. I have tried running this code in both Office 2010 and Office 2007.

我在 Windows XP 上的 Excel 2007 中编写了一个 VBA 宏,以将数据从 Excel 电子表格复制到 Powerpoint 演示文稿中。当这个启用宏的电子表格在全新安装的 Windows 7 上运行时,它会失败。因此,我提取了无法指出问题的代码,并且在尝试打开现有的 powerpoint 文件时似乎失败了。我曾尝试在 Office 2010 和 Office 2007 中运行此代码。

The code I am trying to use it (just the problem parts shown below)

我正在尝试使用它的代码(只是下面显示的问题部分)

Sub test()
   Dim PowerPointApplication As PowerPoint.Application
   Dim PowerPointFile As PowerPoint.Presentation

   Set PowerPointApplication = CreateObject("PowerPoint.Application")
   Set PowerPointFile = PowerPointApplication.Presentations.Open("PATH_TO_FILE\test.pptx")
End Sub

The macro fails on the Presentations.Open line above with the following error

宏在上面的 Presentations.Open 行上失败,并出现以下错误

Run-time error '-2147467259 (80004005)':
Method 'Open' of object 'Presentations' failed

I have already enabled the PowerPoint 12.0 Object Library in the references settings in the VBEditor for the spreadsheet. All the other references match exactly with the file that runs without error on my Windows XP box.

我已经在电子表格的 VBEditor 的参考设置中启用了 PowerPoint 12.0 对象库。所有其他引用与在我的 Windows XP 机器上运行时没有错误的文件完全匹配。

I have looked all over the web for an answer and cant find anything. I read something about Windows 7 and offline files, so tried turning that off but it didnt help.

我在网上到处寻找答案,但找不到任何东西。我阅读了有关 Windows 7 和脱机文件的一些内容,因此尝试将其关闭,但没有帮助。

I am logged in as an administrator user as well, and tried moving the pptx that I am opening to other directories as well with no success.

我也以管理员用户身份登录,并尝试将我打开的 pptx 也移动到其他目录,但没有成功。

I am running the following version of Windows:

我正在运行以下版本的 Windows:

Windows 7 Professional
Service Pack 1
64 Bit

Any help would be appreciated!

任何帮助,将不胜感激!

回答by John Wilson

Is PATH_TO_FILE a variable (or constant)??

PATH_TO_FILE 是变量(或常量)吗??

If so shouldn't it be

如果是这样不应该

PowerPointFile = PowerPointApplication.Presentations.Open(PATH_TO_FILE & "\test.pptx")

PowerPointFile = PowerPointApplication.Presentations.Open(PATH_TO_FILE & "\test.pptx")

回答by mooseman

This does work in office 2016 on Win7 SP1 64bit OS

这在 Win7 SP1 64 位操作系统上的 Office 2016 中确实有效

Should be good for Office 2010.

应该适用于 Office 2010。

Could be John's advise on the path to file, also.

也可能是约翰关于文件路径的建议。

Sub test()
Dim PowerPointFile As PowerPoint.Presentation
Dim PPTObj As Object
Set PPTObj = CreateObject("PowerPoint.application")

Set PowerPointFile = PPTObj.Presentations.Open("C:\test.pptx")

End Sub