J
jcm21
I have an existing macro that works just fine in Excel 2003, however it
doesn't want to run when opened in 2007 Beta 2. I get an object required
error... any ideas on how I can correct this are greatly appreciated.
The error is generated on line 7 - "Set p =
ActiveSheet.Pictures.Insert(PictureFileName)
Code:
Sub InsertPictureInRange(PictureFileName As String, TargetCells As Range)
' inserts a picture and resizes it to fit the TargetCells range
Dim p As Object, t As Double, l As Double, w As Double, h As Double
If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub
If Dir(PictureFileName) = "" Then Exit Sub
' import picture
Set p = ActiveSheet.Pictures.Insert(PictureFileName)
' determine positions
With TargetCells
t = .Top
l = .Left
w = .Offset(0, .Columns.Count).Left - .Left
h = .Offset(.Rows.Count, 0).Top - .Top
End With
' Get Picture Height and Width
Dim PicHt
PicHt = p.Height
Dim PicWid
PicWid = p.Width
' position picture with Auto-aspect Correction
With p
..Top = t
..Left = l
If PicHt >= PicWid Then
..Width = PicWid * (h / PicHt)
..Height = PicHt * (h / PicHt)
Else
..Width = PicWid * (w / PicWid)
..Height = PicHt * (w / PicWid)
End If
End With
Set p = Nothing
End Sub
doesn't want to run when opened in 2007 Beta 2. I get an object required
error... any ideas on how I can correct this are greatly appreciated.
The error is generated on line 7 - "Set p =
ActiveSheet.Pictures.Insert(PictureFileName)
Code:
Sub InsertPictureInRange(PictureFileName As String, TargetCells As Range)
' inserts a picture and resizes it to fit the TargetCells range
Dim p As Object, t As Double, l As Double, w As Double, h As Double
If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub
If Dir(PictureFileName) = "" Then Exit Sub
' import picture
Set p = ActiveSheet.Pictures.Insert(PictureFileName)
' determine positions
With TargetCells
t = .Top
l = .Left
w = .Offset(0, .Columns.Count).Left - .Left
h = .Offset(.Rows.Count, 0).Top - .Top
End With
' Get Picture Height and Width
Dim PicHt
PicHt = p.Height
Dim PicWid
PicWid = p.Width
' position picture with Auto-aspect Correction
With p
..Top = t
..Left = l
If PicHt >= PicWid Then
..Width = PicWid * (h / PicHt)
..Height = PicHt * (h / PicHt)
Else
..Width = PicWid * (w / PicWid)
..Height = PicHt * (w / PicWid)
End If
End With
Set p = Nothing
End Sub