xin giúp đỡ code chèn ảnh bằng userform.

Liên hệ QC
Tôi tuân thủ nội quy khi đăng bài

nguyen trung cuong

Thành viên mới
Tham gia
16/8/23
Bài viết
7
Được thích
3
Chào các anh chị trên diễn đàn. Em là một thành viên mới đang tập tành viết VBA, hiện em đang gặp 2 vấn đề dưới đây nhờ anh chị giúp đỡ
1. Em muốn chèn ảnh qua userform tuy nhiên khi kích vào "add picture" thì ảnh không hiện lên userform nhưng vẫn lưu được vào trang tính đúng yêu cầu. (em muốn hỏi đây có phải là lỗi do em dùng bản excel đời cũ không hay do e viết code sai? em hiện đang dùng excel 2013)
2.Sau khi kích vào "SAVE" thì ảnh sẽ lưu tại thư mục picture để khi tìm kiếm ảnh sẽ hiện lại theo tên hàng nhưng hiện code e viết không hề hoạt động mong anh chị giúp đỡ
Chi tiết e có gửi trong file đính kèm, anh toàn bộ vấn đề em miêu tả nằm trong "giao diện chính" anh chị kích vào là sẽ ra userform nhập liệu.
Vì chỉ mới học VBA nên không thể làm chi tiết hơn câu hỏi để mọi người hiểu rõ vấn đề. Rất xin lỗi vì sự bất tiện này.
Cảm ơn anh chị vì đã dành thời gian đọc câu hỏi của em.
 

File đính kèm

  • qltblammoi - Copy.rar
    743.4 KB · Đọc: 9
  • qltb.xlsm
    480.3 KB · Đọc: 12
Có lẽ bộ VBA trong bộ cài Office của bạn có lỗi gì đó.
1692237536804.png
Bài đã được tự động gộp:

Bạn nghiên cứu Menu Ribbon nhé, mình thấy nó tiện hơn dùng form cho menu chính như bạn đang thiết kế.
1692237661726.png
 
Upvote 0
Chào các anh chị trên diễn đàn. Em là một thành viên mới đang tập tành viết VBA, hiện em đang gặp 2 vấn đề dưới đây nhờ anh chị giúp đỡ
1. Em muốn chèn ảnh qua userform tuy nhiên khi kích vào "add picture" thì ảnh không hiện lên userform nhưng vẫn lưu được vào trang tính đúng yêu cầu. (em muốn hỏi đây có phải là lỗi do em dùng bản excel đời cũ không hay do e viết code sai? em hiện đang dùng excel 2013)
2.Sau khi kích vào "SAVE" thì ảnh sẽ lưu tại thư mục picture để khi tìm kiếm ảnh sẽ hiện lại theo tên hàng nhưng hiện code e viết không hề hoạt động mong anh chị giúp đỡ
Chi tiết e có gửi trong file đính kèm, anh toàn bộ vấn đề em miêu tả nằm trong "giao diện chính" anh chị kích vào là sẽ ra userform nhập liệu.
Vì chỉ mới học VBA nên không thể làm chi tiết hơn câu hỏi để mọi người hiểu rõ vấn đề. Rất xin lỗi vì sự bất tiện này.
Cảm ơn anh chị vì đã dành thời gian đọc câu hỏi của em.
File định dạng PNG thì không thể Load bằng cách của bạn được đâu, phải viết code thêm nửa.
 
Upvote 0
Lần chỉnh sửa cuối:
Upvote 0
Tham khảo thử file này xem có giúp ích được gì không.
Bác hơi làm biếng đó, không chịu cập nhật code sang 64 bit gì hết. :D

Module: basGDIPlus

Mã:
Option Explicit

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

#If VBA7 Then
    'Declare a UDT to store the bitmap information
    Private Type PICTDESC
        Size As Long
        Type As Long
        hPic As LongPtr
        hPal As LongPtr
    End Type
    
    'Declare a UDT to store the GDI+ Startup information
    Private Type GDIPlusStartupInput
        GdiPlusVersion As Long
        DebugEventCallback As LongPtr
        SuppressBackgroundThread As Long
        SuppressExternalCodecs As Long
    End Type
    
    'Windows API calls into the GDI+ library
    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 LongPtr) As Long
    Private Declare PtrSafe Function GdipDisposeImage Lib "GDIPlus" (ByVal image As LongPtr) As Long
    Private Declare PtrSafe Sub GdiplusShutdown Lib "GDIPlus" (ByVal token As LongPtr)
    Private Declare PtrSafe Function OleCreatePictureIndirect Lib "oleaut32" (PicDesc As PICTDESC, RefIID As GUID, ByVal fPictureOwnsHandle As Long, iPic As IPicture) As Long
#Else
    'Declare a UDT to store the bitmap information
    Private Type PICTDESC
        Size As Long
        Type As Long
        hPic As Long
        hPal As Long
    End Type
    
    'Declare a UDT to store the GDI+ Startup information
    Private Type GDIPlusStartupInput
        GdiPlusVersion As Long
        DebugEventCallback As Long
        SuppressBackgroundThread As Long
        SuppressExternalCodecs As Long
    End Type
    
    'Windows API calls into the GDI+ library
    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 Sub GdiplusShutdown Lib "GDIPlus" (ByVal token As Long)
    Private Declare Function OleCreatePictureIndirect Lib "oleaut32" (PicDesc As PICTDESC, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
#End If


Public Function LoadImage(ByVal sFilename As String) As IPicture
    
    Dim uGdiInput As GDIPlusStartupInput
    Dim lResult As Long
    #If VBA7 Then
        Dim hGdiPlus As LongPtr
        Dim hGdiImage As LongPtr
        Dim hBitmap As LongPtr
    #Else
        Dim hGdiPlus As Long
        Dim hGdiImage As Long
        Dim hBitmap As Long
    #End If
    
    'Initialize GDI+
    uGdiInput.GdiPlusVersion = 1
    lResult = GdiplusStartup(hGdiPlus, uGdiInput)
    
    If lResult = 0 Then
        
        'Load the image
        lResult = GdipCreateBitmapFromFile(StrPtr(sFilename), hGdiImage)
        
        If lResult = 0 Then
            
            'Create a bitmap handle from the GDI image
            lResult = GdipCreateHBITMAPFromBitmap(hGdiImage, hBitmap, 0)
            
            'Create the IPicture object from the bitmap handle
            Set LoadImage = ConvertToIPicture(hBitmap)
            
            'Tidy up
            GdipDisposeImage hGdiImage
        End If
        
        'Shutdown GDI+
        GdiplusShutdown hGdiPlus
    End If
    
End Function


#If VBA7 Then
    Private Function ConvertToIPicture(ByVal hPic As LongPtr) As IPicture
#Else
    Private Function ConvertToIPicture(ByVal hPic As Long) As IPicture
#End If
    Dim lResult As Long
    Dim uPicinfo As PICTDESC
    Dim IID_IDispatch As GUID
    Dim iPic As IPicture
    
    'OLE Picture types
    Const PICTYPE_BITMAP = 1
    
    ' Create the Interface GUID (for the 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 uPicInfo with necessary parts.
    With uPicinfo
        .Size = Len(uPicinfo)
        .Type = PICTYPE_BITMAP
        .hPic = hPic
        .hPal = 0
    End With
    
    ' Create the Picture object.
    lResult = OleCreatePictureIndirect(uPicinfo, IID_IDispatch, True, iPic)
    
    ' Return the new Picture object.
    Set ConvertToIPicture = iPic
    
End Function
 
Upvote 0
Bác hơi làm biếng đó, không chịu cập nhật code sang 64 bit gì hết. :D

Module: basGDIPlus

Mã:
Option Explicit

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

#If VBA7 Then
    'Declare a UDT to store the bitmap information
    Private Type PICTDESC
        Size As Long
        Type As Long
        hPic As LongPtr
        hPal As LongPtr
    End Type
   
    'Declare a UDT to store the GDI+ Startup information
    Private Type GDIPlusStartupInput
        GdiPlusVersion As Long
        DebugEventCallback As LongPtr
        SuppressBackgroundThread As Long
        SuppressExternalCodecs As Long
    End Type
   
    'Windows API calls into the GDI+ library
    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 LongPtr) As Long
    Private Declare PtrSafe Function GdipDisposeImage Lib "GDIPlus" (ByVal image As LongPtr) As Long
    Private Declare PtrSafe Sub GdiplusShutdown Lib "GDIPlus" (ByVal token As LongPtr)
    Private Declare PtrSafe Function OleCreatePictureIndirect Lib "oleaut32" (PicDesc As PICTDESC, RefIID As GUID, ByVal fPictureOwnsHandle As Long, iPic As IPicture) As Long
#Else
    'Declare a UDT to store the bitmap information
    Private Type PICTDESC
        Size As Long
        Type As Long
        hPic As Long
        hPal As Long
    End Type
   
    'Declare a UDT to store the GDI+ Startup information
    Private Type GDIPlusStartupInput
        GdiPlusVersion As Long
        DebugEventCallback As Long
        SuppressBackgroundThread As Long
        SuppressExternalCodecs As Long
    End Type
   
    'Windows API calls into the GDI+ library
    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 Sub GdiplusShutdown Lib "GDIPlus" (ByVal token As Long)
    Private Declare Function OleCreatePictureIndirect Lib "oleaut32" (PicDesc As PICTDESC, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
#End If


Public Function LoadImage(ByVal sFilename As String) As IPicture
   
    Dim uGdiInput As GDIPlusStartupInput
    Dim lResult As Long
    #If VBA7 Then
        Dim hGdiPlus As LongPtr
        Dim hGdiImage As LongPtr
        Dim hBitmap As LongPtr
    #Else
        Dim hGdiPlus As Long
        Dim hGdiImage As Long
        Dim hBitmap As Long
    #End If
   
    'Initialize GDI+
    uGdiInput.GdiPlusVersion = 1
    lResult = GdiplusStartup(hGdiPlus, uGdiInput)
   
    If lResult = 0 Then
       
        'Load the image
        lResult = GdipCreateBitmapFromFile(StrPtr(sFilename), hGdiImage)
       
        If lResult = 0 Then
           
            'Create a bitmap handle from the GDI image
            lResult = GdipCreateHBITMAPFromBitmap(hGdiImage, hBitmap, 0)
           
            'Create the IPicture object from the bitmap handle
            Set LoadImage = ConvertToIPicture(hBitmap)
           
            'Tidy up
            GdipDisposeImage hGdiImage
        End If
       
        'Shutdown GDI+
        GdiplusShutdown hGdiPlus
    End If
   
End Function


#If VBA7 Then
    Private Function ConvertToIPicture(ByVal hPic As LongPtr) As IPicture
#Else
    Private Function ConvertToIPicture(ByVal hPic As Long) As IPicture
#End If
    Dim lResult As Long
    Dim uPicinfo As PICTDESC
    Dim IID_IDispatch As GUID
    Dim iPic As IPicture
   
    'OLE Picture types
    Const PICTYPE_BITMAP = 1
   
    ' Create the Interface GUID (for the 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 uPicInfo with necessary parts.
    With uPicinfo
        .Size = Len(uPicinfo)
        .Type = PICTYPE_BITMAP
        .hPic = hPic
        .hPal = 0
    End With
   
    ' Create the Picture object.
    lResult = OleCreatePictureIndirect(uPicinfo, IID_IDispatch, True, iPic)
   
    ' Return the new Picture object.
    Set ConvertToIPicture = iPic
   
End Function
cái này là e không biết luôn í
Bài đã được tự động gộp:

Tham khảo thử file này xem có giúp ích được gì không.
e kích vô mở nó báo lỗi để e thử thêm code của @ongke0711 xem
 

File đính kèm

  • anh.jpg
    anh.jpg
    60.4 KB · Đọc: 9
Lần chỉnh sửa cuối:
Upvote 0
Bác hơi làm biếng đó, không chịu cập nhật code sang 64 bit gì hết. :D

Module: basGDIPlus

Mã:
Option Explicit

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

#If VBA7 Then
    'Declare a UDT to store the bitmap information
    Private Type PICTDESC
        Size As Long
        Type As Long
        hPic As LongPtr
        hPal As LongPtr
    End Type
  
    'Declare a UDT to store the GDI+ Startup information
    Private Type GDIPlusStartupInput
        GdiPlusVersion As Long
        DebugEventCallback As LongPtr
        SuppressBackgroundThread As Long
        SuppressExternalCodecs As Long
    End Type
  
    'Windows API calls into the GDI+ library
    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 LongPtr) As Long
    Private Declare PtrSafe Function GdipDisposeImage Lib "GDIPlus" (ByVal image As LongPtr) As Long
    Private Declare PtrSafe Sub GdiplusShutdown Lib "GDIPlus" (ByVal token As LongPtr)
    Private Declare PtrSafe Function OleCreatePictureIndirect Lib "oleaut32" (PicDesc As PICTDESC, RefIID As GUID, ByVal fPictureOwnsHandle As Long, iPic As IPicture) As Long
#Else
    'Declare a UDT to store the bitmap information
    Private Type PICTDESC
        Size As Long
        Type As Long
        hPic As Long
        hPal As Long
    End Type
  
    'Declare a UDT to store the GDI+ Startup information
    Private Type GDIPlusStartupInput
        GdiPlusVersion As Long
        DebugEventCallback As Long
        SuppressBackgroundThread As Long
        SuppressExternalCodecs As Long
    End Type
  
    'Windows API calls into the GDI+ library
    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 Sub GdiplusShutdown Lib "GDIPlus" (ByVal token As Long)
    Private Declare Function OleCreatePictureIndirect Lib "oleaut32" (PicDesc As PICTDESC, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
#End If


Public Function LoadImage(ByVal sFilename As String) As IPicture
  
    Dim uGdiInput As GDIPlusStartupInput
    Dim lResult As Long
    #If VBA7 Then
        Dim hGdiPlus As LongPtr
        Dim hGdiImage As LongPtr
        Dim hBitmap As LongPtr
    #Else
        Dim hGdiPlus As Long
        Dim hGdiImage As Long
        Dim hBitmap As Long
    #End If
  
    'Initialize GDI+
    uGdiInput.GdiPlusVersion = 1
    lResult = GdiplusStartup(hGdiPlus, uGdiInput)
  
    If lResult = 0 Then
      
        'Load the image
        lResult = GdipCreateBitmapFromFile(StrPtr(sFilename), hGdiImage)
      
        If lResult = 0 Then
          
            'Create a bitmap handle from the GDI image
            lResult = GdipCreateHBITMAPFromBitmap(hGdiImage, hBitmap, 0)
          
            'Create the IPicture object from the bitmap handle
            Set LoadImage = ConvertToIPicture(hBitmap)
          
            'Tidy up
            GdipDisposeImage hGdiImage
        End If
      
        'Shutdown GDI+
        GdiplusShutdown hGdiPlus
    End If
  
End Function


#If VBA7 Then
    Private Function ConvertToIPicture(ByVal hPic As LongPtr) As IPicture
#Else
    Private Function ConvertToIPicture(ByVal hPic As Long) As IPicture
#End If
    Dim lResult As Long
    Dim uPicinfo As PICTDESC
    Dim IID_IDispatch As GUID
    Dim iPic As IPicture
  
    'OLE Picture types
    Const PICTYPE_BITMAP = 1
  
    ' Create the Interface GUID (for the 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 uPicInfo with necessary parts.
    With uPicinfo
        .Size = Len(uPicinfo)
        .Type = PICTYPE_BITMAP
        .hPic = hPic
        .hPal = 0
    End With
  
    ' Create the Picture object.
    lResult = OleCreatePictureIndirect(uPicinfo, IID_IDispatch, True, iPic)
  
    ' Return the new Picture object.
    Set ConvertToIPicture = iPic
  
End Function

Tôi có sử dụng code trong basGDIPlus để xem ảnh trong link dưới đây trên userform với control Image

Nhưng phần transparent của ảnh bị hiển thị thị màu đen. Nhờ bác và mọi người hoàn thiện giúp.
 
Upvote 0
Tôi có sử dụng code trong basGDIPlus để xem ảnh trong link dưới đây trên userform với control Image

Nhưng phần transparent của ảnh bị hiển thị thị màu đen. Nhờ bác và mọi người hoàn thiện giúp.
Thử cái này xem sao, có thêm giải pháp là LoadImage vào Label luôn.
 

File đính kèm

  • LoadImage PNG.xlsm
    24.1 KB · Đọc: 15
Upvote 0
Tôi có sử dụng code trong basGDIPlus để xem ảnh trong link dưới đây trên userform với control Image

Nhưng phần transparent của ảnh bị hiển thị thị màu đen. Nhờ bác và mọi người hoàn thiện giúp.
Can thiệp sâu vào API thì tôi không rành rồi. File bạn @giaiphap xử lý ngon lành vụ này.
 
Upvote 0
Web KT
Back
Top Bottom