I had to change the code because it was failing on checking whether any attachments have been selected. I also added a bit of code to stop the attachments overlaying when more than one is selected. That aside, it seems to work
Sub Attachment()
Dim filters As String
Dim filename1 As Variant
Dim f As Variant
Dim idx As Long
Sheets.Add.Name = "Attachments"
Sheets("SCR Form").Move Before:=Sheets("Attachments")
' Get the file name.
'filename1 = Application.GetOpenFilename(FileFilter:="Select Files(*.xls;*.xlsx;*.doc;*.docx;*.ppt),*.xls;*.xlsx;*.doc;*.docx;*.ppt", Title:="Select a file")
filename1 = Application.GetOpenFilename(FileFilter:="Select Files(*.xls;*.xlsx;*.doc;*.docx;*.ppt),*.xls;*.xlsx;*.doc;*.docx;*.ppt", Title:="Select a file", MultiSelect:=True)
If Not IsArray(filename1) Then
If filename1 = False Then
Application.DisplayAlerts = False
Worksheets("Attachments").Delete
Application.DisplayAlerts = True
Exit Sub
End If
Else
' Insert the file.
For Each f In filename1
idx = idx + 1
InsertPicture CStr(f), idx, Application.Selection
Next
End If
End Sub
Sub InsertPicture( _
ByVal filename1 As String, _
ByVal idx As Long, _
ByRef location As Range)
Dim objI As Object
Dim rngI As Range
Sheets("Attachments").Range("A2") = "Attachments Below"
Set myDocument = Sheets("Attachments")
Set objI = Sheets("Attachments").OLEObjects.Add(Filename:=filename1, Link:=False, DisplayAsIcon:=True, IconFileName:=filename1, IconLabel:=filename1) ', Width:=40, Height:=40)
objI.Top = idx * 30
objI.Left = 5
End Sub
Just tried closing the workbook, and it failed. Not sure what was going on, but it failed on a Left statement as it was using Instr and subtracting one, failed if not found. I also ran out of memory and had to abort excel.