Articles   Members Online: 3
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to open a WORD-document and replace Bookmarks with given Values Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
02-Jun-03
Category
OLE
Language
Delphi 2.x
Views
174
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Andreas Heidenreich

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: 

1   TDocKapselWORD = class(TDocKapsel)
2   private
3     OleWord: OLEVariant;
4   public
5     function DocClose: Integer;
6     function DocNew(VorlagenFilename: string): Integer;
7     function ReplaceTM(TM_Name, Ergebnis: string): Integer;
8     procedure Test;
9   end;
10  
11  function TDocKapselWORD.DocNew(TemplateFilename: string): Integer;
12  // Opens the connection to WORD
13  // Returns 0 when ok, -1 on Error
14  var
15    LocalWordDoc: OLEVariant;
16    rtnCode: integer;
17  begin
18    rtnCode := 0;
19    // Is OLEWord still open? When yes, -> Error
20    if VarIsEmpty(OLEWord) = FALSE then
21      rtnCode := -1
22    else
23    begin
24      try
25        LocalWordDoc := CreateOleObject('WORD.Document');
26      except
27        // OLE-connection not successful
28        rtnCode := -1;
29      end;
30      if rtnCode >= 0 then
31      begin
32        // New Document with given template
33        LocalWordDoc.Application.Documents.Add(TemplateFilename);
34        // Put new document in private variable
35        OLEWord := LocalWordDoc.Application.ActiveDocument;
36        LocalWordDoc.close();
37        // Everything gone ok?
38        if OLEWord.Application.Documents.Count > 0 then
39          rtnCode := 0
40        else
41          RtnCode := -1;
42      end;
43    end;
44    DocNew := rtnCode;
45  end;
46  
47  function TDocKapselWORD.ReplaceTM(TM_Name, Ergebnis: string): Integer;
48  // Replaces Bookmark TM_Name with String Ergebnis
49  // returns 0 when ok, -1 on error.
50  begin
51    if OLEWord.Bookmarks.exists(TM_Name) then
52    begin
53      OLEWord.Bookmarks.Item(TM_Name).Range.Text := Ergebnis;
54      if OLEWord.Bookmarks.exists(TM_Name) then
55        result := -1
56      else
57        result := 0
58    end
59    else
60      result := -1;
61  end;
62  
63  function TDocKapselWORD.DocClose: Integer;
64  // Closes Document and OLE-connection
65  // Returns 0 when ok, -1 on Error
66  var
67    rtnCode: integer;
68  begin
69    result := -1;
70    if not VarIsEmpty(OleWord) then
71    try
72      OleWord.close();
73      OleWord := unassigned;
74      if VarIsEmpty(OleWord) then
75        result := 0
76      else
77        result := -1;
78    except
79      OleWord := unassigned;
80      result := -1;
81    end;
82  end;
83  
84  procedure TDocKapselWord.test;
85  var
86    BMCount, BM: integer;
87    MyBookmarks: array[1..42] of string;
88    MyTexts: array[1..42] of string;
89    rtnCode: integer
90  begin
91    rtnCode := DocNew('TestFile.DOT');
92    if rtnCode = 0 then
93    begin // Go on only when Opened
94      ...
95        // Here You need to initialize the bookmarks: how many, what text to which
96      // bookmark and so on. I suppose here, it's in two arrays!
97      ...
98        BM := 1
99        repeat
100       rtnCode := ReplaceTM(bookmarks[BM], texts[BM]);
101       BM := BM + 1;
102     until (BM > 42) or (rtnCode < 0);
103     ...
104       // Some Processing afterwards, perhaps print or save
105     ...
106       rtnCode := DocClose;
107   end;
108   ...
109     // Some Processing, when it was or was not successful
110   ...
111 end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC