Load Image in User Form - 64bit Excel [VBA]

netspeedz

New Member
Joined
Aug 11, 2011
Messages
21
I have a user form that displays an image (downloaded using another standard VBA module). Everything works fine on a 32bit installation of Microsoft Office 2021, however, when using the workbook on a system with 64bit installation of Microsoft Office, the images are not being displayed (the download code works fine on 32bit/64bit). Using debug.print statements, I've determined that the issue lies within the code that loads the image (getting image load failures from this code). The user form pops up as designed in both 32bit/64bit - just no image displayed in the 64bit user form image control.

Would appreciate some assistance to find a fix/workaround to get the images displayed on the user form on 64bit Microsoft Office installations. Below, I've included the code for both the source module of the user form and the standard module code for the subroutine that loads the image to the user form.

User Form Module Code below:

VBA Code:
Sub UserForm_Activate()

    'ClearCache "http://silverprice.org/charts/silver_3d_b_o_USD.png"
    DownloadChart24HourFromWeb
    UpdateChart
    
End Sub

Sub UpdateChart()

    'Set Currentchart = Sheets("External Data").ChartObjects(1).Chart
    picnm = ThisWorkbook.Path & Application.PathSeparator & "Chart24Hour.png"
    'Currentchart.Export Filename:=picnm, FilterName:="PNG"
    Image1.Picture = LoadPictureGDI(picnm)
    
End Sub

Standard Module Code for Subroutine that loads image:


Code:
Option Explicit

' Declare necessary Windows API functions for GDI+ and OLE
#If VBA7 Then
    Private Declare PtrSafe Function GdiplusStartup Lib "GDIPlus" (token As LongPtr, inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As LongPtr = 0) As Long
    Private Declare PtrSafe Function GdipCreateBitmapFromFile Lib "GDIPlus" (ByVal FileName As LongPtr, bitmap As LongPtr) As Long
    Private Declare PtrSafe Function GdipCreateHBITMAPFromBitmap Lib "GDIPlus" (ByVal bitmap As LongPtr, hbmReturn As LongPtr, ByVal background As Long) As Long
    Private Declare PtrSafe Function GdipDisposeImage Lib "GDIPlus" (ByVal image As LongPtr) As Long
    Private Declare PtrSafe Function GdiplusShutdown Lib "GDIPlus" (ByVal token As LongPtr) As Long
    Private Declare PtrSafe Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PICTDESC, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
#Else
    Private Declare Function GdiplusStartup Lib "GDIPlus" (token As Long, inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As Long = 0) As Long
    Private Declare Function GdipCreateBitmapFromFile Lib "GDIPlus" (ByVal FileName As Long, bitmap As Long) As Long
    Private Declare Function GdipCreateHBITMAPFromBitmap Lib "GDIPlus" (ByVal bitmap As Long, hbmReturn As Long, ByVal background As Long) As Long
    Private Declare Function GdipDisposeImage Lib "GDIPlus" (ByVal image As Long) As Long
    Private Declare Function GdiplusShutdown Lib "GDIPlus" (ByVal token As Long) As Long
    Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PICTDESC, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
#End If

' Declare necessary custom types for GDI+ and OLE
Private Type GUID
    Data1 As Long
    Data2 As Integer
    Data3 As Integer
    Data4(0 To 7) As Byte
End Type

Private Type PICTDESC
    Size As Long
    Type As Long
    hPic As Long
    hPal As Long
End Type

Private Type GdiplusStartupInput

    GdiplusVersion As Long
    DebugEventCallback As Long
    SuppressBackgroundThread As Long
    SuppressExternalCodecs As Long
    
End Type

' Function to load an image file using GDI+ and return as IPicture
Public Function LoadPictureGDI(ByVal sFilename As String) As IPicture

    Dim uGdiInput As GdiplusStartupInput
    Dim hGdiPlus As LongPtr
    Dim lResult As Long
    Dim hGdiImage As LongPtr
    Dim hBitmap As LongPtr

    ' Initialize GDI+
    uGdiInput.GdiplusVersion = 1
    lResult = GdiplusStartup(hGdiPlus, uGdiInput)

    ' If GDI+ initialization is successful
    If lResult = 0 Then
        ' Load the image from file
        lResult = GdipCreateBitmapFromFile(StrPtr(sFilename), hGdiImage)
        ' If image creation is successful
        If lResult = 0 Then
            ' Convert the GDI+ image to HBITMAP
            lResult = GdipCreateHBITMAPFromBitmap(hGdiImage, hBitmap, 0)
            ' Create IPicture object from HBITMAP
            Set LoadPictureGDI = CreateIPicture(hBitmap)
            ' Release GDI+ image resources
            GdipDisposeImage hGdiImage
        End If
        ' Shutdown GDI+
        GdiplusShutdown hGdiPlus
    End If
    
End Function

' Function to create IPicture object from HBITMAP
Private Function CreateIPicture(ByVal hPic As LongPtr) As IPicture

    Dim lResult As Long
    Dim uPicInfo As PICTDESC
    Dim IID_IDispatch As GUID
    Dim IPic As IPicture

    Const PICTYPE_BITMAP = 1

    ' Initialize GUID for IPicture interface
    With IID_IDispatch
        .Data1 = &H7BF80980
        .Data2 = &HBF32
        .Data3 = &H101A
        .Data4(0) = &H8B
        .Data4(1) = &HBB
        .Data4(2) = &H0
        .Data4(3) = &HAA
        .Data4(4) = &H0
        .Data4(5) = &H30
        .Data4(6) = &HC
        .Data4(7) = &HAB
    End With

    ' Fill PICTDESC structure
    With uPicInfo
        .Size = Len(uPicInfo)
        .Type = PICTYPE_BITMAP
        .hPic = hPic
        .hPal = 0
    End With

    ' Create IPicture object from HBITMAP
    lResult = OleCreatePictureIndirect(uPicInfo, IID_IDispatch, True, IPic)
    ' Return the IPicture object
    Set CreateIPicture = IPic
    
End Function
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Try this :

VBA Code:
Option Explicit

' Declare necessary Windows API functions for GDI+ and OLE
#If VBA7 Then
    Private Declare PtrSafe Function GdiplusStartup Lib "GDIPlus" (token As LongPtr, inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As LongPtr = 0) As Long
    Private Declare PtrSafe Function GdipCreateBitmapFromFile Lib "GDIPlus" (ByVal FileName As LongPtr, BITMAP As LongPtr) As Long
    Private Declare PtrSafe Function GdipCreateHBITMAPFromBitmap Lib "GDIPlus" (ByVal BITMAP As LongPtr, hbmReturn As LongPtr, ByVal background As Long) As Long
    Private Declare PtrSafe Function GdipDisposeImage Lib "GDIPlus" (ByVal image As LongPtr) As Long
    Private Declare PtrSafe Function GdiplusShutdown Lib "GDIPlus" (ByVal token As LongPtr) As Long
    Private Declare PtrSafe Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PICTDESC, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
#Else
    Private Enum LongPtr
        [_]
    End Enum
    Private Declare Function GdiplusStartup Lib "GDIPlus" (token As LongPtr, inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As LongPtr = 0) As Long
    Private Declare Function GdipCreateBitmapFromFile Lib "GDIPlus" (ByVal FileName As LongPtr, BITMAP As LongPtr) As Long
    Private Declare Function GdipCreateHBITMAPFromBitmap Lib "GDIPlus" (ByVal BITMAP As LongPtr, hbmReturn As LongPtr, ByVal background As Long) As Long
    Private Declare Function GdipDisposeImage Lib "GDIPlus" (ByVal image As LongPtr) As Long
    Private Declare Function GdiplusShutdown Lib "GDIPlus" (ByVal token As LongPtr) As Long
    Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PICTDESC, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
#End If

' Declare necessary custom types for GDI+ and OLE
Private Type GUID
    Data1 As Long
    Data2 As Integer
    Data3 As Integer
    Data4(0 To 7) As Byte
End Type

Private Type PICTDESC
    Size As Long
    Type As Long
    hPic As LongPtr
    hPal As LongPtr
End Type

Private Type GdiplusStartupInput
    GdiplusVersion            As Long
    DebugEventCallback        As LongPtr
    SuppressBackgroundThread  As LongPtr
    SuppressExternalCodecs    As Long
End Type


