Hi Panda,
I'm not sure if I'm picking exactly the correct constants, but this seems to work. The first one uses shapes and links the pic. Just as I was playing around a bit, the second way uses an image control, but the pic ends up saving (I'm presuming based on filesize) as the control is on the sheet. (different from on a userform I believe)
In the Worksheet's Module:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngPicNames As Range
Const Path As String = "G:\2011\2011-01-24\"
If Not Application.Intersect(Target, Range("C2")) Is Nothing _
And Target.Count = 1 Then
'--- shapes / linked ---
Set rngPicNames = Sheet2.Range(Mid(Range("C2").Validation.Formula1, 2))
If Not IsError(Application.Match(Range("C2"), rngPicNames, 0)) Then
If Not ChangePic(Path & rngPicNames.Offset(, 1).Cells( _
Application.Match(Range("C2"), rngPicNames, 0)).Value, "MyPic") Then
MsgBox "Unable to locate pic"
End If
End If
'--- image control ---
If Not IsError(Application.Match(Range("C2"), rngPicNames, 0)) Then
With Me.imgPart
.Picture = LoadPicture(Path & _
rngPicNames.Offset(, 1).Cells( _
Application.Match(Range("C2"), rngPicNames, 0)).Value)
End With
End If
End If
End Sub
In a Standard Module:
Option Explicit
Function ChangePic(Path As String, shpName As String) As Boolean
Dim _
shpPic As Object, _
dblPicHgt As Double, _
dblPicLeft As Double, _
dblPicTop As Double, _
dblPicWid As Double
On Error GoTo errPrint
Set shpPic = Sheet1.Shapes(shpName)
With shpPic
dblPicHgt = .Height
dblPicLeft = .Left
dblPicTop = .Top
dblPicWid = .Width
.Delete
End With
Set shpPic = Sheet1.Shapes.AddPicture(Path, msoTrue, msoFalse, dblPicLeft, _
dblPicTop, dblPicWid, dblPicHgt)
shpPic.Name = "MyPic"
ChangePic = True
Exit Function
errPrint:
Select Case Err.Number
Case -2147024809
MsgBox "The picture/shape with the specified name of ""MyPic"" wasn't found.", _
vbCritical, vbNullString
Case 1004
MsgBox "Missing picture file or bad path", vbCritical, vbNullString
End Select
End Function
Hope that helps a little at least,
Mark