我正在用 VBA 开发一个应用程序。用户窗体连接到读取 SPSS Statistics SAV 文件或 SPSS Dimensions MDD 文件的 COM 对象。
此应用程序的一部分将元数据存储在 XML 文档中,以便我们稍后可以检索元数据并重新填充或更新从用户表单创建的图形。只要我们依赖于本地驱动器上存在的 XML 文件,这就可以正常工作——这不是一个理想的解决方案。我们更愿意将 XML 嵌入(而不是链接)到 PPTM 文件中,我已经能够做到这一点(见附件)。
问题是我找不到让 VBA 成功提取 OLEObject XML 文件的方法。
OLEObject 可以从 PPT 手动打开(鼠标单击/等),并且呈现良好。但是,当我们尝试以编程方式提取此文档并将其保存到驱动器以便 VBA 可以将文件路径传递给 COM 对象时,所提取的 XML 文件总是出现损坏。
我发现的唯一方法是:
metaDoc.Copy
CreateObject("Shell.Application").Namespace(ActivePresentation.Path).self.InvokeVerb "Paste"
我已经读到 OLEFormat.ProgID = "Package" 存在一些困难,这可能不允许所需的行为。
我有一些解决方法,例如创建 PPTM 文件的 ZIP 副本并从该文件夹中提取嵌入的文档 XML 文件,这应该可以,但如果有更简单的方法来激活此形状/对象并通过 VBA 与之交互,这将非常有帮助。
下面是一些创建 XML 并将其插入的示例代码。问题是我如何提取它,或者我必须做上面提到的ZIP方法吗?
Public Const XMLFileName As String = "XML Embedded File.xml"
Sub ExampleCreateEmbedXML()
Dim fso As Object
Dim oFile As Object
Dim metaDoc As Shape
Dim shp As Shape
Dim sld As Slide
Dim user As String
Dim xmlExists As Boolean
xmlExists = False
user = Environ("Username")
XMLFilePath = "C:\Users\" & user & "\" & XMLFileName
Set sld = ActivePresentation.Slides(1)
For Each shp In sld.Shapes
If shp.Name = XMLFileName Then
xmlExists = True
End If
Next
If Not xmlExists Then
'If the XML OLEObject doesn't exist, then create one:
'Create a new file in the active workbook's path
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFile = fso.CreateTextFile(XMLFilePath)
oFile.Close
'And then embed the new xml document into the appropriate slide
Set metaDoc = sld.Shapes.AddOLEObject(FileName:=XMLFilePath _
, Link:=False, DisplayAsIcon:=False)
metaDoc.Name = XMLFileName
'Save this out to a drive so it can be accessed by a COM Object:
metaDoc.Copy
CreateObject("Shell.Application").Namespace(ActivePresentation.Path).self.InvokeVerb "Paste"
'This file will be an empty XML file which will not parse, but even files that have been
' created correctly by the COM object (verified against the embed file vs. the extracted file)
' do not open properly. It seems that this method of pasting the object yields errors in
' xml structure.
' I have compared by activating the Metadoc object which renders fine in any XML viewer
' but the saved down version does not open and is obviously broken when viewed in txt editor
Else:
'The file exists, so at this point the COM object would read it
' and do stuff to the document or allow user to manipulate graphics through
' userform interfaces which connect to a database
' the COM object then saves the XML file
' another subroutine will then re-insert the XML File.
' this part is not a problem, it's getting VBA to open and read the OLEObject which is
' proving to be difficult.
End If
End Sub