Aplikasi Excelku. Hallo Sobat Excel semuanya, selamat datang di Blog Aplikasi Excelku. Terimakasih Saya ucapkan kepada Anda semua yang sudah mengunjungi blog sederhana Saya ini. Pada kesempatan ini Saya akan berbagi sebuah tutorial sekaligus tips dan trik mengenai VBA Excel. Dan sesuai dengan judul artikel/postingan ini, Saya akan memberikan tutorial mengenai Cara Menyisipkan (Insert) Gambar PNG Kedalam Userform VBA Excel.
Seperti yang kita ketahui bersama, bahwa di Userform VBA Excel kita hanya bisa menyisipkan (insert) gambar yang berekstensi/format *.jpg, *.jpeg, *.BMP, dan *.ico, untuk format gambar *.PNG pastinya akan secara otomatis ditolak/tidak akan bisa. Ada sebuah cara agar nantinya kita bisa menyisipkan gambar berekstensi/format *.PNG, yaitu dengan menambahkan sebuah kode/script Macro. Dan berikut ini cara atau pun langkah-langkahnya:
Insert PNG Picture Di Userform VBA Excel
Membuat Sebuah Userform Untuk Menyisipkan (Insert) Gambar
Pada contoh ini saya membuat sebuah Userform yang akan Saya gunakan untuk menyisipkan sebuah file gambar, dimana didalam Userform ini terdapat 2 (dua) komponen control lainnya, diantaranya adalah:
- Image; dan
- Command Button.
Menyisipkan Kode/Script Macro
'Insert ke ModuleOption ExplicitPrivate Type GUID'https://aplikasiexcelku.blogspot.comData1 As LongData2 As IntegerData3 As IntegerData4(0 To 7) As ByteEnd TypePrivate Type PICTDESCSize As LongType As LonghPic As LongPtrhPal As LongPtrEnd TypePrivate Type GdiplusStartupInputGdiplusVersion As LongDebugEventCallback As LongPtrSuppressBackgroundThread As LongSuppressExternalCodecs As LongEnd TypePrivate Declare PtrSafe Function GdiplusStartup Lib "GDIPlus" (token As LongPtr, _inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As LongPtr = 0) As LongPtrPrivate Declare PtrSafe Function GdipCreateBitmapFromFile Lib "GDIPlus" (ByVal filename As LongPtr, bitmap As LongPtr) As LongPtrPrivate Declare PtrSafe Function GdipCreateHBITMAPFromBitmap Lib "GDIPlus" (ByVal bitmap As LongPtr, _hbmReturn As LongPtr, ByVal background As Long) As LongPtrPrivate Declare PtrSafe Function GdipDisposeImage Lib "GDIPlus" (ByVal image As LongPtr) As LongPtrPrivate Declare PtrSafe Function GdiplusShutdown Lib "GDIPlus" (ByVal token As LongPtr) As LongPtrPrivate Declare PtrSafe Function OleCreatePictureIndirect Lib "oleaut32.dll" (PicDesc As PICTDESC, _RefIID As GUID, ByVal fPictureOwnsHandle As LongPtr, IPic As IPicture) As LongPtrPublic Function LoadImage(ByVal strFName As String) As IPictureDim uGdiInput As GdiplusStartupInputDim hGdiPlus As LongPtrDim hGdiImage As LongPtrDim hBitmap As LongPtruGdiInput.GdiplusVersion = 1If GdiplusStartup(hGdiPlus, uGdiInput) = 0 ThenIf GdipCreateBitmapFromFile(StrPtr(strFName), hGdiImage) = 0 ThenGdipCreateHBITMAPFromBitmap hGdiImage, hBitmap, 0Set LoadImage = ConvertToIPicture(hBitmap)GdipDisposeImage hGdiImageEnd IfGdiplusShutdown hGdiPlusEnd IfEnd FunctionPublic Function ConvertToIPicture(ByVal hPic As LongPtr) As IPicture'https://aplikasiexcelku.blogspot.comDim uPicInfo As PICTDESCDim IID_IDispatch As GUIDDim IPic As IPictureConst PICTYPE_BITMAP = 1With 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) = &HABEnd WithWith uPicInfo.Size = Len(uPicInfo).Type = PICTYPE_BITMAP.hPic = hPic.hPal = 0End WithOleCreatePictureIndirect uPicInfo, IID_IDispatch, True, IPicSet ConvertToIPicture = IPicEnd Function
'Insert ke Userform (CommandButton)Private Sub CommandButton1_Click()'https://aplikasiexcelku.blogspot.comDim vntFilename As VariantvntFilename = Application.GetOpenFilename("*.PNG (*.png),*.png")If vntFilename = "False" Then Exit SubImage1.Picture = LoadImage(vntFilename)End Sub
Post a Comment for "Insert PNG Picture Di Userform VBA Excel"
Tinggalkan komentar Anda pada kolom komentar di bawah ini :