Declaring

G

Greg Maxey

I am trying to discipline myself to properly declare variable. In the
example below it took me 15 minutes to stumble on Oject to work with
oSpError. Is Object the right choice in this case?

Sub printSpellingErrors()
Dim oSpErrors As ProofreadingErrors
Dim oSpError As Object

Set oSpErrors = ActiveDocument.Range.SpellingErrors

For Each oSpError In oSpErrors
.......
 
J

Jezebel

ProofreadingErrors is actually a collection of Range objects. So while 'as
object' will work, 'as Range' would be better.

In the spirit of good discipline, when you declare object variables it is
better to be explicit about the library that defines them --

Dim oSPErrors as Word.ProofreadingErrors
Dim oSPError as Word.Range

Doesn't make any difference with ProofreadingErrors because there's unlikely
to be any ambiguity; but it can matter with objects like Range -- Excel
(amongst others) has Range objects also, and they are not interchangeable.
If you added a reference to Excel to your application, VBA would have to
guess which kind of Range you meant. In fact it uses the order in which the
references are listed.

Similarly, when you create objects --

Set oSPError as new Word.Range
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top