for each to Word with COM

I have implemented a Word AddIn in Delphi which uses the COM interface of Word 97.
What i miss is the "for each" loop of VBA to access all the elements of a list even when the index is string not integer. Some lists of Word cannot be enumerated with integers.
I suspect the undocumented property _NewEnum (from the imported OLB) to provide the access.
Has anyone explored this property and its usage?
I think this is a "feature" to force the programmers to use VBA even with COM interfaces.
LVL 11
robert_marquardtAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RBertoraCommented:
say list name is "Dem"

use

for lp := 0 to Dem.count-1
begin
  Myvariable := Dem(lp);
end;

that generally works,
can you give me a specific example you are thinking of

Rob;-)
0
robert_marquardtAuthor Commented:
Bookmarks are indexed with their names.
So its BookmarkList.Items('Name of Bookmark')
Some of these lists cannot be indexed by numbers or they have noncontigous indices and throw an exception on nonexistant elements. Even with a message box on Word side.
0
vladikaCommented:
You can use _NewEnum property to query for IEnumVariant interface and use this interface to enumerate collection items. See demo

uses ...... ComObj, ActiveX;

procedure TForm1.Button1Click(Sender: TObject);
var
  WordApp: Variant;
  Document: Variant;
  Enum: IEnumVariant;
  Bookmark: Variant;
begin
  try
    WordApp := GetActiveOleObject('Word.Application.8');
  except
    WordApp := CreateOleObject('Word.Application.8');
  end;

  WordApp.Visible := True;
  Document := WordApp.Documents.Open('C:\My Documents\Bookmark.doc');

  Memo1.Clear;
  Enum := IUnknown(Document.Bookmarks._NewEnum) as IEnumVariant; // query for IEnumVariant interface
  while Enum.Next(1, Bookmark, nil) = S_OK do
    Memo1.Lines.Add(Bookmark.Name); // use this interface do enum items
end;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.