Mirror

Open a WORD-document and replace Bookmarks with given Values (Views: 67)


Problem/Question/Abstract:

An Example how to Open a WORD-Document and replace the bookmarks inside of it.

Answer:

here I try to give a detailed Example how to Open an existing WORD-document from the background of an application an replace bookmarks in this document with given text.

Global declarations:

TDocKapselWORD = class(TDocKapsel)
private
  OleWord: OLEVariant;
public
  function DocClose: Integer;
  function DocNew(VorlagenFilename: string): Integer;
  function ReplaceTM(TM_Name, Ergebnis: string): Integer;
  procedure Test;
end;

function TDocKapselWORD.DocNew(TemplateFilename: string): Integer;
// Opens the connection to WORD
// Returns 0 when ok, -1 on Error
var
  LocalWordDoc: OLEVariant;
  rtnCode: integer;
begin
  rtnCode := 0;
  // Is OLEWord still open? When yes, -> Error
  if VarIsEmpty(OLEWord) = FALSE then
    rtnCode := -1
  else
  begin
    try
      LocalWordDoc := CreateOleObject('WORD.Document');
    except
      // OLE-connection not successful
      rtnCode := -1;
    end;
    if rtnCode >= 0 then
    begin
      // New Document with given template
      LocalWordDoc.Application.Documents.Add(TemplateFilename);
      // Put new document in private variable
      OLEWord := LocalWordDoc.Application.ActiveDocument;
      LocalWordDoc.close();
      // Everything gone ok?
      if OLEWord.Application.Documents.Count > 0 then
        rtnCode := 0
      else
        RtnCode := -1;
    end;
  end;
  DocNew := rtnCode;
end;

function TDocKapselWORD.ReplaceTM(TM_Name, Ergebnis: string): Integer;
// Replaces Bookmark TM_Name with String Ergebnis
// returns 0 when ok, -1 on error.
begin
  if OLEWord.Bookmarks.exists(TM_Name) then
  begin
    OLEWord.Bookmarks.Item(TM_Name).Range.Text := Ergebnis;
    if OLEWord.Bookmarks.exists(TM_Name) then
      result := -1
    else
      result := 0
  end
  else
    result := -1;
end;

function TDocKapselWORD.DocClose: Integer;
// Closes Document and OLE-connection
// Returns 0 when ok, -1 on Error
var
  rtnCode: integer;
begin
  result := -1;
  if not VarIsEmpty(OleWord) then
  try
    OleWord.close();
    OleWord := unassigned;
    if VarIsEmpty(OleWord) then
      result := 0
    else
      result := -1;
  except
    OleWord := unassigned;
    result := -1;
  end;
end;

procedure TDocKapselWord.test;
var
  BMCount, BM: integer;
  MyBookmarks: array[1..42] of string;
  MyTexts: array[1..42] of string;
  rtnCode: integer
begin
  rtnCode := DocNew('TestFile.DOT');
  if rtnCode = 0 then
  begin // Go on only when Opened
    ...
      // Here You need to initialize the bookmarks: how many, what text to which
    // bookmark and so on. I suppose here, it's in two arrays!
    ...
      BM := 1
      repeat
      rtnCode := ReplaceTM(bookmarks[BM], texts[BM]);
      BM := BM + 1;
    until (BM > 42) or (rtnCode < 0);
    ...
      // Some Processing afterwards, perhaps print or save
    ...
      rtnCode := DocClose;
  end;
  ...
    // Some Processing, when it was or was not successful
  ...
end;

<< Back to main page