Help inserting pictures with macro

excelhelp19

New Member
Joined
Oct 11, 2017
Messages
7
Hello, I am using the following code to insert a list of pics, it inserts a link to the pics instead of pasting the pics, can someone help

Sub InsertPictures()
'Application.ScreenUpdating = False
'ThisWorkbook.Worksheets("CONTENTS").Visible = xlVisible
Dim CONT As Worksheet
Set CONT = ThisWorkbook.Worksheets("CONTENTS")
Dim RNG As Range
Dim MYCELL As Range
Dim WS As String
Dim T As Worksheet
Dim P As String
Set RNG = CONT.Range("A4:A500")
For Each MYCELL In RNG
If MYCELL <> "" Then
WS = MYCELL
P = MYCELL.Offset(0, 3)
Worksheets(WS).Select
Worksheets(WS).Range("A16").Select
ActiveSheet.Pictures.Insert(ThisWorkbook.PATH & "\PHOTO" & P).Select
With Selection
.Left = Range("A16").Left
.Top = Range("A16").Top
.ShapeRange.Width = 1030#
.ShapeRange.Rotation = 0#
End With
End If
Next
End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
excelhelp19,

Welcome to the Board.

The Pictures.Insert method doesn't save the picture with the file, so if you're trying to share or distribute the file the user will only see a link. You might try changing this line...

Code:
ActiveSheet.Pictures.Insert(ThisWorkbook.PATH & "\PHOTO" & P).Select

...with the following...

Code:
ActiveSheet.Shapes.AddPicture(ThisWorkbook.Path & "\PHOTO" & P, True, True, 100, 100, 86, 129).Select

You'll need to adjust the arguments - Left, Top, Width, Height.

Cheers,

tonyyy
 
Upvote 0
This is what the code actually looks like

Sub Picture()
'Application.ScreenUpdating = False
'ThisWorkbook.Worksheets("CONTENTS").Visible = xlVisible
Dim CONT As Worksheet
Set CONT = ThisWorkbook.Worksheets("CONTENTS")
Dim RNG As Range
Dim MYCELL As Range
Dim WS As String
Dim T As Worksheet
Dim P As String
Dim PATH As String
Set RNG = CONT.Range("A4:A500")
For Each MYCELL In RNG
If MYCELL <> "" Then
WS = MYCELL
P = MYCELL.Offset(0, 3)
Worksheets(WS).Select
Worksheets(WS).Range("A16").Select
ActiveSheet.Pictures.Insert(ThisWorkbook.PATH & "\PHOTO" & P).Select
With Selection
.Left = Range("A16").Left
.Top = Range("A16").Top
.ShapeRange.Width = 1030#
.ShapeRange.Rotation = 0#
End With
End If
Next
End Sub
 
Upvote 0
Sub Picture()
'Application.ScreenUpdating = False
'ThisWorkbook.Worksheets("CONTENTS").Visible = xlVisible
Dim CONT As Worksheet
Set CONT = ThisWorkbook.Worksheets("CONTENTS")
Dim RNG As Range
Dim MYCELL As Range
Dim WS As String
Dim T As Worksheet
Dim P As String
Dim PATH As String
Set RNG = CONT.Range("A4:A500")
For Each MYCELL In RNG
If MYCELL <> "" Then
WS = MYCELL
P = MYCELL.Offset(0, 3)
Worksheets(WS).Select
Worksheets(WS).Range("A16").Select
ActiveSheet.Pictures.Insert(ThisWorkbook.PATH & "\PHOTO" & P).Select
With Selection
.Left = Range("A16").Left
.Top = Range("A16").Top
.ShapeRange.Width = 1030#
.ShapeRange.Rotation = 0#
End With
End If
Next
End Sub
 
Upvote 0
Not surprised, since the original line also produced an error (for me). What error number / description are you getting?
 
Upvote 0
Not surprised, since the original line also produced an error (for me). What error number / description are you getting?
Got it fixed with this.
Code:
ActiveSheet.Shapes.AddPicture(ThisWorkbook.PATH & "\photo\" & P, False, True, 1, 1, 1, 1).Select
 
Upvote 0
With the original line you have to have the pictures in a folder called "photo" in the same folder the spreadsheet is in.
 
Upvote 0
The missing "" would have been my suggestion. Glad you got it working...
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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