Whats wrong here? Macro returns error instead of exit sub

Started by frank, December 20, 2011, 12:11:20 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

frank

Hi Guys

New to Visio and new to macros I am looking for a lot of sample code.
While most things work (without knowing why  :)) I have another question that you might be able to help me understand...?

The following code works when there is at least one shape on the page, it selects it/them and deletes the selection.
If no shapes are on the page, it should simply do nothing and exit the sub. But I only get an error. Whats wrong here?


Private Sub ResetPage()   
    ActiveWindow.SelectAll
    If ActiveWindow.Selection.count = 0 Then
        Exit Sub
    End If
    Application.ActiveWindow.Selection.DeleteEx (visDeleteNormal)
End Sub
Best Regards
Frank

aledlund

there are several functions that if they return an empty set will flag it as an error (it's normal). The programmer should get in the practice of wrapping these functions in an "on error"

http://msdn.microsoft.com/en-us/library/aa201755(v=office.10).aspx

al

AndyW

You can't do a select all if there are no shapes on the page, just add a check to see if there are any shapes.

Private Sub ResetPage()
   
    If Application.ActivePage.Shapes.Count > 0 Then
   
        call Application.ActiveWindow.SelectAll
       
        call Application.ActiveWindow.Selection.Delete
   
    End If
   
End Sub
Live life with an open mind

frank

Hi Andy
Hi aledlund

Thanks to both of you for taking the time to respond and for the clarification.
Both replies really bring me forward. Thanks!

cheers,
Frank
Best Regards
Frank