ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How to judge wheter an object is existing or not (https://www.excelbanter.com/excel-programming/344361-how-judge-wheter-object-existing-not.html)

lvcha.gouqizi

How to judge wheter an object is existing or not
 
I have a function which basically returns a found shape if succeed. But
what if nothing could be found and nothing is returned? How to judge
this in its calling code?

Function findShape(text as String)

if *** then
findShape = specifiedShape
end if
end function



sub doSth()
if findShape("special") then
msgbox "found"
end if
end sub

There always reports error on the line "if NOT findShape then", saying
that "Object does not support this property or method". Any
suggestions? Thanks,

lvcha


lvcha.gouqizi

How to judge wheter an object is existing or not
 
Also, when no shape can be found, the line in doSth()

Set prevOval = findOval(TargetText)

results in "Object required" Run-time error.

How to fix that?


Dave Peterson

How to judge wheter an object is existing or not
 
I'd use something like:

Function findShape(myText as String) as Boolean
dim myShape as shape
set myshape = nothing
on error resume next
set myshape = activesheet.shapes(myText)
on error goto 0

if myshape is nothing then
findshape = false
else
findshape = true
end if

end function

or shorter:

Function findShape(myText as String) as Boolean
on error resume next
FindShape = CBool(Len(activesheet.shapes(myText).Name) 0)
on error goto 0
end function

"lvcha.gouqizi" wrote:

I have a function which basically returns a found shape if succeed. But
what if nothing could be found and nothing is returned? How to judge
this in its calling code?

Function findShape(text as String)

if *** then
findShape = specifiedShape
end if
end function

sub doSth()
if findShape("special") then
msgbox "found"
end if
end sub

There always reports error on the line "if NOT findShape then", saying
that "Object does not support this property or method". Any
suggestions? Thanks,

lvcha


--

Dave Peterson

Tom Ogilvy

How to judge wheter an object is existing or not
 
Function findShape(text as String) as Shape

On error Resume Next
if *** then
set findShape = specifiedShape
end if
On Error goto 0
end function

---------------------

Dim shp as Shape
On Error Resume Next
set shp = FindShape("shapename")
On Error goto 0

if not shp is nothing then
msgbox "found " & shp.Name
else
msgbox "Not found"
End if

--
Regards,
Tom Ogilvy


"lvcha.gouqizi" wrote in message
ups.com...
Also, when no shape can be found, the line in doSth()

Set prevOval = findOval(TargetText)

results in "Object required" Run-time error.

How to fix that?




lvcha.gouqizi

How to judge wheter an object is existing or not
 
Thanks Dave. But if findShape() can find some shape, I would like to
get that Shape rather than just get the "True" value.

The problem is that when no shape can be found, the findShape() gets a
run-time error, and when some shape is found, the T/F judgement get an
error.


lvcha.gouqizi

How to judge wheter an object is existing or not
 
Thanks Tom very much!

I use shp = nothing in my original code so cannot get through each
time. My bad.

Can I ask more about Shape. What's the difference between Shape
Collection and Shape Object? If there is need to deal with shape
collection, should I use Null, Empty, or Nothing?


Tom Ogilvy

How to judge wheter an object is existing or not
 
The shape collection holds the list of all the shapes. It has a count
property, so to determine if there are no shapes

if activesheet.Shapes.count = 0 then
' there are no shapes.
else
for each shp in Activesheet.Shapes
msgbox shp.Name
Next
End if

--
Regards,
Tom Ogilvy


"lvcha.gouqizi" wrote in message
oups.com...
Thanks Tom very much!

I use shp = nothing in my original code so cannot get through each
time. My bad.

Can I ask more about Shape. What's the difference between Shape
Collection and Shape Object? If there is need to deal with shape
collection, should I use Null, Empty, or Nothing?





All times are GMT +1. The time now is 05:34 PM.

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