error handling程序不能在“438运行时错误”
我有一个脚本,循环通过在工作表中的形状,抓住图像属性到picHeight
和picWidth
variables:
ws.Shapes(myImg.ID).Select With Selection killer = IsError(.ShapeRange.Height) If IsError(.ShapeRange.Height) = True Then GoTo badShape End If PicHeight = .ShapeRange.Height PicWidth = .ShapeRange.Width End With
有时,形状不是图片(在图纸上也有图表和文本框),VBA一直抛出一个“438错误”( 对象不支持这个属性或方法 )我有上面的error handling程序。 select对象不具有.shaperange.height
或shaperange.width
属性时,会出现问题。
看来VBA根本就不能将代码交给error handling程序,只是因为select没有选定的成员。
我不认为你需要error handling,如果你只是使用.Height
& .Width
属性(而不是.ShapeRange.Height
或.SHapeRange.Width
我刚刚testing下面对图表,矩形,图片和表单标签:
Sub Shapes() Dim sh As Shape Dim wa As Worksheet Set ws = Sheet1 For Each sh In ws.Shapes Debug.Print sh.Name & ": " & sh.Height Debug.Print sh.Name & ": " & sh.Width Next End Sub
并得到以下结果:
Chart 1: 216 Chart 1: 360 Rectangle 2: 72 Rectangle 2: 72 Picture 3: 218 Picture 3: 379 Label21: 18 Label21: 72
所以,你的代码可以简化为:
With ws.Shapes(myImg.ID) PicHeight = .Height PicWidth = .Width End With