+ Reply to Thread
Results 1 to 2 of 2

Paste Picture to userform image control using Excel VBA 64bit win10

  1. #1
    Registered User
    Join Date
    10-28-2020
    Location
    seoul
    MS-Off Ver
    2016
    Posts
    1

    Paste Picture to userform image control using Excel VBA 64bit win10

    I'm using 64bit office in 64bit window 10 PC
    I did googling and found below sample code to load picture from current clipboard to userform
    but not working !!! Firstly I doubt about the dll referencing because I can't manually add reference using tools > reference command in VBE..
    So I used regsvr32 C:\windows\system32 oleaut32.dll in administrator mode and now succeeded but still can't get clipboard image to userform..
    I changed a little code and simplified it and below is the final one. I'll appreciated it if anybody can give me a simple clue.

    I attached the sample file with userform
    ==================================================================
    'Option Private Module
    Option Explicit
    Option Compare Text

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


    Public Type uPicDesc
    Size As Long
    Type As Long
    hPic As LongPtr
    hPal As LongPtr
    End Type

    Public Declare PtrSafe Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Long) As Long
    Public Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As LongPtr) As Long
    Public Declare PtrSafe Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As LongPtr 'Correct wFormat type is integer or long???
    Public Declare PtrSafe Function CloseClipboard Lib "user32" () As Long

    Public Declare PtrSafe Function OleCreatePictureIndirect Lib "oleaut32.dll" (PicDesc As uPicDesc, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
    ' In here, I tried manual referencing via tools > reference but can't, the error says
    ' "Can't add a reference to the specified file"
    ' Or can I use regsvr32 oleaut32.dll command in cmd window instead ??
    ' I already did regsvr32 registration and succeeeded but function still not working .

    Public Declare PtrSafe Function CopyEnhMetaFile Lib "gdi32" Alias "CopyEnhMetaFileA" (ByVal hemfSrc As LongPtr, ByVal lpszFile As String) As LongPtr
    Public Declare PtrSafe Function CopyImage Lib "user32" (ByVal Handle As LongPtr, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As LongPtr


    Const CF_BITMAP = 2
    Const CF_PALETTE = 9
    Const CF_ENHMETAFILE = 14
    Const IMAGE_BITMAP = 0
    Const LR_COPYRETURNORG = &H4

    Public Function PastePicture(Optional lXlPicType As Long = xlPicture) As IPicture
    PastePictureVBA7 (lXlPicType)
    End Function

    Public Function PastePictureVBA7(Optional lXlPicType As Long = xlPicture) As IPicture

    Dim H As Long, hPicAvail As Long, hPtr As LongPtr, hPal As LongPtr, lPicType As Long, hCopy As LongPtr ''Correct lPicType type is integer or long???

    lPicType = IIf(lXlPicType = xlBitmap, CF_BITMAP, CF_ENHMETAFILE)
    hPicAvail = IsClipboardFormatAvailable(lPicType)

    If hPicAvail <> 0 Then
    H = OpenClipboard(0&)

    If H > 0 Then
    hPtr = GetClipboardData(lPicType)

    If lPicType = CF_BITMAP Then
    hCopy = CopyImage(hPtr, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG)
    Else
    hCopy = CopyEnhMetaFile(hPtr, vbNullString)
    End If


    H = CloseClipboard
    If hPtr <> 0^ Then Set PastePictureVBA7 = CreatePictureVBA7(hCopy, 0, lPicType)
    End If
    End If
    End Function

    Public Function CreatePictureVBA7(ByVal hPic As LongPtr, ByVal hPal As LongPtr, ByVal lPicType) As IPicture

    Dim r As Long, uPicInfo As uPicDesc, IID_IDispatch As GUID, IPic As IPicture
    Const PICTYPE_BITMAP = 1
    Const PICTYPE_ENHMETAFILE = 4

    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

    With uPicInfo
    .Size = Len(uPicInfo)
    .Type = IIf(lPicType = CF_BITMAP, PICTYPE_BITMAP, PICTYPE_ENHMETAFILE)
    .hPic = hPic
    .hPal = IIf(lPicType = CF_BITMAP, hPal, 0)
    End With

    r = OleCreatePictureIndirect(uPicInfo, IID_IDispatch, 1, IPic)
    'Is there somting wrong in here ???

    If r = 0 Then
    Set CreatePictureVBA7 = IPic
    End If
    End Function
    Last edited by soar73; 10-30-2020 at 12:06 AM.

  2. #2
    Registered User
    Join Date
    02-08-2021
    Location
    portugal
    MS-Off Ver
    MSO 365
    Posts
    1

    Talking Re: Paste Picture to userform image control using Excel VBA 64bit win10

    Hello, Dear,
    It is frustrating but, I found this:
    DKeny post at May 2nd, 2006, 09:43 AM
    (Sorry, I not yet authorized to post a link, I'm new here)
    Google for this post and you may find it
    Unique required change was OleCreatePictureIndirect Lib , from ""olepro32.dll" to "oleaut32.dll"
    And it worked... after 2monthes looking for a solution.
    I hope it works to you too.
    Option Explicit

    'Requires a reference to the "OLE Automation" type library

    '----------------------------------------------------------------------------
    ' User-Defined Type for API Calls
    '----------------------------------------------------------------------------

    'Declare a Type to store a GUID for the IPicture OLE Interface
    Private Type GUID
    Data1 As Long
    Data2 As Integer
    Data3 As Integer
    Data4(0 To 7) As Byte
    End Type

    'Declare a Type to store the image information
    Private Type uPicDesc
    Size As Long
    Type As Long
    hPic As Long
    hPal As Long
    End Type


    '----------------------------------------------------------------------------
    'Windows API Function Declarations
    '----------------------------------------------------------------------------

    'Does the clipboard contain a Metafile Picture?
    Private Declare PtrSafe Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Integer) As Long

    'Open the Clipboard
    Private Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long

    'Get a handle on the Picture
    Private Declare PtrSafe Function GetClipboardData Lib "user32" (ByVal wFormat As Integer) As Long

    'Create a copy of the metafile
    Private Declare PtrSafe Function CopyEnhMetaFile Lib "gdi32" Alias "CopyEnhMetaFileA" (ByVal hemfSrc As Long, ByVal lpszFile As String) As Long

    'Close the clipboard
    Private Declare PtrSafe Function CloseClipboard Lib "user32" () As Long

    'Convert the handle into an OLE IPicture interface.
    Private Declare PtrSafe Function OleCreatePictureIndirect Lib "oleaut32.dll" (PicDesc As uPicDesc, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long


    Private Sub UserForm_Initialize()
    'For test it, do not forget to copy manually your picture.
    Set Me.Picture = PastePicture
    End Sub
    Function PastePicture() As IPicture

    Const lMETAFILE As Long = 14

    Dim lPictureAvailable As Long
    Dim lClipHandle As Long
    Dim lPicHandle As Long
    Dim lCopyHandle As Long
    Dim uInterGUID As GUID
    Dim uPictureInfo As uPicDesc
    Dim lOLEHandle As Long
    Dim iTempPicture As IPicture

    'Check if the clipboard contains a picture file
    lPictureAvailable = IsClipboardFormatAvailable(lMETAFILE)

    If lPictureAvailable <> 0 Then

    'Get a Handle on the Clipboard
    lClipHandle = OpenClipboard(0&)

    If lClipHandle > 0 Then

    'Get a Handle on the Picture
    lPicHandle = GetClipboardData(lMETAFILE)

    'Make a local copy, in case the clipboard is changed
    lCopyHandle = CopyEnhMetaFile(lPicHandle, vbNullString)

    'Release Handle from Clipboard
    lClipHandle = CloseClipboard

    'Only Continue if we have a handle on the Picture
    If lPicHandle <> 0 Then

    ' Create the Interface GUID (for the IPicture interface)
    With uInterGUID
    .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 UPictureInfo with necessary parts.
    With uPictureInfo
    .Size = Len(uPictureInfo) ' Length of structure.
    .Type = 4 ' Type of Picture = Metafile
    .hPic = lCopyHandle ' Handle to image.
    .hPal = 0 ' Handle to palette.
    End With

    'Create the IPicture Object
    lOLEHandle = OleCreatePictureIndirect(uPictureInfo, uInterGUID, True, iTempPicture)

    If lOLEHandle = 0 Then
    Set PastePicture = iTempPicture
    End If
    End If
    End If
    End If
    End Function

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Form Control Problems in Excel '16 on Win10
    By MrBill in forum Excel General
    Replies: 1
    Last Post: 06-17-2019, 11:46 PM
  2. [SOLVED] Add picture from userform image control to sheet cell as comment vba
    By KK1234 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-01-2016, 10:57 AM
  3. [SOLVED] VBA to transfer image FROM userform image control TO a worksheet cell
    By Zoediak in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-01-2014, 02:51 PM
  4. [SOLVED] show jpeg picture inside a userform image control box
    By cfinch100 in forum Excel Programming / VBA / Macros
    Replies: 31
    Last Post: 07-29-2013, 03:44 PM
  5. How to access bitmap picture stored as property of image control on userform
    By Spere in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-17-2013, 05:41 PM
  6. [SOLVED] Programatically control picture property of Image control
    By Brassman in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-24-2005, 05:06 PM
  7. Programatically control picture property of Image control
    By Brassman in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-24-2005, 10:11 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1