I need an unprotected image in a protected worksheet

captzero

New Member
Joined
Aug 24, 2015
Messages
10
Hi,
I'm using VBA to unprotect a worksheet and fetch a product image and place it in 4 worksheets (in the same merged cell range in all sheets) and then protect the sheet again.
Not all the images users will be inserting have the same dimensions and I'd like the inserted image to be resized by the user if its too long for the cell range etc.
How am I able to make it so that the inserted image remains unprotected once the worksheet is protected?
VBA Code:
Sub Insert_Product_Image()
ActiveSheet.Unprotect Password:="000000"
  Dim fNameAndPath As Variant
  Dim img As Picture
  Dim SheetsNames(), i As Long
  Dim rng As Range
  Dim sh As Worksheet
  
  SheetsNames = Array("Project Pricing Calculator", "Customer Job Quote", _
                      "Customer Invoice", "Customer Receipt")
  fNameAndPath = Application.GetOpenFilename _
("(*.gif; *.jpg; *.bmp; *.tif; *.jpeg; *.png), *.gif; *.jpg; *.bmp; *.tif; *.jpeg; *.png", _
, "Select your Product Image to Import")
  
If fNameAndPath = "False" Then
Sheets("Project Pricing Calculator").Protect Password:="0000002"
Exit Sub
End If

  For i = LBound(SheetsNames) To UBound(SheetsNames)
    Set sh = Sheets(SheetsNames(i))
    'Delete the old image to put the new image
    On Error Resume Next: sh.Pictures("NewPicture").Delete: On Error GoTo 0
    Set rng = sh.Range("E8:G16")
    Set img = sh.Pictures.Insert(fNameAndPath)
    With img
      .Name = "NewPicture"
      'Resize image to fit in the cell range....
      .ShapeRange.LockAspectRatio = msoTrue    ' lock aspect ratio checkbox not selected
      .Left = rng.Left
      .Top = rng.Top
      .Width = rng.Width
      .Height = rng.Height
       'Centre image in merged cells
      .Top = rng.Top + (rng.Height - .Height) / 2
      .Left = rng.Left + (rng.Width - .Width) / 2
      .Placement = 1
      .PrintObject = True
    End With
  Next i
  ActiveSheet.Protect Password:="000000"
  Worksheets("Project Pricing Calculator").Activate
End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi there

Try replacing
VBA Code:
ActiveSheet.Protect Password:="000000"
with
VBA Code:
ActiveSheet.Protect Password:="000000", DrawingObjects:=False, Contents:=True, Scenarios:=True
 
Upvote 0
Solution

Forum statistics

Threads
1,223,893
Messages
6,175,248
Members
452,623
Latest member
cliftonhandyman

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