Mega Code Archive

 
Categories / Delphi / Examples
 

Delphitoword

Getting data from Delphi app into Word document From: Darek Maluchnik <embrio@plearn.edu.pl> Assuming that you have Word2(6)/Delphi1 or 32bit Word/Delphi2. Try: Make macro in Word: Declare Function StringFromDelphi Lib "c:\sample\test.dll" As String Sub MAIN mystring$ = StringFromDelphi Insert mystring$ End Sub Create simple TEST.DLL in Delphi - just form with a button. Save it (eg.in c:\sample - see Word macro) as test.dpr and testform.pas. Now add to your project EXPORTED function 'StringFromDelphi' and 'close' on button click. You can paste the stuff from below: library Test; (* test.dpr in c:\sample *) uses Testform in 'TESTFORM.PAS'; exports StringFromDelphi; begin end. unit Testform; (* testform.pas in c:\sample *) interface uses WinTypes, WinProcs, Forms, Classes, Controls, StdCtrls, SysUtils; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); end; var Form1: TForm1; function StringFromDelphi : PChar; export; {$ifdef WIN32} stdcall; {$endif} implementation {$R *.DFM} function StringFromDelphi: Pchar; var StringForWord : array[0..255] of char; begin Application.CreateForm(TForm1, Form1); Form1.ShowModal; Result:=StrPCopy(StringForWord, Form1.Button1.caption); end; procedure TForm1.Button1Click(Sender: TObject); begin close; end; end. Compile test.dll. Run macro from Word, Delphi form should appear - click the button to get some data from Delphi. There is a text in PCMagazine Vol12.No22 on accessing DLL functions from Word. You can get it (DLLACCES) from PCMag web site. WordBasic via OLE From: cehjohnson@aol.com (CEHJohnson) Try the following: MsWord := CreateOleObject('Word.Basic'); MsWord.FileNewDefault; MsWord.TogglePortrait; Creating Word Documents From: amccullo@ccu1.auckland.ac.nz (Alan MCCulloch) I have found the following works well D2 -> Word 97, using "Bookmark" fields in Word. .. .. .. implementation uses OleAuto; .. .. .. var V : Variant ; .. .. .. V := 0; // at some point just to initialise .. .. .. some functions if V = 0 then begin V := CreateOLEObject('Word.Application'); V.WordBasic.AppShow; end; // this example assumes we are filling in some bookmark // fields on a "standard letter", from a query that has previously // been executed, in a data module called pnm_data (OK , should // have used a with...block !) V.WordBasic.Fileopen('Your Word Doc name'); V.WordBasic.EditBookmark('Title',0,0,0,1); V.WordBasic.Insert(Title); V.WordBasic.EditBookmark('FirstName',0,0,0,1); V.WordBasic.Insert(FirstName + ' '); V.WordBasic.EditBookmark('LastName',0,0,0,1); V.WordBasic.Insert(pnm_data.ContactsQuery1Fam_Name.AsString + ' '); V.WordBasic.EditBookmark('Address1',0,0,0,1); V.WordBasic.Insert(pnm_data.ContactsQuery1Address1.AsString + ' '); V.WordBasic.EditBookmark('Address2',0,0,0,1); V.WordBasic.Insert(pnm_data.ContactsQuery1Address2.AsString + ' '); V.WordBasic.EditBookmark('Address3',0,0,0,1); V.WordBasic.Insert(pnm_data.ContactsQuery1Address3.AsString + ' '); V.WordBasic.EditBookmark('Title1',0,0,0,1); V.WordBasic.Insert(Title); V.WordBasic.EditBookmark('LastName1',0,0,0,1); V.WordBasic.Insert(pnm_data.ContactsQuery1Fam_Name.AsString + ' '); (You could V.WordBasic.PrintDefault; if you want to tell Word to print it as well....and many other commands, like saving, changing font etc can be done) ....etc Starting Word without AutoStart Macro From: "Rui Chambel" <rchambel@inix.pt> To disable the AutoOpen Macro, you can execute this command WordBasic.DisableAutoMacros First you must create the WordBasic object and then execute that method. Wordbasic from Delphi using Parameters From: Steve Diederichs Here is some code done for Delphi 2.0 to Word 95. I tested a little bit when Word 97 came out and a few changes were required -- didn't actually convert to Word 97 because at the time it proved to be significantly slower than Word 95. The SendKeys were used out of desperation. Function TAutoMerge.ProcessMerge(FSource, FData, FOutput : string) : boolean; var MSWord : Variant; i, NumDocs : integer; Found : boolean; s, LastOLECommand : string; begin ProcessMerge := False; try LastOLECommand := 'Creating OLE Object.'; MSWord := CreateOLEObject('Word.Basic'); LastOLECommand := 'Show MS Word.'; MSWord.AppShow; Application.ProcessMessages; LastOLECommand := 'Open document file >' + FSource + '<.'; MSWord.FileOpen(Name := FSource, ConfirmConversions := 0, ReadOnly := 1, AddToMru := 0, PasswordDoc := '', PasswordDot := '', Revert := 0, WritePasswordDoc := '', WritePasswordDot := ''); LastOLECommand := 'Screen updating = false.'; MSWord.ToolsOptionsSpelling(AutomaticSpellChecking := 0); LastOLECommand := 'Set background printing to off.'; MSWord.ToolsOptionsPrint(Background := 0); Application.ProcessMessages; LastOLECommand := 'Open Data file >' + FData + '<.'; MSWord.MailMergeOpenDataSource(Name := FData, ConfirmConversions := 0, ReadOnly := 1, LinkToSource := 1, AddToMru := 0, PasswordDoc := '', PasswordDot := '', WritePasswordDoc := '', WritePasswordDot := '', Connection := '', SQLStatement := '', SQLStatement1 := '', Revert := 1); LastOLECommand := 'Start the Mail Merge.'; MSWord.MailMerge(CheckErrors := 2, Destination := 1, MergeRecords:= 0, From := '', To := '', Suppression := 0, MailSubject := '', MailAsAttachment := 0, MailAddress := ''); LastOLECommand := 'Set up for SendKeys to select printer.'; Application.ProcessMessages; MSWord.AppShow; s := '{home}%l{enter}{home}%n' + FOutput + '{tab}{enter}{home}{enter}'; // sdd 1.1 MSWord.SendKeys(s, -1); MSWord.MailMergeToPrinter; Application.ProcessMessages; ProcessMerge := True; LastOLECommand := 'All done with merge.'; except on EOleException do begin inc(TotalOLEErrors); lblStatus.caption := LastOLECommand; if (TotalOLEErrors >= TOTALOLEERRORS_MAX) then begin s := 'There has been at least one OLE error(' + IntToStr(TotalOLEErrors) + '), the last one was >' + LastOLECommand + '<.'; ShowMessage(s); end; end end; end; ************************************************************************ IF YOU NEED TO CHECK FROM A DEPHI APP WHETHER WORD IS ALREADY OPEN, THEN Try to use GetActiveOleObject before you call CreateOleObject. procedure GetWordObject : Variant; begin Result := nil; try Result := GetActiveOleObject('Word.Basic'); except try Result := CreateOleObject('Word.Basic'); except MessageDlg('Word is not available !',mtInformation,[mbOk],0); Result := nil; end; end; end;