如何在 PowerPoint VBA 中模拟 ThisPresentation

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

How to simulate ThisPresentation in PowerPoint VBA

vbapowerpointpowerpoint-vba

提问by Gary McGill

I would like to be able to access the document properties of a PowerPoint add-in file (a presentation saved as "PowerPoint Add-in (*.ppa)", from some VBA code in the add-in itself.

我希望能够通过加载项本身的一些 VBA 代码访问 PowerPoint 加载项文件(保存为“PowerPoint 加载项 (*.ppa)”的演示文稿)的文档属性。

If it helps to understand the problem, what I'm actually trying to do is read a custom document property that stores the version number of the add-in, so that I can display that in a dialog box.

如果它有助于理解问题,我实际尝试做的是读取存储加载项版本号的自定义文档属性,以便我可以在对话框中显示它。

With Word & Excel I can do this using ThisDocument& ThisWorkbook, both of which return a reference to the document containing the running code. However, there is no ThisPresentationequivalent in PowerPoint.

在 Word 和 Excel 中,我可以使用ThisDocument&执行此操作ThisWorkbook,它们都返回对包含正在运行的代码的文档的引用。但是,ThisPresentationPowerPoint 中没有等效项。

For a standard PowerPoint presentation or template, I could use ActivePresentation. However, this method won't work for an add-in.

对于标准的 PowerPoint 演示文稿或模板,我可以使用ActivePresentation. 但是,此方法不适用于加载项。

Any ideas? Please, no suggestions about where else I should stick the version number :-)

有任何想法吗?拜托,没有关于我应该在哪里贴版本号的建议:-)

采纳答案by Todd Main

REVISED FEB 2, 2010: Cleaned up answer to only show the final solution

2010 年 2 月 2 日修订:清理答案以仅显示最终解决方案



Here's the way to do what was asked, no DLLs. Really simple:

这是执行要求的方法,没有 DLL。真的很简单:

Sub ReturnPPAasPresentation()
    Dim p As Presentation
    Set p = Presentations("presentation1.ppa")
    Dim title As String, version As String
    version = p.CustomDocumentProperties("Version").Value
    title = p.BuiltInDocumentProperties("Title").Value
    MsgBox "Version: " & version & " of " & title, vbOKOnly, title
End Sub

回答by macnerd

Like everyone else I expected a ThisPresentation object in PowerPoint. I thought of another way to accomplish it, without a hardcoded filename. Obviously any piece of code would need to know how to distinguish between the projects. I chose to use the projectname for this (default name "VBAProject" in the Project Explorer): it is not used for anything else, no user will change it and if it is protected they can't.

和其他人一样,我希望 PowerPoint 中有一个 ThisPresentation 对象。我想到了另一种方法来完成它,没有硬编码的文件名。显然,任何一段代码都需要知道如何区分项目。我选择为此使用项目名称(项目资源管理器中的默认名称“VBAProject”):它不用于其他任何事情,没有用户会更改它,如果它受到保护,他们就不能。

Here is my code (change MyProject into your own projectname):

这是我的代码(将 MyProject 更改为您自己的项目名称):

Function ThisPresentation() As Presentation
Dim p As Presentation

For Each p In Presentations
    If p.VBProject.Name = "MyProject" Then
        Set ThisPresentation = p
        Exit Function
    End If
Next
End Function

回答by Tahlor

Credit goes to macnerd nerd for the general idea, but added AddIn functionality. Unfortunately, AddIns don't have VBProject names, so not quite as robust:

总体思路归功于 macnerd nerd,但添加了 AddIn 功能。不幸的是,AddIns 没有 VBProject 名称,所以不是很健壮:

Function ThisPresentation(project_name As String) As Object
Dim p As Object

all_presentations = Array(Application.AddIns, Application.Presentations)
For Each pArray In all_presentations
    For Each p In pArray
        Debug.Print p.FullName
        If InStr(p.FullName, project_name) > 0 Then
            Set ThisPresentation = p
            Exit Function
        End If
    Next
Next
End Function