Link to home
Start Free TrialLog in
Avatar of SteveFarndon2000
SteveFarndon2000Flag for United Kingdom of Great Britain and Northern Ireland

asked on

How to delete all bookmarks in Word 2000 document

Hi Experts,

Can you help me?

I can create a Word document using a _ApplicationDisp type interface in the Word2000.pas unit and add text, create tables, add bookmarks, etc. just fine. Trouble comes when I want to delete the bookmarks I have just created.

The code snippet below executes just fine. It iterates over the Bookmarks collection, executes the 'Bookmark.delete' but when you look at the Word document the bookmarks are still there staring you in the face.

I checked the number of bookmarks it loops over and it matches the number in the document (26) so it seems to be working on the right document just not updating it.

Please help!
procedure TMain.BuildDocument();
var
   WordApp : _ApplicationDisp;
   WordDocPolicy : _DocumentDisp;
   DocsCollection : DocumentsDisp;
   aSelection : WordSelection;
   aBookMark : Bookmark;
   aBookMarks : BookmarksDisp;
   aRange : RangeDisp;
   vRange : OleVariant;

   vFileName,
   vVisible : OleVariant;

    procedure ClearBookmarks();
    var
      i : integer;
      OleCount : OleVariant;
    begin
    If aBookmarks.Count >= 1 Then
      For i := 1 to (aBookmarks.count) do begin
         OleCount := i;
         aBookmark := aBookmarks.Item(OleCount);
         aBookMark.delete;  // <-- executes OK but Bookmark still visible in the .doc
         end;
   end;

begin

   CoInitialize(nil);
   WordApp := CoWordApplication.Create as _ApplicationDisp;
   WordApp.Visible := true;
   DocsCollection := WordApp.Documents as DocumentsDisp;

   vFileName := GetCurrentDir()+ '\_docs\blank\POLICY2.doc';
   vVisible := true;

   WordDocPolicy := DocsCollection.Add(vFileName,
     EmptyParam, EmptyParam, vVisible) as _DocumentDisp;

   aRange := aSelection.Range as RangeDisp;
   vRange := aRange;
   aBookmarks := WordDocPolicy.Bookmarks as BookmarksDisp;
   aBookmarks.Add('My_bookmark_name', vRange);

{
... do some more work to create more bookmarks in WordDocPolicy.
}
ClearBookmarks();

end;

Open in new window

Avatar of systan
systan
Flag of Philippines image

hi
Did you try;
For i := 0  to  (aBookmarks.count-1) do begin
Avatar of SteveFarndon2000

ASKER

Systan,

The index on the .Bookmarks collection starts at 1.
A value of i=0 inside the loop gives an error.

Needs some more thought, eh?
Avatar of aflarin
aflarin

>>   aRange := aSelection.Range as RangeDisp;

but aSelection is not initialized yet, isn't it?

If aBookmarks.Count >=0 Then
For i := 0  to  (aBookmarks.count-1) do begin

All arrays starts with zero?,  aBookmarks is possibly a TList or TStringList
OR
remove;
 If aBookmarks.Count >= 1 Then
This might not solve your problem, but it is good practice to delete items in a list, in a reverse order:

If aBookmarks.Count >= 1 Then
  for i := aBookmarks.count downto 1 do
    aBookmarks(i).Delete;

Also, did you save the file? :-)
For me, this way;
procedure ClearBookmarks();
var i : integer;
begin
  For i := 0  to (aBookmarks.count-1) do  aBookMarks.delete(i);
end;
>> This might not solve your problem, but it is good practice to delete items in a list, in a reverse order:

congratulation, you have sharp eye, DragonSlayer
I believe this is a solution
type BookmarksDisp = TStringList;

var
aBookMarks : BookmarksDisp;
...
//form loads?
aBookMarks.Create;
...
//add bookmarks?
if aBookMarks.indexOf(My_bookmark_name) <> -1 then
aBookMarks.Add(My_bookmark_name);
....
//delete bookmarks?
aBookMarks.delete(0);
v
v
v
procedure ClearBookmarks();
var i : integer;
begin
i := aBookMarks.Count-1;
if i <> -1 then
For i := 0  to (aBookmarks.count-1) do  aBookMarks.delete(i);
end;


Systan, bookmarks in Word Automation are indexed from 1 onwards, not 0 :-)
@aflarin, I don't have Delphi in this machine, so I can't really test if it is indeed a solution :-)
ok;
procedure ClearBookmarks();
var i : integer;
begin
i := aBookMarks.Count;
if i <> 0 then
For i := 1  to (aBookmarks.count) do  aBookMarks.delete(i);
end;
@DragonSlayer, I can't test it too, but I think so :)

Steve, if you have 26 bookmarks, I guess only 13 ones remain after ClearBookmarks. Am I right? :)
@aflarin, well since I can't test it, I don't wanna jump the gun and shout "I've got it I've got it!" LoL
EE should have a demerit system too, to discourage experts from simply posting answers... I like the way how "stack-over---ooops i mean the forum-whose-name-shall-not-be-spoken-here" does it.
in short, don't celebrate early,  there's still an open post.
@DragonSlayer, you're scaring me... when I look at your points and think that you probably test the most appropriate solutions... and if you shouted after each... what your neighbors think about it? :)

>> EE should have a demerit system too
They already have "Was this comment helpfull". Isn't is enough for you? Or you told about something else?

PS I didn't call you to celebrate, just told my opinion. Sorry if it was unnecessary for you
DragonSlayer,

Thanks for your suggestion about saving the .doc first. This looks the most likely solution given M$ and the OLE automation quirkiness.
Will test this on Wed. Bit pulled out right now.

cheers
@aflarin, you're the one with the Guru title here :-P

Nothing to apologise about. We were all just ranting, lol
ASKER CERTIFIED SOLUTION
Avatar of Geert G
Geert G
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK, Gurt, that looks good too. Myself I can'y see the difference between your solution and my non-working version/ since your 'Bookmark' and my 'aBookmark' are of the same type.

Appreciate the comments from the rest of you. Will test the alternative solutions soon, I promise. Please hear with me. Cheers, Stephen
i think you just need to add the save option as you already mentioned