AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Multimedia Delphi Word: Via OLE Automation RTF einfügen
Thema durchsuchen
Ansicht
Themen-Optionen

Word: Via OLE Automation RTF einfügen

Ein Thema von RedOne · begonnen am 2. Jun 2008 · letzter Beitrag vom 3. Jun 2008
Antwort Antwort
Benutzerbild von Remko
Remko

Registriert seit: 10. Okt 2006
Ort: 's-Hertogenbosch, Die Niederlande
222 Beiträge
 
RAD-Studio 2010 Arc
 
#1

Re: Word: Via OLE Automation RTF einfügen

  Alt 3. Jun 2008, 21:45
Maybe the code below helps you, it's a snippet from one of my old programs.
ReadFromWord takes an active word document as input var and outputs the document's contents as RTF text. WriteToWord goes the other way round.


Delphi-Quellcode:
function TForm1.ReadFromWord(Doc: _Document): PChar;
var DataObject: IDataObject;
    ReturnData: TStgMedium;
    RTFFormat: TFormatEtc;

    begin
  if Assigned(Doc) then begin
    try
      if Doc.QueryInterface(IDataObject, DataObject) = 0 then begin
        cfRTF := RegisterClipboardFormat('Rich Text Format');
        RTFFormat.cfFormat := CFRTF;
        RTFFormat.tymed := TYMED_HGLOBAL;
        RTFFormat.ptd := nil;
        RTFFormat.dwAspect := DVASPECT_CONTENT;
        RTFFormat.lindex := -1;
        OleCheck(DataObject.GetData(RTFFormat, ReturnData));
        ReadFromWord := GlobalLock(ReturnData.hglobal);
        GlobalUnlock(ReturnData.hglobal);
      end
      else begin
        ShowMessage('QueryInterface failed');
      end;
    except
      ShowMessage('Error while getting RTF');
    end;
  end;
end;

procedure TForm1.WriteToMSWord(WordDoc: _Document; const RTFText: String);
var
  cfRTF: Integer;
  DataObj : IDataObject;
  RTFFormat: TFormatEtc;
  Medium : TStgMedium;
  pGlobal : Pointer;

begin
    OleCheck(WordDoc.QueryInterface(IDataObject,DataObj));
    cfRTF := RegisterClipboardFormat('Rich Text Format');
    RTFFormat.cfFormat := CFRTF;
    RTFFormat.tymed := TYMED_HGLOBAL;
    RTFFormat.ptd := nil;
    RTFFormat.dwAspect := DVASPECT_CONTENT;
    RTFFormat.lindex := -1;
    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;
  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 11:57 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