was ist neu ¦  programmier tips ¦  indy artikel ¦  intraweb artikel ¦  informationen ¦  links ¦  interviews
 sonstiges ¦  tutorials ¦  Add&Win Gewinnspiel

Tips (1541)

Dateien (137)
Datenbanken (90)
Drucken (35)
Grafik (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Mathematik (76)
Multimedia (45)
Oberfläche (107)
Objekte/
ActiveX (51)

OpenTools API (3)
Sonstiges (126)
Strings (83)
System (266)
VCL (242)

Tips sortiert nach
Komponente


Tip suchen

Tip hinzufügen

Add&Win Gewinnspiel

Werbung

38 Visitors Online


 
...RTF-Text aus einer MS Word Instanz auslesen (ohne Zwischenablage)?
Autor: Thomas Stutz
[ Tip ausdrucken ]  

Tip Bewertung (5):  
     


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;

function GetRTF: string;
var
  
DataObject: IDataObject;
  RTFFormat: TFormatEtc;
  ReturnData: TStgMedium;
  Buffer: PChar;
  WordDoc: _Document;
  WordApp: _Application;
begin
  
Result := '';
  try
    
GetActiveOleObject('Word.Application').QueryInterface(_Application, WordApp);
  except
    
ShowMessage('Error: MSWord is not running');
    Exit;
  end;
  if (WordApp <> nilthen
    try
      
WordDoc := WordApp.ActiveDocument;
      WordDoc.QueryInterface(IDataObject, DataObject);
      if GetRTFFormat(DataObject, RTFFormat) then
      begin
        
OleCheck(DataObject.GetData(RTFFormat, ReturnData));
        //RTF is passed through global memory
        
Buffer := GlobalLock(ReturnData.hglobal);
        //Buffer is a pointer to the RTF text
        
Result := StrPas(Buffer);
        GlobalUnlock(ReturnData.hglobal);
        ReleaseStgMedium(ReturnData);
      end;
    except
      
// Error occured...
    
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  
ss: TStringstream;
  rtfText: string;
begin
  
rtfText := GetRTF;
  ss := TStringStream.Create(rtfText);
  try
    
ss.Position := 0;
    Memo1.Text := rtfText;
    RichEdit1.Lines.LoadFromStream(ss);
  finally
    
ss.Free
  end;
end;



 

Bewerten Sie diesen Tipp:

dürftig
ausgezeichnet


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners