Helps fix VBA code when inserting an image to another computer without causing the image to be blank

Excelpromax123

Board Regular
Joined
Sep 2, 2021
Messages
172
Office Version
  1. 2010
Platform
  1. Windows
I used this code to insert employee images into the payroll. On the file I made, it was OK, but when I brought this file to another computer, the image was blank. Please help me, thank you very much

VBA Code:
Sub chenhinhanh()
    On Error Resume Next
        Dim ws As Worksheet
        Dim picPath As Variant
        Dim pic As Picture
        Set ws = ThisWorkbook.Sheets("bangluong")
        picPath = Application.GetOpenFilename("Hinh Anh (*.jpg; *.jpeg; *.png), *.jpg; *.jpeg; *.png", , "Chon Hinh Anh")
        If picPath = False Then Exit Sub
        
        Set pic = ws.Pictures.Insert(picPath)

        With pic
            .Left = ws.Range("B2").Left
            .Top = ws.Range("B2").Top
            .Placement = xlMoveAndSize
            .Name = "NhanVien"
        End With
End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Try this:

VBA Code:
Sub chenhinhanh()
  Dim ws As Worksheet
  Dim picPath As Variant
  Dim pic As Picture
  Dim w As Double, h As Double
 
  Set ws = ThisWorkbook.Sheets("bangluong")
  picPath = Application.GetOpenFilename("Hinh Anh (*.jpg; *.jpeg; *.png), *.jpg; *.jpeg; *.png", , "Chon Hinh Anh")
  If picPath = False Then Exit Sub

  '***Just to get the width and height of the image 

  Set pic = ws.Pictures.Insert(picPath)
  w = pic.Width
  h = pic.Height
  pic.Delete
  
  'If you know or want another width and height in the image you must put it in the variables w and h
  '***
 
  With ws.Shapes.AddPicture(picPath, False, True, ws.Range("B2").Left, ws.Range("B2").Top, w, h)
    .Placement = xlMoveAndSize
    .Name = "NhanVien"
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,793
Messages
6,174,623
Members
452,574
Latest member
hang_and_bang

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