procedure THaupt.ButtonStartWord2;
begin
if not Assigned(FWordApp)
then FWordApp := TWordApplication.Create(Self);
try
FWordApp.Connect;
except
on E:
Exception do begin
FreeAndNil(FWordApp);
Showmessage('
Die Verbindung zu Word konnte nicht ' + '
hergestellt werden:' + #10#13 + E.
Message);
Raise;
end;
// on E
end;
// try except
FWordApp.Visible := True;
FWordApp.WindowState := wdWindowStateMinimize;
// FWordApp.OnQuit := OnWordAppClose;
end;
function GetRTFFormat(DataObject: IDataObject;
var RTFFormat: TFormatEtc): Boolean;
var
Formats: IEnumFORMATETC;
TempFormat: TFormatEtc;
pFormatName: PChar;
Found: Boolean;
begin
try
OleCheck(DataObject.EnumFormatEtc(DATADIR_GET, Formats));
Found := False;
while (
not Found)
and (Formats.Next(1, TempFormat,
nil) = S_OK)
do
begin
pFormatName := AllocMem(255);
GetClipBoardFormatName(TempFormat.cfFormat, pFormatName, 254);
if (
string(pFormatName) = '
Rich Text Format')
then
begin
RTFFormat := TempFormat;
Found := True;
end;
FreeMem(pFormatName);
end;
Result := Found;
except
Result := False;
end;
end;
procedure THaupt.WriteToMSWord(
const RTFText:
String);
var
WordDoc: _Document;
DataObj : IDataObject;
Formats : IEnumFormatEtc;
RTFFormat: TFormatEtc;
Medium : TStgMedium;
pGlobal : Pointer;
begin
// WordApplication1.Documents.Add(EmptyParam, EmptyParam, EmptyParam, EmptyParam);
WordDoc := WordApplication1.ActiveDocument;
OleCheck(WordDoc.QueryInterface(IDataObject,DataObj));
GetRTFFormat(DataObj, RTFFormat);
FillChar(Medium,SizeOf(Medium),0);
Medium.tymed := RTFFormat.tymed;
Medium.hGlobal := GlobalAlloc(GMEM_MOVEABLE, Length(RTFText)+1);
try
pGlobal := GlobalLock(Medium.hGlobal);
CopyMemory(PGlobal,PChar(RTFText),Length(RTFText)+1);
GlobalUnlock(Medium.hGlobal);
OleCheck(DataOBJ.SetData(RTFFormat,Medium,True));
finally
GlobalFree(Medium.hGlobal);
ReleaseStgMedium(Medium);
end;
end;
procedure THaupt.dxWinXPBar3Items3Click(Sender: TObject);
var FileName, newFile: OleVariant;
FPara: Paragraph;
//Absatz
FRange: Range;
//Bereich
vWhat, vBookmark:OleVariant;
begin
DatMod.MTa.First;
Datenblatt.RichEdit1.Lines.Clear;
OpenDialog1.InitialDir := ExtractFilePath(Application.ExeName)+'
drucken\Vorlagen\Maschinenliste\';
OpenDialog1.Filter := '
Dokumentvorlage (*.DOT)|*.dot';
OpenDialog1.DefaultExt := '
dot';
OpenDialog1.Title := '
Sprache laden';
if OpenDialog1.Execute
then
begin
FileName := OpenDialog1.FileName;
WordApplication1.Connect;
WordApplication1.Documents.Open(FileName, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam);
WordDocument1.ConnectTo(WordApplication1.ActiveDocument);
//ANZEIGEN
WordApplication1.visible := true;
FileName := false;
//Jump-To-End
vWhat:=wdGoToBookmark;
vBookmark:='
Einfug';
WordApplication1.Selection.GoTo_(vWhat,emptyParam,emptyParam,vBookmark);
//Formatierung vorab
WordApplication1.Selection.Font.
Name := '
Arial';
WordApplication1.Selection.Font.Size := 10;
//Datensätze Maschinen
DatMod.MTa.First;
while not(DatMod.MTa.Eof)
do
begin
WordApplication1.Selection.Font.Bold := integer(True);
WordApplication1.Selection.TypeText('
Lfd.-Nr.: '+DatMod.MTaLfdNr.AsString+#13);
WordApplication1.Selection.Font.Bold := integer(False);
WriteToMSWord(Memo1.Text);
//WordApplication1.Selection.InsertFile??? GGF.??
WordApplication1.Selection.TypeText('
TABELLE'+#13);
WordApplication1.Selection.TypeText('
Preis / Price: € '+ThouSep(DatMod.MTaVK.AsString, '
.')+'
'+DatMod.MTaVKV.AsString+#13);
WordApplication1.Selection.TypeText('
_________________________'+#13);
DatMod.MTa.Next;
end;
//speichern
SaveDialog1.InitialDir := ExtractFilePath(Application.ExeName)+'
ausdruck\';
SaveDialog1.Filter := '
Microsoft Word-Dokument (*.DOC)|*.doc';
SaveDialog1.DefaultExt := '
doc';
SaveDialog1.FileName := '
';
SaveDialog1.Title := '
Speichern...';
if SaveDialog1.Execute
then
begin
newFile := SaveDialog1.FileName;
WordDocument1.SaveAs(newFile);
end;
//Schließen
WordDocument1.Close(FileName);
WordDocument1.Disconnect;
WordApplication1.Quit;
WordApplication1.Disconnect;
end;
end;