Dictionary help, please

C

cunnus88

Whaddaya know? My third thread starter in one week. Thanks for all th
help, people.

This time, I'm having trouble declaring a Dictionary object.

I've seen the code in other forums and it goes something like:

dim mydictionary as new dictionary

Well, whenever I do that, I get the literal dictionary (you know, th
one with entries and definitions and pronunciation keys?).

I did include a reference to Microsoft Scripting Runtime Library so wh
I should be stuck with a lexicon is beyond me.

Any takers
 
J

Jezebel

When you declare an object variable Word has to find the definition of the
object. If you don't specify which library to look in, Word searches through
the libraries listed in the References list -- in the order in that the
libraries are listed -- and uses the first instance of the object name that
it finds. In your case, it's trying to find a definition of 'Dictionary' and
the first one it finds is in the Word object library, because the Word
library is listed before the Scripting library.

To solve the problem, specify the library you want in the declaration --

Dim MyDict as Scripting.Dictionary
Set MyDict = new Scripting.Dictionary

This is good practice in all cases when you're declaring an object variable,
even when you mean the Word library. If you want a Word dictionary, use

Dim WordDict as Word.Dictionary


Separately, declaring objects using the 'As new ...' syntax, while
legitimate, is generally considered poor programming practice. Better that
the object instantiations be explicitly under your control.
 
C

cunnus88

Thanks for the help, Jezebel.

One more question, if you don't mind?

In this case, if there are two objects from two different librarie
with duplicate names, you said that it was possible to access th
object by declaring the library first, putting a period (or membe
variable operator or whatever it's called) after that, then declarin
the object.

But what if I don't even know what the name of the library is? As i
this case, let's say I heard that there was this great object with
lot of functions, but its name was "document" and it was included in
library named "not a word native library" in the references dialog box
Is there a way to find out what the library's name (e.g. "Scripting" a
opposed to "Microsoft Scripting Library") is within VBA or would I hav
to post another message here to discover that?

To clarify with the example we have, the "Scripting" library can b
accessed by including a reference to "Microsoft Scripting Runtime", bu
to fully utilize all its functions, I would have to know that "Microsof
Scripting Runtime" is referenced through the "Scripting" object in th
code window. How would I know this
 
J

Jezebel

Simplest is to open the Object Browser (on the VBE View menu) -- it lists
them, and also lists all the objects in each case.

Or you can query the application.VBE.ActiveVBProject.References collection.
 

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