Hi
First day on this forum, be gentle.
I have a problem that is driving me mental, I can get part of this done but the last bit is just elluding me.
I will try to explain the problem as fully as I can.
I have a process set up from an Access database that does quite a bit of preparation of data then finally sends the prepared data to an Excel spreadsheet. Click of a button spews the data out to Excel, prompts the user to choose a folder where to save the file then opens the resulting spreadsheet. So far so good.
The final bit of 'data' that needs to go into this sheet is a photo. As the spreadsheet is saved in the same folder as the photos I can get the path to the photo from using 'Application.ActiveWorkbook.Path'. I also know the filename of each photo, this is part of the dataload, so I can now get the full path to the photo (if necessary). Obviously I can get the VBA to open the file picker at the correct folder. It's the final bit I just can't get, getting it to choose the photo based on the filename and insert into the selected cell in my spreadsheet. When I can do that I can then code in the remainder of the function (which will scroll down 5 rows to the next cell where a photo is required, check if that has a photo filename in it, and if so do the same process again, and so on) I can do that bit.
I have tried several different solutions, none do exactly what I want. I can return the filename to the 'File Name' field, but that doesn't select the folder it is in. I can return the path to the 'File Name' field, but that doesn't select the photo, and using the full path including the filename returns an error. It seems this should be easy, but I just can't get it to select the photo and put it into my cell.
This is what I have so far, this does everything I want except selecting and the photo and entering it into the spreadsheet automatically:
Sub InsertPictures()
'Update 20140513
Dim PicList() As Variant
Dim PicFormat As String
Dim Rng As Range
Dim sShape As Shape
Dim FP As String
Dim F As String
Dim intChoice As Integer
Dim FPF As String
'return the current path to this spreadsheet as part of the initial path lookup
FP = Application.ActiveWorkbook.Path
'Now find the filename we want from the cell in question (select the sheet and cell and return the value)
F = Worksheets("qryMandB_WithACMs_").Range("A2").Value
'put the 2 together, this will be the initial filename
FPF = FP & "/" & F
With Application.FileDialog(msoFileDialogOpen)
.InitialFileName = FP
On Error Resume Next
PicList = Application.GetOpenFilename(PicFormat, MultiSelect:=True)
xColIndex = Application.ActiveCell.Column
If IsArray(PicList) Then
xRowIndex = Application.ActiveCell.Row
For lLoop = LBound(PicList) To UBound(PicList)
Set Rng = Cells(xRowIndex, xColIndex)
Set sShape = ActiveSheet.Shapes.AddPicture(PicList(lLoop), msoFalse, msoCTrue, Rng.Left, Rng.Top, Rng.Width, Rng.Height)
xRowIndex = xRowIndex + 1
Next
End If
' End If
End With
End Sub
I need this to be fully automated if possible. So, is it possible? Or am I going about this the wrong way? Any help greatly appreciated, thanks.
Bookmarks