Referencing a Word Bookmark in Delphi 5.0

I need an example of referencing a Word Bookmark using Delphi 5.0. It would be even better if the example manipulated the bookmarks and associated text.

Thanks
LVL 1
eelparkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

intheCommented:
Hi,
here's a couple of ways:
note you may need examples.zip from here:
http://www.inprise.nl/delphi/papers/microexcel/me3.html
it contains a file(wordconst.pas)you may need for last demo i post here.

1:
vWordDocument := CreateOleObject('Word.Basic');
    vWordDocument.FileNew;
 
    vWordDocument.EditBookmark 'aBookmark', , , , -1 // goto a bookmark
 
    vWordDocument.insert('some text');
 
    vWordDocument.insert(#13);
    vWordDocument.appshow;
 
The easiest way to get things done is to start recording a macro in Word, do the things you want to do and thereafter examine the recorded macro. All VBA-commands are available in Delphi through the 'word.basic' object.

or 2:

WordApp.ActiveDocument.Bookmarks.Item('Bookmark').Range.Text :=
  'some text';

3:
procedure TForm1.btnStartClick(Sender: TObject);
var
  Filename, SaveChanges: OleVariant;
  aDoc: _Document;
  b1, b2, b3: string;
  Index: OleVariant;
begin
  b1 := 'StdBookMark';
  b2 := 'HeaderBookMark';
  b3 := 'FooterBookMark';
  btnStart.Enabled := false;
  Screen.Cursor := crHourGlass;
  try
    aDoc := WordApp.Documents.Add(EmptyParam, EmptyParam);
    aDoc.Range(EmptyParam, EmptyParam).InsertAfter(
      'This is a document for testing header and footer functions.');     aDoc.Range(EmptyParam, EmptyParam).Bookmarks.Add(b1, EmptyParam);     aDoc.Sections.Item(1).Headers.Item(wdHeaderFooterPrimary).Range.Text
      := 'This is a header';
 
aDoc.Sections.Item(1).Headers.Item(wdHeaderFooterPrimary).Range.Bookmarks.Add(       b2, EmptyParam);
    aDoc.Sections.Item(1).Footers.Item(wdHeaderFooterPrimary).Range.Text
      := 'This is a footer';
 
aDoc.Sections.Item(1).Footers.Item(wdHeaderFooterPrimary).Range.Bookmarks.Add(       b3, EmptyParam);
    if aDoc.Bookmarks.Exists(b1) then
    begin
      Index := b1;
      aDoc.Bookmarks.Item(Index).Range.InsertAfter('> After Bookmark');     end;
    if aDoc.Bookmarks.Exists(b2) then
    begin
      Index := b2;
      aDoc.Bookmarks.Item(Index).Range.InsertAfter('> After Bookmark');     end;
    if aDoc.Bookmarks.Exists(b3) then
    begin
      Index := b3;
      aDoc.Bookmarks.Item(Index).Range.InsertAfter('> After Bookmark');     end;
    Filename := ExtractFilePath(Forms.Application.ExeName) + Edit1.Text       + '.doc';
    OleVariant(aDoc).SaveAs(Filename);
    SaveChanges := wdDoNotSaveChanges;
    aDoc.Close(SaveChanges, EmptyParam, EmptyParam);
  finally
    Screen.Cursor := crDefault;
    btnStart.Enabled := true;
  end;
end;


4:
probably the nicest

procedure TForm1.launchWordClick(Sender: TObject);
begin
    if not assigned (Word) then
    begin
          word := TWordObject.Create;
          with word do
          begin
              Caption := 'Prueba de Objeto Delphi';
              visible := true;
              NewDoc('prueba');
              gotomarca('NomProy');
              InsertText('marca 1');
        end;
    end;
end;
 
procedure TWordObject.GotoMarca(Marca: String);
// place the cursor in a bookmark
var
  what: OleVariant;
  which: OleVariant;
  count: OleVariant;
  name: OleVariant;
begin
    what := wdGotoBookMark;
    which := unAssigned;
    Count := unAssigned;
    Name := marca;
    FWordApp.Selection.GoTo_(what,which,Count, Name);
end;



may also want to read this (nothing about bookmarks just a tutorial type thing):
http://hometown.aol.com/charliecal/TechPapers/DelphiWord.htm

Regards Barry
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
intheCommented:
btw
wdGotoBookMark is one of the consts defined in wordTLB.pas and WordConst.pas in examples.zip file i mentioned ..
0
eelparkAuthor Commented:
Thanks a lot!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.