Author: Jonas Bilinkevicius
How to insert text at a Bookmark
Answer:
1 uses2 ComObj;
3 4 procedure TForm1.Button1Click(Sender: TObject);
5 const6 // Word Document to open7 YourWordDocument = 'c:\test\worddoc.doc';
8 var9 BookmarkName, Doc, R: OleVariant;
10 begin11 // Start a Word instance12 try13 WordApp := CreateOleObject('Word.Application');
14 except15 ShowMessage('Could not start MS Word!');
16 end;
17 // Open your Word document18 WordApp.Documents.Open(YourWordDocument);
19 Doc := WordApp.ActiveDocument;
20 21 // name of your bookmark22 BookmarkName := 'MyBookMark';
23 24 // Check if bookmark exists25 if Doc.Bookmarks.Exists(BookmarkName) then26 begin27 R := Doc.Bookmarks.Item(BookmarkName).Range;
28 // Add text at our bookmark29 R.InsertAfter('Text in bookmark');
30 // You make a text formatting like changing its color31 R.Font.Color := clRed;
32 end;
33 34 // Save your document and quit Word35 ifnot VarIsEmpty(WordApp) then36 begin37 WordApp.DisplayAlerts := 0;
38 WordApp.Documents.Item(1).Save;
39 WordApp.Quit;
40 BookmarkName := Unassigned;
41 R := Unassigned;
42 WordApp := Unassigned;
43 end;
44 end;