VBA to insert a custom image

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,352
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have code to insert a set image. Could someone tell me how to alter it so it can insert a custom image please?

Code:
Sub cmdJakeSig()
    Dim shp As Shape
    Set shp = ThisWorkbook.Worksheets("sheet1").Shapes("textbox4")
        Sheets("Sheet2").Shapes("ImgJ").Copy
        Sheets("sheet1").Paste Destination:=Sheets("sheet1").Cells(1, 1)
        Selection.Top = shp.Top + shp.Height + "50"
    'ActiveSheet.Protect Password:=""
End Sub
 
My code puts the image in the right spot, doesn't take long to complete but doesn't push the image to the next page if it gets split between the two.

Code:
Sub cmdCustomSig()
Dim fNameAndPath As Variant
Dim img As Picture, shp As Shape
Set shp = ThisWorkbook.Worksheets("sheet1").Shapes("textbox4")

fNameAndPath = Application.GetOpenFilename(Title:="Select Picture To Be Imported")
If fNameAndPath = False Then Exit Sub
    Set img = Worksheets("sheet1").Pictures.Insert(fNameAndPath)
    With img
       .Top = shp.Top + shp.Height + "50"
       .Left = "0"
    End With
     
End Sub
 
Upvote 0

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
sounds like your super doesn't want you getting any help !!
My hands are tied if I can't play with your workbook...:oops:
 
Upvote 0
Try this one then....AND it's faster

Slight mod AND ..it assumes you lr is based on Col "A"

Code:
Private Sub CommandButton1_Click()
'insert custom image code from michael
Dim fNameAndPath As Variant
Dim img As Picture, shp As Shape
Set shp = Worksheets("NPSS_quote_sheet").Shapes("TextBox4")
fNameAndPath = Application.GetOpenFilename(Title:="Select Signature To Be Imported")
If fNameAndPath = False Then Exit Sub
 Dim h As Long, lr As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
Set img = Worksheets("NPSS_quote_sheet").Pictures.Insert(fNameAndPath)
  h = 51
    If Sheets("NPSS_quote_sheet").Rows(h).PageBreak <> xlPageBreakNone Then
    If lr >= h - 5 Then 'this is 5 rows before the page break
    With img
       .Left = 0
       .Top = h + 50 + shp.Top + shp.Height
    End With
    Else
    With img
       .Left = 0
       .Top = shp.Top + shp.Height + 50
    End With
    End If
    End If
End Sub
 
Last edited:
Upvote 0
Much quicker thanks but it puts the image near the top of the page, not under the notes box on the first page.
 
Upvote 0
The longest data column is definitely Col "A" isn't it ??
AND
where is the page break, line 50 which is standard, or id it a custom page break ??
 
Last edited:
Upvote 0
Ok, psrt of the problem was that the sheet didn't have any PageBreaks !
Secondly Col "A" only had one line of data, whereas Col "H" had a full set of data !
Try this way

Code:
Private Sub CommandButton1_Click()
'insert custom image code from michael
Dim fNameAndPath As Variant
Dim img As Picture, shp As Shape
Set shp = Worksheets("Sheet1").Shapes("TextBox4")
fNameAndPath = Application.GetOpenFilename(Title:="Select Signature To Be Imported")
If fNameAndPath = False Then Exit Sub
 Dim n As Long, lr As Long
lr = Cells(Rows.Count, "H").End(xlUp).Row
Set img = Worksheets("Sheet1").Pictures.Insert(fNameAndPath)
  n = 37
    If Sheets("Sheet1").Rows(n).PageBreak <> xlPageBreakNone Then
    If lr >= n - 11 Then 'this is 5 rows before the page break
    With img
       .Left = 0
       .Top = n + 50 + shp.Top + shp.Height
    End With
    Else
    With img
       .Left = 0
       .Top = shp.Top + shp.Height + 50
    End With
    End If
    End If
End Sub
 
Upvote 0
BTW, you need to actually insert a Page Break at row 37, for the code to work !!!
 
Upvote 0
That doesn't do much different. It still splits the image if near the bottom of the page.
 
Upvote 0
Did you inert a new page break at cell A37, as mentioned in the previous post ?
 
Upvote 0

Forum statistics

Threads
1,215,053
Messages
6,122,882
Members
449,097
Latest member
dbomb1414

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