-1

So I am getting this error Object Reference not set to an instance of an object. for some reason when I run it image = nothing which is causing rng = code to get this error.

Dim image As Excel.Shape
        ' This sets a reference to the image/end indactor.
        image = oSheet.Shapes(oSheet.Shapes.Count)

        ' This sets up the range we are going to want to cut/copy over.
        rng = oSheet.Range("A1", oSheet.Cells(image.TopLeftCell.Row + 3, 8))
Dmcovey1993
  • 75
  • 1
  • 8

1 Answers1

0

You should always check for potential errors - especially when dealing with things like Excel!

Dim image As Excel.Shape
' This sets a reference to the image/end indactor.
image = oSheet.Shapes(oSheet.Shapes.Count)

If image IsNot Nothing Then
    ' This sets up the range we are going to want to cut/copy over.
    rng = oSheet.Range("A1", oSheet.Cells(image.TopLeftCell.Row + 3, 8))
Else
    Throw New Exception("There are no shapes on the sheet 'oSheet'.")
End If

That of course assumes oSheet is not Nothing as well. I hope that helps you on your way.

Grim
  • 672
  • 4
  • 17