VB.net对象引用未设置错误

时间:2014-06-20 02:42:34

标签: vb.net

所以我得到的错误是Object Reference未设置为对象的实例。出于某种原因,当我运行它时image = =什么导致rng =代码来获取此错误。

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))

1 个答案:

答案 0 :(得分:0)

您应该始终检查潜在的错误 - 尤其是在处理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

当然,假设oSheet也不是什么都没有。我希望在你的路上帮助你。