问题
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.
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 ThisPresentation
equivalent in PowerPoint.
For a standard PowerPoint presentation or template, I could use ActivePresentation
. However, this method won't work for an add-in.
Any ideas? Please, no suggestions about where else I should stick the version number :-)
回答1:
REVISED FEB 2, 2010: Cleaned up answer to only show the final solution
Here's the way to do what was asked, no DLLs. Really simple:
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
回答2:
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.
Here is my code (change MyProject into your own projectname):
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
回答3:
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:
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
来源:https://stackoverflow.com/questions/1472418/how-to-simulate-thispresentation-in-powerpoint-vba