' Function to load an image file using GDI+ and return as IPicture
Public Function LoadPictureGDI(ByVal sFilename As String) As IPicture

    Dim uGdiInput As GdiplusStartupInput
    Dim hGdiPlus As LongPtr
    Dim lResult As Long
    Dim hGdiImage As LongPtr
    Dim hBitmap As LongPtr

    ' Initialize GDI+
    uGdiInput.GdiplusVersion = 1
    lResult = GdiplusStartup(hGdiPlus, uGdiInput)

    ' If GDI+ initialization is successful
    If lResult = 0 Then
        ' Load the image from file
        lResult = GdipCreateBitmapFromFile(StrPtr(sFilename), hGdiImage)
        ' If image creation is successful
        If lResult = 0 Then
            ' Convert the GDI+ image to HBITMAP
            lResult = GdipCreateHBITMAPFromBitmap(hGdiImage, hBitmap, 0)
            ' Create IPicture object from HBITMAP
            Set LoadPictureGDI = CreateIPicture(hBitmap)
            ' Release GDI+ image resources
            GdipDisposeImage hGdiImage
        End If
        ' Shutdown GDI+
        GdiplusShutdown hGdiPlus
    End If
 
End Function

' Function to create IPicture object from HBITMAP
Private Function CreateIPicture(ByVal hPic As LongPtr) As IPicture

    Dim lResult As Long
    Dim uPicInfo As PICTDESC
    Dim IID_IDispatch As GUID
    Dim IPic As IPicture

    Const PICTYPE_BITMAP = 1

    ' Initialize GUID for IPicture interface
    With IID_IDispatch
        .Data1 = &H7BF80980
        .Data2 = &HBF32
        .Data3 = &H101A
        .Data4(0) = &H8B
        .Data4(1) = &HBB
        .Data4(2) = &H0
        .Data4(3) = &HAA
        .Data4(4) = &H0
        .Data4(5) = &H30
        .Data4(6) = &HC
        .Data4(7) = &HAB
    End With

    ' Fill PICTDESC structure
    With uPicInfo
        .Size = Len(uPicInfo)
        .Type = PICTYPE_BITMAP
        .hPic = hPic
        .hPal = 0
    End With

    ' Create IPicture object from HBITMAP
    lResult = OleCreatePictureIndirect(uPicInfo, IID_IDispatch, True, IPic)
    ' Return the IPicture object
    Set CreateIPicture = IPic
 
End Function

I recommend that you take a look at this:

EDIT:
I would replace the OleCreatePictureIndirect function exported by the old olepro32.dll with the one exported by the new oleAut32.dll library.

So, try replacing :
VBA Code:
Private Declare PtrSafe Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PICTDESC, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
with this :
VBA Code:
Private Declare PtrSafe Function OleCreatePictureIndirect Lib "oleAut32.dll" (ByRef lpPictDesc As PICTDESC, ByRef riid As GUID, ByVal fOwn As Long, ByRef lplpvObj As Object) As Long
Do the same with the vb6 declaration.
 
Last edited:
Upvote 0
Try this :

VBA Code:
Option Explicit

' Declare necessary Windows API functions for GDI+ and OLE
#If VBA7 Then
    Private Declare PtrSafe Function GdiplusStartup Lib "GDIPlus" (token As LongPtr, inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As LongPtr = 0) As Long
    Private Declare PtrSafe Function GdipCreateBitmapFromFile Lib "GDIPlus" (ByVal FileName As LongPtr, BITMAP As LongPtr) As Long
    Private Declare PtrSafe Function GdipCreateHBITMAPFromBitmap Lib "GDIPlus" (ByVal BITMAP As LongPtr, hbmReturn As LongPtr, ByVal background As Long) As Long
    Private Declare PtrSafe Function GdipDisposeImage Lib "GDIPlus" (ByVal image As LongPtr) As Long
    Private Declare PtrSafe Function GdiplusShutdown Lib "GDIPlus" (ByVal token As LongPtr) As Long
    Private Declare PtrSafe Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PICTDESC, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
