• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1986
  • Last Modified:

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
0
eelpark
Asked:
eelpark
  • 2
1 Solution
 
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
 
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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