3.5.1 The Enumerator Object

LuaCOM

next up previous contents Next: 3.5.2 The Connection Point Up: 3.5 Other Objects Previous: 3.5 Other Objects   Contents

3.5.1 The Enumerator Object

This object is a proxy for a COM object that implements the IEnumVARIANT interface. It translates the calls made to fields of the table to method calls using that interface. Enumerators arise often when dealing with collections. To obtain an enumerator for a collection, use the Lua API method GetEnumerator. Example:

--
-- Sample use of enumerators
--
-- Gets an instance
word = luacom.GetObject("Word.Application")
-- Gets an enumerator for the Documents collection
docs_enum = luacom.GetEnumerator(word.Documents)
-- Prints the names of all open documents
doc = docs_enum:Next()
while doc do
  print(doc.Name)
  doc = docs_enum:Next()
end

The Extended Lua API method pairs allows the traversal of the enumeration using Lua's for statement. The sample above can be rewritten this way:

--
-- Sample use of enumerators
--
-- Gets an instance
word = luacom.GetObject("Word.Application")
-- Prints the names of all open documents
for index, doc in luacomE.pairs(word.Documents) do
  print(doc.Name)
end



Fabio Mascarenhas de Queiroz 2005-01-07