?
Solved

for each to Word with COM

Posted on 1999-11-24
3
Medium Priority
?
362 Views
Last Modified: 2010-04-04
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.
0
Comment
Question by:robert_marquardt
3 Comments
 
LVL 7

Expert Comment

by:RBertora
ID: 2231022
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
 
LVL 11

Author Comment

by:robert_marquardt
ID: 2232109
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
 
LVL 3

Accepted Solution

by:
vladika earned 300 total points
ID: 2233014
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…
Suggested Courses

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question