#Else
    Private Enum LongPtr
        [_]
    End Enum
    Private Declare Function GdiplusStartup Lib "GDIPlus" (token As LongPtr, inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As LongPtr = 0) As Long
    Private Declare Function GdipCreateBitmapFromFile Lib "GDIPlus" (ByVal FileName As LongPtr, BITMAP As LongPtr) As Long
    Private Declare Function GdipCreateHBITMAPFromBitmap Lib "GDIPlus" (ByVal BITMAP As LongPtr, hbmReturn As LongPtr, ByVal background As Long) As Long
    Private Declare Function GdipDisposeImage Lib "GDIPlus" (ByVal image As LongPtr) As Long
    Private Declare Function GdiplusShutdown Lib "GDIPlus" (ByVal token As LongPtr) As Long
    Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PICTDESC, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
#End If

' Declare necessary custom types for GDI+ and OLE
Private Type GUID
    Data1 As Long
    Data2 As Integer
    Data3 As Integer
    Data4(0 To 7) As Byte
End Type

Private Type PICTDESC
    Size As Long
    Type As Long
    hPic As LongPtr
    hPal As LongPtr
End Type

Private Type GdiplusStartupInput
    GdiplusVersion            As Long
    DebugEventCallback        As LongPtr
    SuppressBackgroundThread  As LongPtr
    SuppressExternalCodecs    As Long
End Type


' Function to load an image file using GDI+ and return as IPicture
Public Function LoadPictureGDI(ByVal sFilename As String) As IPicture

    Dim uGdiInput As GdiplusStartupInput
    Dim hGdiPlus As LongPtr
    Dim lResult As Long
    Dim hGdiImage As LongPtr
    Dim hBitmap As LongPtr

    ' Initialize GDI+
    uGdiInput.GdiplusVersion = 1
    lResult = GdiplusStartup(hGdiPlus, uGdiInput)

    ' If GDI+ initialization is successful
    If lResult = 0 Then
        ' Load the image from file
        lResult = GdipCreateBitmapFromFile(StrPtr(sFilename), hGdiImage)
        ' If image creation is successful
        If lResult = 0 Then
            ' Convert the GDI+ image to HBITMAP
            lResult = GdipCreateHBITMAPFromBitmap(hGdiImage, hBitmap, 0)
            ' Create IPicture object from HBITMAP
            Set LoadPictureGDI = CreateIPicture(hBitmap)
            ' Release GDI+ image resources
            GdipDisposeImage hGdiImage
        End If
        ' Shutdown GDI+
        GdiplusShutdown hGdiPlus
    End If
 
End Function

' Function to create IPicture object from HBITMAP
Private Function CreateIPicture(ByVal hPic As LongPtr) As IPicture

    Dim lResult As Long
    Dim uPicInfo As PICTDESC
    Dim IID_IDispatch As GUID
    Dim IPic As IPicture

    Const PICTYPE_BITMAP = 1

    ' Initialize GUID for IPicture interface
    With IID_IDispatch
        .Data1 = &H7BF80980
        .Data2 = &HBF32
        .Data3 = &H101A
        .Data4(0) = &H8B
        .Data4(1) = &HBB
        .Data4(2) = &H0
        .Data4(3) = &HAA
        .Data4(4) = &H0
        .Data4(5) = &H30
        .Data4(6) = &HC
        .Data4(7) = &HAB
    End With

    ' Fill PICTDESC structure
    With uPicInfo
        .Size = Len(uPicInfo)
        .Type = PICTYPE_BITMAP
        .hPic = hPic
        .hPal = 0
    End With

    ' Create IPicture object from HBITMAP
    lResult = OleCreatePictureIndirect(uPicInfo, IID_IDispatch, True, IPic)
    ' Return the IPicture object
    Set CreateIPicture = IPic
 
End Function

I recommend that you take a look at this:

EDIT:
I would replace the OleCreatePictureIndirect function exported by the old olepro32.dll with the one exported by the new oleAut32.dll library.

So, try replacing :
VBA Code:
Private Declare PtrSafe Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PICTDESC, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
with this :
VBA Code:
Private Declare PtrSafe Function OleCreatePictureIndirect Lib "oleAut32.dll" (ByRef lpPictDesc As PICTDESC, ByRef riid As GUID, ByVal fOwn As Long, ByRef lplpvObj As Object) As Long
Do the same with the vb6 declaration.
Appreciate the quick response.

When running the process on both 32bit and 64bit Excel, get the following:
Run-time error 13 Type mismatch error on line of code: lResult = OleCreatePictureIndirect(uPicInfo, IID_IDispatch, True, IPic)

Not sure how or what parameter is missing.

Any additional assistance would be appreciated.
 
Upvote 0
Try this declare and see what happens:
VBA Code:
Private Declare PtrSafe Function OleCreatePictureIndirect Lib "oleAut32.dll" (ByRef lpPictDesc As PICTDESC, ByRef riid As GUID, ByVal fOwn As Long, ByRef lplpvObj As IPicture) As Long
 
Upvote 0
Also, try replacing every instance of IPicture with stdole.IPicture throughout the entire module (including the declarations section)

The reason being, IPicture is inconviniently also a hidden interface of the Excel library which takes precedence over the stdole interface if not fully qualified.
 
Upvote 0
Solution
Also, try replacing every instance of IPicture with stdole.IPicture throughout the entire module (including the declarations section)

The reason being, IPicture is inconviniently also a hidden interface of the Excel library which takes precedence over the stdole interface if not fully qualified.
Excellent - That seemed to work - with a little tweaking:) I've included the complete working code below for historical purposes as well as helping someone else down the road with similar issues.

I will be testing for rest of the week and will be back to mark your response as the answer. Thank you so much:) Your quick response and help is certainly appreciated.

Working code:


VBA Code:
Option Explicit

' Declare necessary Windows API functions for GDI+ and OLE
#If VBA7 Then
    Private Declare PtrSafe Function GdiplusStartup Lib "GDIPlus" (token As LongPtr, inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As LongPtr = 0) As Long
    Private Declare PtrSafe Function GdipCreateBitmapFromFile Lib "GDIPlus" (ByVal FileName As LongPtr, BITMAP As LongPtr) As Long
    Private Declare PtrSafe Function GdipCreateHBITMAPFromBitmap Lib "GDIPlus" (ByVal BITMAP As LongPtr, hbmReturn As LongPtr, ByVal background As Long) As Long
    Private Declare PtrSafe Function GdipDisposeImage Lib "GDIPlus" (ByVal image As LongPtr) As Long
    Private Declare PtrSafe Function GdiplusShutdown Lib "GDIPlus" (ByVal token As LongPtr) As Long
    Private Declare PtrSafe Function OleCreatePictureIndirect Lib "oleAut32.dll" (ByRef lpPictDesc As PICTDESC, ByRef riid As GUID, ByVal fOwn As Long, ByRef lplpvObj As stdole.IPicture) As Long
#Else
    Private Enum LongPtr
        [_]
    End Enum
    Private Declare Function GdiplusStartup Lib "GDIPlus" (token As LongPtr, inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As LongPtr = 0) As Long
    Private Declare Function GdipCreateBitmapFromFile Lib "GDIPlus" (ByVal FileName As LongPtr, BITMAP As LongPtr) As Long
    Private Declare Function GdipCreateHBITMAPFromBitmap Lib "GDIPlus" (ByVal BITMAP As LongPtr, hbmReturn As LongPtr, ByVal background As Long) As Long
    Private Declare Function GdipDisposeImage Lib "GDIPlus" (ByVal image As LongPtr) As Long
    Private Declare Function GdiplusShutdown Lib "GDIPlus" (ByVal token As LongPtr) As Long
    Private Declare PtrSafe Function OleCreatePictureIndirect Lib "oleAut32.dll" (ByRef lpPictDesc As PICTDESC, ByRef riid As GUID, ByVal fOwn As Long, ByRef lplpvObj As stdole.IPicture) As Long
#End If

' Declare necessary custom types for GDI+ and OLE
Private Type GUID
    Data1 As Long
    Data2 As Integer
    Data3 As Integer
    Data4(0 To 7) As Byte
End Type

Private Type PICTDESC
    Size As Long
    Type As Long
    hPic As LongPtr
    hPal As LongPtr
End Type

Private Type GdiplusStartupInput
    GdiplusVersion            As Long
    DebugEventCallback        As LongPtr
    SuppressBackgroundThread  As LongPtr
    SuppressExternalCodecs    As Long
End Type


' Function to load an image file using GDI+ and return as stdole.IPicture
Public Function LoadPictureGDI(ByVal sFilename As String) As stdole.IPicture

    Dim uGdiInput As GdiplusStartupInput
    Dim hGdiPlus As LongPtr
    Dim lResult As Long
    Dim hGdiImage As LongPtr
    Dim hBitmap As LongPtr

    ' Initialize GDI+
    uGdiInput.GdiplusVersion = 1
    lResult = GdiplusStartup(hGdiPlus, uGdiInput)

    ' If GDI+ initialization is successful
    If lResult = 0 Then
        ' Load the image from file
        lResult = GdipCreateBitmapFromFile(StrPtr(sFilename), hGdiImage)
        ' If image creation is successful
        If lResult = 0 Then
            ' Convert the GDI+ image to HBITMAP
            lResult = GdipCreateHBITMAPFromBitmap(hGdiImage, hBitmap, 0)
            ' Create stdole.IPicture object from HBITMAP
            Set LoadPictureGDI = CreateIPictureFromBitmap(hBitmap)
            ' Release GDI+ image resources
            GdipDisposeImage hGdiImage
        End If
        ' Shutdown GDI+
        GdiplusShutdown hGdiPlus
    End If
    
End Function

' Function to create stdole.IPicture object from HBITMAP
Private Function CreateIPictureFromBitmap(ByVal hPic As LongPtr) As stdole.IPicture

    Dim lResult As Long
    Dim uPicInfo As PICTDESC
    Dim IID_IPicture As GUID
    Dim IPic As stdole.IPicture

    Const PICTYPE_BITMAP = 1

    ' Initialize GUID for stdole.IPicture interface
    With IID_IPicture
        .Data1 = &H7BF80980
        .Data2 = &HBF32
        .Data3 = &H101A
        .Data4(0) = &H8B
        .Data4(1) = &HBB
        .Data4(2) = &H0
        .Data4(3) = &HAA
        .Data4(4) = &H0
        .Data4(5) = &H30
        .Data4(6) = &HC
        .Data4(7) = &HAB
    End With

    ' Fill PICTDESC structure
    With uPicInfo
        .Size = Len(uPicInfo)
        .Type = PICTYPE_BITMAP
        .hPic = hPic
        .hPal = 0
    End With

    ' Create stdole.IPicture object from HBITMAP
    lResult = OleCreatePictureIndirect(uPicInfo, IID_IPicture, True, IPic)
    
    ' Check if creation was successful
    If lResult = 0 Then
        ' Return the stdole.IPicture object
        Set CreateIPictureFromBitmap = IPic
    Else
        ' If creation failed, set to Nothing
        Set CreateIPictureFromBitmap = Nothing
    End If
    
End Function

' Function to create stdole.IPicture object from HBITMAP
Private Function CreateIPicture(ByVal hPic As LongPtr) As stdole.IPicture

    Dim lResult As Long
    Dim uPicInfo As PICTDESC
    Dim IID_IPicture As GUID
    Dim IPic As stdole.IPicture

    Const PICTYPE_BITMAP = 1

    ' Initialize GUID for stdole.IPicture interface
    With IID_IPicture
        .Data1 = &H7BF80980
        .Data2 = &HBF32
        .Data3 = &H101A
        .Data4(0) = &H8B
        .Data4(1) = &HBB
        .Data4(2) = &H0
        .Data4(3) = &HAA
        .Data4(4) = &H0
        .Data4(5) = &H30
        .Data4(6) = &HC
        .Data4(7) = &HAB
    End With

    ' Fill PICTDESC structure
    With uPicInfo
        .Size = Len(uPicInfo)
        .Type = PICTYPE_BITMAP
        .hPic = hPic
        .hPal = 0
    End With

    ' Create stdole.IPicture object from HBITMAP
    lResult = OleCreatePictureIndirect(uPicInfo, IID_IPicture, True, IPic)
    
    ' Check if creation was successful
    If lResult = 0 Then
        ' Return the stdole.IPicture object
        Set CreateIPicture = IPic
    Else
        ' If creation failed, set to Nothing
        Set CreateIPicture = Nothing
    End If
    
End Function
 
Upvote 0
Also, try replacing every instance of IPicture with stdole.IPicture throughout the entire module (including the declarations section)

The reason being, IPicture is inconviniently also a hidden interface of the Excel library which takes precedence over the stdole interface if not fully qualified.
Seems to be working. Sure do appreciate the help on this issue.

------------
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,964
Members
449,094
Latest member
Anshu121

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top