ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Excel 2007 Beta 2 - Macro Run-time error '424' Object required (https://www.excelbanter.com/excel-programming/364544-excel-2007-beta-2-macro-run-time-error-424-object-required.html)

jcm21

Excel 2007 Beta 2 - Macro Run-time error '424' Object required
 
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


All times are GMT +1. The time now is 09:30 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com