Vba 在Office中将存储在CustomXMLPart中的base64数据用作映像

Vba 在Office中将存储在CustomXMLPart中的base64数据用作映像,vba,excel,ms-office,base64,ms-word,Vba,Excel,Ms Office,Base64,Ms Word,关于在功能区的按钮上使用Excel文件中存储的图像的后续步骤: 是否可以使用以base64编码字符串存储在CustomXMLPart/CustomXMLNode中的图像作为Office文档中的图像,而无需先将其保存到磁盘并重新加载 我要使用图像的位置将IPictureDisp对象作为参数(如LoadPicture函数返回,但这将仅从磁盘加载文件)。首先,您需要将基本数据转换为字节数组: Private Function decodeBase64(ByVal strData As String)

关于在功能区的按钮上使用Excel文件中存储的图像的后续步骤:

是否可以使用以base64编码字符串存储在CustomXMLPart/CustomXMLNode中的图像作为Office文档中的图像,而无需先将其保存到磁盘并重新加载


我要使用图像的位置将
IPictureDisp
对象作为参数(如
LoadPicture
函数返回,但这将仅从磁盘加载文件)。

首先,您需要将基本数据转换为字节数组:

Private Function decodeBase64(ByVal strData As String) As Byte()
    Dim objXML As MSXML2.DOMDocument
    Dim objNode As MSXML2.IXMLDOMElement

    Set objXML = New MSXML2.DOMDocument
    Set objNode = objXML.createElement("b64")
    objNode.DataType = "bin.base64"
    objNode.Text = strData
    decodeBase64 = objNode.nodeTypedValue

    Set objNode = Nothing
    Set objXML = Nothing
End Function
发件人:

然后,您可以将其加载到内存中,并使用有关此主题的信息创建IPitureDisp:

Type GUID
  Data1 As Long
  Data2 As Integer
  Data3 As Integer
  Data4(7) As Byte
End Type

Private Declare Function CreateStreamOnHGlobal Lib "ole32.dll" (ByRef hGlobal As Any,     ByVal fDeleteOnResume As Long, ByRef ppstr As Any) As Long
Private Declare Function OleLoadPicture Lib "olepro32.dll" (ByVal lpStream As IUnknown, ByVal lSize As Long, ByVal fRunMode As Long, ByRef riid As GUID, ByRef lplpObj As Any) As Long
Private Declare Function CLSIDFromString Lib "ole32.dll" (ByVal lpsz As Long, ByRef pclsid As GUID) As Long

Private Const SIPICTURE As String = "{7BF80980-BF32-101A-8BBB-00AA00300CAB}"

Public Function PictureFromArray(ByRef b() As Byte) As IPicture
  On Error GoTo errorhandler

  Dim istrm As IUnknown
  Dim tGuid As GUID

  If Not CreateStreamOnHGlobal(b(LBound(b)), False, istrm) Then
    CLSIDFromString StrPtr(SIPICTURE), tGuid
    OleLoadPicture istrm, UBound(b) - LBound(b) + 1, False, tGuid, PictureFromArray
  End If

  Set istrm = Nothing
  Exit Function
errorhandler:
  Debug.Print "Could not convert to IPicture!"
End Function