Solved

How to delete all bookmarks in Word 2000 document

Posted on 2010-08-30
21
777 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 4
  • +2
21 Comments
 
LVL 14

Expert Comment

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

Author Comment

by:SteveFarndon2000
ID: 33559470
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
ID: 33559478
>>   aRange := aSelection.Range as RangeDisp;

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

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:systan
ID: 33559550
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
ID: 33559563
OR
remove;
 If aBookmarks.Count >= 1 Then
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 33560648
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
ID: 33561053
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
ID: 33561185
>> 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
ID: 33561187
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
ID: 33561216
Systan, bookmarks in Word Automation are indexed from 1 onwards, not 0 :-)
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 33561219
@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
ID: 33561317
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
ID: 33561450
@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
ID: 33561665
@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
ID: 33562081
in short, don't celebrate early,  there's still an open post.
0
 
LVL 13

Expert Comment

by:aflarin
ID: 33562277
@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
ID: 33562364
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
ID: 33562614
@aflarin, you're the one with the Guru title here :-P

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

Accepted Solution

by:
Geert Gruwez earned 500 total points
ID: 33564899
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
ID: 33607083
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 37

Expert Comment

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

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi - replicating a form 8 111
Strange behavior when a form is closed 6 77
Delphi 2 91
Delphi Firemonkey: if the Sms contain special characters it won't send it 3 82
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

732 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