I'm looking to insert multiple itemized objects in excel via a VBA

1

Is there a way you can set this code to have the embedded image go into a separate cell instead of on top of each other?

For example, i run this macro to insert 3 PDFs. I want these 3 PDFs to go in cells A2, A3, A4.

The reason I am doing this is because I am going to use this spreadsheet in a mail merge to send the individual PDFs to different email addresses.

Sub AddOlEObject()

Dim mainWorkBook As Workbook

Set mainWorkBook = ActiveWorkbook

Folderpath = "C:\Users\jyousef\Documents\SL\October15"

Set fso = CreateObject("Scripting.FileSystemObject")

NoOfFiles = fso.GetFolder(Folderpath).Files.Count

Set listfiles = fso.GetFolder(Folderpath).Files

For Each fls In listfiles

    Counter = Counter + 1

    Range("A" & Counter).Value = fls.Name

    strCompFilePath = Folderpath & "\" & Trim(fls.Name)

    If strCompFilePath <> "" Then

        ActiveSheet.OLEObjects.Add(Filename:=strCompFilePath, Link:=False, DisplayAsIcon:=True, IconIndex:=1, IconLabel:=strCompFilePath).Select

            'Sheets("Object").Activate

        'Sheets("Object").Range("B" & ((Counter - 1) * 3) + 1).Select

    End If

Next

mainWorkBook.Save

End Sub

JAY

Posted 2016-08-16T22:19:14.747

Reputation: 11

Please provide the macro you are using. – gtwebb – 2016-08-16T23:15:51.363

I pasted the code in my post. I found this code from a previous post titled "Is it possible to insert multiple objects at once in Excel 2007?" – JAY – 2016-08-17T14:51:23.443

Answers

1

Objects are not actually placed in cells so I'm not sure how that will work with a mail merge but to place objects at different point just select the different cells (objects are placed at the active cell if you don't specify top and left).

Add something like

Range("A" & Counter).select

after

Range("A" & Counter).Value = fls.Name

gtwebb

Posted 2016-08-16T22:19:14.747

Reputation: 2 852

Great, this itemized the objects but it did not send the attachments when I went to do my mail merge. Do you have any suggestions? – JAY – 2016-08-17T17:06:18.847

1I dont really know anything about mail merges from excel but i would guess you are probably better off storing a full filepath in excel instead of the actual attachment. – gtwebb – 2016-08-17T17:24:14.673

Really? What updates to the code should I make? – JAY – 2016-08-17T17:52:29.423