AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

RTF Text in Word Dokument einfügen?

Ein Thema von Meriones · begonnen am 1. Jan 2008
Antwort Antwort
Meriones

Registriert seit: 25. Feb 2007
33 Beiträge
 
#1

RTF Text in Word Dokument einfügen?

  Alt 1. Jan 2008, 21:33
Hallo Delphi Profis,
ich möchte einen Text, den ich als RTF formartiert habe in ein bestehendes Word-Dokument einfügen.
Dazu habe ich folgendes gutes Beispiel gefunden:

http://www.swissdelphicenter.ch/de/showcode.php?id=2060

Delphi-Quellcode:
uses
  Word_TLB, ActiveX, ComObj;

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 WriteToMSWord(const RTFText: String);
var
  WordDoc: _Document;
  WordApp: _Application;
  DataObj : IDataObject;
  Formats : IEnumFormatEtc;
  RTFFormat: TFormatEtc;
  Medium : TStgMedium;
  pGlobal : Pointer;
begin
    try
      GetActiveOleObject('Word.Application').QueryInterface(_Application, WordApp);
    except
      WordApp := CoWordApplication.Create;
    end;
// rausgenommen WordApp.Documents.Add(EmptyParam, EmptyParam, EmptyParam, EmptyParam);
    WordApp.Visible := True;
    WordDoc := WordApp.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 TForm1.Button2Click(Sender: TObject);
begin
  WriteToMSWord(Memo1.Text); // may be rtf-formatted text
end;
Hier wird allerdings immer der gesammte Text des Dokuments überschrieben, ich will aber jeweils nur neuen Text anfügen.
Hat jemand eine Ahnung wie man das machen könnte?

Tausend Dank, Meriones
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 09:01 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz