Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I convert a stdole.StdPicture to a different Type?
    primarykey
    data
    text
    <p>To receive the bounty, please provide an answer with working code. Thanks.</p> <p>I have a stdole.StdPicture Object of the Type vbPicTypeIcon. I need to convert it to Type vbPicTypeBitmap. Due to project contraints, I need to be able to do this using Win32 or VBA. I am trying to load a file's icon to a command bar button. Here is what I have so far. It produces a lovely black square:) I am really new to graphics land so pardon me if it's a basic question.</p> <pre><code>Option Explicit Private Const vbPicTypeBitmap As Long = 1 Private Const vbPicTypeIcon As Long = 3 Private Const SHGFI_ICON As Long = &amp;H100&amp; Private Const SHGFI_SMALLICON As Long = &amp;H1&amp; Private Type PICTDESC cbSize As Long pictType As Long hIcon As Long hPal As Long End Type Private Type typSHFILEINFO hIcon As Long iIcon As Long dwAttributes As Long szDisplayName As String * 260 szTypeName As String * 80 End Type Private Declare Function DestroyIcon Lib "user32" (ByVal hIcon As Long) As Long Private Declare Function SHGetFileInfoA Lib "shell32.dll" (ByVal pszPath As String, ByVal dwFileAttributes As Long, psfi As typSHFILEINFO, ByVal cbSizeFileInfo As Long, ByVal uFlags As Long) As Long Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (lpPictDesc As PICTDESC, riid As Any, ByVal fOwn As Long, ipic As stdole.IPictureDisp) As Long Public Sub Test() Dim btn As Office.CommandBarButton Dim lngRslt As Long Dim lngAppInstc As Long Dim strFilePath As String Dim tFI As typSHFILEINFO Dim pic As stdole.IPictureDisp Set btn = TestEnv.GetTestButton lngAppInstc = Excel.Application.Hinstance strFilePath = TestEnv.GetTestFile If LenB(strFilePath) = 0&amp; Then Err.Raise 70&amp; End If SHGetFileInfoA strFilePath, 0&amp;, tFI, LenB(tFI), SHGFI_ICON Or SHGFI_SMALLICON Set pic = IconToPicture(tFI.hIcon) btn.Picture = pic Exit_Proc: On Error Resume Next If tFI.hIcon Then lngRslt = DestroyIcon(tFI.hIcon) End If Exit Sub Err_Hnd: MsgBox Err.Description, vbCritical Or vbMsgBoxHelpButton, Err.Number, Err.HelpFile, Err.HelpContext Resume Exit_Proc Resume End Sub Private Function IconToPicture(ByVal hIcon As Long) As stdole.IPictureDisp 'Modified from code by Francesco Balena on DevX Dim pic As PICTDESC Dim guid(0 To 3) As Long Dim pRtnVal As stdole.IPictureDisp pic.cbSize = LenB(pic) 'pic.pictType = vbPicTypeBitmap pic.pictType = vbPicTypeIcon pic.hIcon = hIcon ' this is the IPicture GUID {7BF80980-BF32-101A-8BBB-00AA00300CAB} ' we use an array of Long to initialize it faster guid(0) = &amp;H7BF80980 guid(1) = &amp;H101ABF32 guid(2) = &amp;HAA00BB8B guid(3) = &amp;HAB0C3000 ' create the picture, ' return an object reference right into the function result OleCreatePictureIndirect pic, guid(0), True, pRtnVal Set IconToPicture = pRtnVal End Function </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload