Macro for inserting pictures bug

grigori28

New Member
Joined
Jun 8, 2018
Messages
2
Hi guys,

I have the following macro which works as follows: once run, it reads the product codes (e.g. 12381-1231) in column B and inserts corresponding product pictures from another folder to the excel file. Now the problem is that even when I save the document and then send it to others, they can not see the pictures.

Does anyone have a clue what I am doing wrong? Here is the code:

Sub InsertPics()

Dim Rw&, PFAD$, Datei$, Bild As Shape

PFAD = "\\levi.com\ls\Regional\LSE\BRU\DATA\Merchandising\H2'18\6. VLP\PICTURES"
With ActiveSheet
For Rw = 2 To 100
Datei = .Range("B" & Rw) & ".jpg"
If Dir(PFAD & Datei) <> vbNullString Then
.Pictures.Insert (PFAD & Datei)
Set Bild = .Shapes(.Shapes.Count)
Bild.LockAspectRatio = msoFalse
Bild.Left = .Range("A" & Rw).Left
Bild.Top = .Range("A" & Rw).Top
Bild.Width = .Range("A" & Rw).Width
Bild.Height = .Range("A" & Rw).Height
End If
Next
End With
End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Use the AddPicture method of the Shapes object instead. As per Excel's help file...

Code:
[U][B]S​yntax[/B][/U]

expression[B].AddPicture(Filename, LinkToFile, SaveWithDocument, Left, Top, Width, Height)[/B]


expression   A variable that represents a [B]Shapes[/B] object.

Maybe something like this...

Code:
Sub InsertPics()

    Dim LastRow&, Rw&, PFAD$, Datei$, Bild As Shape
    
    PFAD = "\\levi.com\ls\Regional\LSE\BRU\DATA\Merchandising\H2'18\6. VLP\PICTURES\" 'added backslash to path, but remove if it isn't needed
    
    With ActiveSheet
        LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
        For Rw = 2 To LastRow
            Datei = .Range("B" & Rw).Value & ".jpg"
            If Dir(PFAD & Datei) <> vbNullString Then
                Set Bild = .Shapes.AddPicture( _
                    Filename:=PFAD & Datei, _
                    LinkToFile:=msoFalse, _
                    SaveWithDocument:=msoTrue, _
                    Left:=.Range("A" & Rw).Left, _
                    Top:=.Range("A" & Rw).Top, _
                    Width:=.Range("A" & Rw).Width, _
                    Height:=.Range("A" & Rw).Height)
            End If
        Next
    End With
    
End Sub

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,906
Members
452,366
Latest member
TePunaBloke

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