Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi RTF in Word einfügen (https://www.delphipraxis.net/34156-rtf-word-einfuegen.html)

TypusMensch 18. Nov 2004 11:25


RTF in Word einfügen
 
Folg. Problem:
Ich habe mehrere RTF-Dateien. Diese sind Grundlage! Ich möchte diese jetzt in ein vorhandenes Word-Dokument einfügen:

Delphi-Quellcode:
procedure ....... ;
var FileName, newFile: OleVariant;
    FPara: Paragraph; //Absatz
    FRange: Range; //Bereich
    vWhat, vBookmark:OleVariant;
begin
FileName := 'c:\vorlage.dot';
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;
//hiernach kommt später noch ne Schleife um mehrere RTFs einzufügen...
WordApplication1.Selection.Font.Bold := integer(True);
WordApplication1.Selection.TypeText('Lfd.-Nr.: '+DatMod.MTaLfdNr.AsString+#13);
WordApplication1.Selection.Font.Bold := integer(False);
WordApplication1.Selection.TypeText('Datenblatt:'+#13);
// HIER MUSS DIE RTF EINGEFÜGT WERDEN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//speichern
SaveDialog1.InitialDir := ExtractFilePath(Application.ExeName)+'ausdruck\';
SaveDialog1.Filter := 'Microsoft Word-Dokument (*.DOC)|*.doc';
SaveDialog1.DefaultExt := 'doc';
SaveDialog1.FileName := '';
SaveDialog1.Title := 'Speichern...';
if Haupt.SaveDialog1.Execute then
  begin
  newFile := SaveDialog1.FileName;
  WordDocument1.SaveAs(newFile);
  end;
//Schließen
WordDocument1.Close(FileName);
WordDocument1.Disconnect;
WordApplication1.Quit;
WordApplication1.Disconnect;
end;
Ich weiß, dass es die Prozedur WordApplication1.Selection.InsertFile gibt , weiß aber nicht ob die dafür geeignet ist und wie ich diese verwende. Hab nix in Google, DP oder Hilfe gefunden...

ThX
Marcus

fkerber 18. Nov 2004 13:46

Re: RTF in Word einfügen
 
Hi!

Warumm lädst du die RTF-Datei nicht vorher in ein Richedit und "überträgst" sie dann?


Ciao Frederic

TypusMensch 18. Nov 2004 15:10

Re: RTF in Word einfügen
 
Nun ja. In den RTFs sind vorwiegend Tabellen. Wie soll ich die den von RichEdit (in diesem Fall Jedi RichEdit, JVCL) zum Word-Dokument übertragen.

Oder was vielleicht besser ist:
Wie kann ich eine Word-Datei (DOC) in einer andere Einfügen? Wenn ich von vornherein auf DOCs umsteige, ist es eh besser.

[edit=Admin]'Pushen' erst nach 24 Stunden. Folgenden 'Push'-Beitrag entfernt. Mfg, Daniel[/edit]

TypusMensch 21. Nov 2004 13:04

Re: RTF in Word einfügen
 
okay. Nach langem Suchen bin ich auf das hier gestoßen. Es lädt eine RTF in ein Doc. Wenn aber ich das in einer Schleife einbinde, öffnet er jedesmal ein neues Dokument, statt es hintereinander einzufügen. Zweiten lädt er auch die RTFs nicht komplett ein. Verstehe ich nicht, vielleicht kann jemand mit dem Quellcode was anfangen, diesen modifizieren auf mein Problem. Ich selbst fühle mich nicht in der Lage. Leider verstehe ich die zwei Funktionen nicht annähernd. Und jeglicher Versuch scheiterte.

MfG
Marcus

toms 21. Nov 2004 18:26

Re: RTF in Word einfügen
 
Zitat:

Zitat von TypusMensch
okay. Nach langem Suchen bin ich auf das hier gestoßen. Es lädt eine RTF in ein Doc. Wenn aber ich das in einer Schleife einbinde, öffnet er jedesmal ein neues Dokument, statt es hintereinander einzufügen.

Hi,

Ist ja klar warum, denn es wird ja dies jedesmal aufgerufen:
Delphi-Quellcode:
WordApp.Documents.Add(...

TypusMensch 22. Nov 2004 11:53

Re: RTF in Word einfügen
 
Okay. Ich möchte nicht unhöflich erscheinen, aber das Kommentar bringt mich trotzdem nicht weiter.

Anmerkung: Ich selbst habe auch schon versucht, diese Zeile einfach wegzulassen, doch dann ist das Ergebnis immernoch nicht das, was gewünscht ist. Er lädt, dann zwar eine Memo ein aber löscht die zuvor eingeladene DOT-Datei.

Also die Prozedur dxWinXPBar3Items3Click startet das Ganze. ButtonStartWord2 startet Word, wird aufgerufen aus dxWinXPBar3Items3Click. GetRTFFormat und WriteToMSWord ist für das Einladen einer RTF da.

Es kann doch aber auch nicht so schwer sein, eine Word-Datei in eine anderen an beliebiger Stelle einzufügen!!! :x

Ich gebe ma den kompletten Quellcode, hoffe jemand kann mir da echt ma helfen, wird knapp, muss das ganze doch bald abgeben :cry: :

Delphi-Quellcode:
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;

TypusMensch 23. Nov 2004 11:19

Re: RTF in Word einfügen
 
Hallo, ich noch einmal.

Ich möchte doch nicht mehr als eine DOC-Datei (Word-Dokument) in eine andere einfügen. Das kann doch nicht so schwer sein. Ich meine, wenn man schon mittels OLE Tabellen erstellen, Grafiken einbinden und sonstige Wunder vollbringen kann, dann kann doch das auch nicht so schwer sein. :cry: :( :cry:

MfG
Marcus

spacewolf 13. Jul 2005 15:05

Re: RTF in Word einfügen
 
schon mal per Zwischenablage probiert ?

DGL-luke 13. Jul 2005 16:07

Re: RTF in Word einfügen
 
du musst doch anstatt
Delphi-Quellcode:
WordApp.Documents.Add(...
auch auf deines der documents zugreigen und etwas einfügen können. sprich [delphi]WordApp.Documents[0].Insert[/delphi oder irgendsoetwas.


Alle Zeitangaben in WEZ +1. Es ist jetzt 21:52 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz