Send picture to the back

Darkai

New Member
Joined
Mar 8, 2021
Messages
4
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Hi All
I have a problem. I'm trying to move the picture to the back as is pasted into the sheet because I have some text that needs to be in the front of the photo. Is this something you could help me with?
Thank you

Sub Insert_Photo()
Dim strFile As String
Dim rng As Range
Dim sh
Const cFile As String = "Image Files(*.bmp; *.jpg; *.jepg; *.png; *.tif),"

strFile = Application.GetOpenFilename(filefilter:=cFile, Title:=Ts)

If strFile = "False" Then
Else

Set rng = ActiveSheet.Range("d40").MergeArea
With rng
Set sh = ActiveSheet.Shapes.AddPicture(Filename:=strFile, LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, Left:=.Left, Top:=.Top, Width:=.Width, Height:=.Height)
sh.LockAspectRatio = msoFalse


End With
Set sh = Nothing
Set rng = Nothing
End If
End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Welcome to the board!

Is the text written in a cell or is it in a text box? The pics and all the other objects in a worksheet "float" on top of the worksheet. If you want to bring the text on top of the picture make sure it's in a text box.

Once you have your your text in a text box you can arrange the floating objects in the preferred order using their ZOrderPosition property.
 
Upvote 0
Hi Misca
Text is written in a text box. How I can add this to my code ?
Thank you
 
Upvote 0
Try something like:
VBA Code:
Sub Insert_Photo()
Dim strFile As String
Dim rng As Range
Dim sh As Shape
Dim Ts As String
Const cFile As String = "Image Files(*.bmp; *.jpg; *.jepg; *.png; *.tif),"

strFile = Application.GetOpenFilename(filefilter:=cFile, Title:=Ts)

    If strFile = "False" Then
    Else
    
        Set rng = ActiveSheet.Range("d40").MergeArea
        
        With rng
            Set sh = ActiveSheet.Shapes.AddPicture(Filename:=strFile, LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, Left:=.Left, Top:=.Top, Width:=.Width, Height:=.Height)
        
            With sh
                .LockAspectRatio = msoFalse
                .ZOrder msoSendToBack   'Sends the image to the back
            End With
        
        End With
        
        Set sh = Nothing
        Set rng = Nothing
    End If

End Sub
The code sends the new image all the way to the back. See other alternatives from the MSOffice Documentations.
 
Upvote 0
Hi All
I tried to protect the worksheet so some of the cells can't be edited. Unfortunately, when I protected the worksheet my VBA button stopped working (Run-Time error 1004, The specified value is out of range) Can I add or change something in the code so that will work? Thank you in advanced for the time.
Kind Regards
 
Upvote 0
You normally protect a worksheet when you want to avoid wrong kind of changes made to the worksheet. This applies to the macros as well as the users.

To solve this problem you have to unprotect the worksheet before you can make any changes and protect it again when the changes have been done. You can do that the a code as well by adding the unprotect line at the beginning of your code and protect line at the very end. You can find the lines you need from here with the full explanation.
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,551
Members
449,088
Latest member
davidcom

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