Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 795
  • Last Modified:

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

0
SteveFarndon2000
Asked:
SteveFarndon2000
  • 7
  • 5
  • 4
  • +2
1 Solution
 
systanCommented:
hi
Did you try;
For i := 0  to  (aBookmarks.count-1) do begin
0
 
SteveFarndon2000Managing DirectorAuthor Commented:
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
 
aflarinCommented:
>>   aRange := aSelection.Range as RangeDisp;

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

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
systanCommented:
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
 
systanCommented:
OR
remove;
 If aBookmarks.Count >= 1 Then
0
 
DragonSlayerCommented:
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
 
systanCommented:
For me, this way;
procedure ClearBookmarks();
var i : integer;
begin
  For i := 0  to (aBookmarks.count-1) do  aBookMarks.delete(i);
end;
0
 
aflarinCommented:
>> 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
 
systanCommented:
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
 
DragonSlayerCommented:
Systan, bookmarks in Word Automation are indexed from 1 onwards, not 0 :-)
0
 
DragonSlayerCommented:
@aflarin, I don't have Delphi in this machine, so I can't really test if it is indeed a solution :-)
0
 
systanCommented:
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
 
aflarinCommented:
@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
 
DragonSlayerCommented:
@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
 
systanCommented:
in short, don't celebrate early,  there's still an open post.
0
 
aflarinCommented:
@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
 
SteveFarndon2000Managing DirectorAuthor Commented:
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
 
DragonSlayerCommented:
@aflarin, you're the one with the Guru title here :-P

Nothing to apologise about. We were all just ranting, lol
0
 
Geert GruwezOracle dbaCommented:
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
 
SteveFarndon2000Managing DirectorAuthor Commented:
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
 
Geert GruwezOracle dbaCommented:
i think you just need to add the save option as you already mentioned
0

Featured Post

Industry Leaders: 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!

  • 7
  • 5
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now