Mega Code Archive

 
Categories / Delphi / Activex OLE
 

Using OLE server of Word95 or Word97

Title: Using OLE server of Word95 or Word97 Question: How to start use Word97 OLE server? Answer: There are one difference in starting Word95 and Word97 OLE servers. This difference is name of OLE object that should be created. Below is example how to start Word95 server and insert our data into new document and close it: procedure TForm1.Insert95BtnClick(Sender: TObject); var S: string; MSWord: Variant; begin try MsWord := CreateOleObject('Word.Basic'); except ShowMessage('Could not start Microsoft Word 95!'); Exit; end; S := 'We inserted this line from Delphi!'; MsWord.AppShow; MSWord.FileNew; MSWord.Insert(S); if not VarIsEmpty(MSWord) then begin MSWord.FileSaveAs(Name := 'd:\foo95.doc', AddToMru := 1); MSWord.AppClose; end; end; Below is example how to start Word97 and insert our data into new document and close it: procedure TForm1.Insert97BtnClick(Sender: TObject); const wdBlue = $00000002; wdAlignParagraphCenter = $00000001; var S: string; WordApp: Variant; Range: Variant; begin try WordApp := CreateOleObject('Word.Application'); except ShowMessage('Could not start Microsoft Word 97!'); Exit; end; WordApp.Visible := True; WordApp.Documents.Add; WordApp.Documents.Item(1).Paragraphs.Add; Range := WordApp.Documents.Item(1).Range( WordApp.Documents.Item(1).Paragraphs.Item(WordApp.Documents.Item(1).Paragraphs.Count - 1).Range.End, WordApp.Documents.Item(1).Paragraphs.Item(WordApp.Documents.Item(1).Paragraphs.Count - 1).Range.End); Range.Text := 'We inserted this line from Delphi!' Range.Bold := True; Range.Font.Name := 'Times New Roman'; Range.Font.Size := 18; Range.Font.ColorIndex := wdBlue; Range.ParagraphFormat.Alignment := wdAlignParagraphCenter; WordApp.Documents.Item(1).Paragraphs.Add; if not VarIsEmpty(WordApp) then begin WordApp.DisplayAlerts := 0; WordApp.Documents.Item(1).SaveAs('d:\foo97.doc',,,,True); WordApp.Quit; end; end;