Solved

How to delete all bookmarks in Word 2000 document

Posted on 2010-08-30
21
764 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:SteveFarndon2000
  • 7
  • 5
  • 4
  • +2
21 Comments
 
LVL 14

Expert Comment

by:systan
Comment Utility
hi
Did you try;
For i := 0  to  (aBookmarks.count-1) do begin
0
 

Author Comment

by:SteveFarndon2000
Comment Utility
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?
0
 
LVL 13

Expert Comment

by:aflarin
Comment Utility
>>   aRange := aSelection.Range as RangeDisp;

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

0
 
LVL 14

Expert Comment

by:systan
Comment Utility
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
0
 
LVL 14

Expert Comment

by:systan
Comment Utility
OR
remove;
 If aBookmarks.Count >= 1 Then
0
 
LVL 14

Expert Comment

by:DragonSlayer
Comment Utility
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? :-)
0
 
LVL 14

Expert Comment

by:systan
Comment Utility
For me, this way;
procedure ClearBookmarks();
var i : integer;
begin
  For i := 0  to (aBookmarks.count-1) do  aBookMarks.delete(i);
end;
0
 
LVL 13

Expert Comment

by:aflarin
Comment Utility
>> 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
0
 
LVL 14

Expert Comment

by:systan
Comment Utility
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;


0
 
LVL 14

Expert Comment

by:DragonSlayer
Comment Utility
Systan, bookmarks in Word Automation are indexed from 1 onwards, not 0 :-)
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 14

Expert Comment

by:DragonSlayer
Comment Utility
@aflarin, I don't have Delphi in this machine, so I can't really test if it is indeed a solution :-)
0
 
LVL 14

Expert Comment

by:systan
Comment Utility
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;
0
 
LVL 13

Expert Comment

by:aflarin
Comment Utility
@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? :)
0
 
LVL 14

Expert Comment

by:DragonSlayer
Comment Utility
@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.
0
 
LVL 14

Expert Comment

by:systan
Comment Utility
in short, don't celebrate early,  there's still an open post.
0
 
LVL 13

Expert Comment

by:aflarin
Comment Utility
@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
0
 

Author Comment

by:SteveFarndon2000
Comment Utility
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
0
 
LVL 14

Expert Comment

by:DragonSlayer
Comment Utility
@aflarin, you're the one with the Guru title here :-P

Nothing to apologise about. We were all just ranting, lol
0
 
LVL 36

Accepted Solution

by:
Geert Gruwez earned 500 total points
Comment Utility
This works: (I even tested it with D2010 :) )


type
  TfrmTestEE = class(TForm)
    btnShowBookmark: TButton;
    Doc: TWordDocument;
    memInfo: TMemo;
    WordApp: TWordApplication;
    btnDeleteBookmarks: TButton;
    procedure btnShowBookmarkClick(Sender: TObject);
    procedure btnDeleteBookmarksClick(Sender: TObject);
  end;

var
  frmTestEE: TfrmTestEE;

implementation

{$R *.dfm}

procedure TfrmTestEE.btnShowBookmarkClick(Sender: TObject);
var
  I: integer;
  bm: Bookmark;
  FileName, X: OleVariant;
begin
  FileName := 'c:\data\test.doc';
  WordApp.Connect;
  WordApp.Documents.Open(FileName, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
    EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam);
  Doc.ConnectTo(WordApp.ActiveDocument);
  for I := 0 to Doc.Bookmarks.Count - 1 do
  begin
    X := I+1;
    bm := Doc.Bookmarks.Item(X);
    memInfo.Lines.Add(bm.Name);
  end;
  Doc.Disconnect;
  WordApp.Disconnect;
end;

procedure TfrmTestEE.btnDeleteBookmarksClick(Sender: TObject);
var
  bm: Bookmark;
  FileName, X: OleVariant;
begin
  FileName := 'c:\data\test.doc';
  WordApp.Connect;
  WordApp.Documents.Open(FileName, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
    EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam);
  Doc.ConnectTo(WordApp.ActiveDocument);
  while Doc.Bookmarks.Count > 0 do
  begin
    X := 1;
    bm := Doc.Bookmarks.Item(X);
    bm.Delete;
  end;
  Doc.Save;
  Doc.Disconnect;
  WordApp.Disconnect;
end;

Open in new window

0
 

Author Comment

by:SteveFarndon2000
Comment Utility
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
0
 
LVL 36

Expert Comment

by:Geert Gruwez
Comment Utility
i think you just need to add the save option as you already mentioned
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now