PDA

View Full Version : What is different between macros?



akokin
08-27-2007, 12:26 PM
Hello.
Whether you can explain me sense of a following code by the line? Thismacro gave from site Allan Wyatt which allows to will cycle forwards through the windows:


Sub ChangeWin()
On Error GoTo ChangeWinErr
Set bb = ActiveWindow.Next
If Windows.Count > 1 Then
bb.Activate
Exit Sub
End If
ChangeWinErr:
Windows(1).Activate
End Sub

Why it is impossible to use one line of a code simply:


ActiveDocument. ActiveWindow. Next. Activate?

Thanks.

Norie
08-27-2007, 12:39 PM
Well what do you think happens when you reach the last document?

The code will try to goto a non-existent document, causing an error.

fumei
08-27-2007, 01:22 PM
And it would be a good idea to use Option Explicit, as bb is never declared.

akokin
08-27-2007, 07:49 PM
Thank you.

Well, then what is bb - Variant or a Object? I think it variable as Object (from help: Returns the next object in the collection.).

fumei
08-27-2007, 09:51 PM
What is bb? That is precisely my point re: declaring it. Option Explicit forces you to declare variables/objects.

Dim bb As Window
Dim bb As Variant
Dim bb As Object

are all valid. Personally, if it is a window...I would declare it as a window.

And what the heck does "bb" mean???? I think names should give a clue about the thing named.