如何把一个Word文档流直接加载到OleContainer中?

procedure LoadOleCtnrFromStream(OleContainer: TOleContainer; Stream: TStream);
const
  DataFormatCount
= 2;
  StreamSignature
= $434F4442; {'BDOC'}
type
  TStreamHeader
= record
   
case Integer of
     
0: ( { New }
        Signature: Integer;
        DrawAspect: Integer;
        DataSize: Integer);
     
1: ( { Old }
        PartRect: TSmallRect);
 
end;
var
  Header: TStreamHeader;
  Mem: TMemoryStream;
begin
  Mem:
= TMemoryStream.Create;
  try
    Header.Signature :
=  StreamSignature;
    Header.DrawAspect :
= 1;
    Mem.Write(Header, SizeOf(Header));
    Mem.CopyFrom(Stream,
0);
    Mem.Position :
= 0;
    OleContainer.LoadFromStream(Mem);
   
//OleContainer.DoVerb(0);//就可以通过OleContainer.OleObject得到Word文档对象
  finally
    Mem.Free;
 
end;
end;

//测试代码,当然你也可以通过从数据库的一字段取出Word文档然后放到一个流里传给LoadOleCtnrFromStream函数
procedure TForm1.Button4Click(Sender: TObject);
var
  fileStream: TFileStream;
begin
  fileStream :
= TFileStream.Create('D:/1.doc', fmOpenReadWrite);
  try
    LoadOleCtnrFromStream(OleContainer, fileStream);
  finally
    fileStream.Free;
 
end;
end;

你可能感兴趣的:(delphi笔记It's,long,long,ago,文档,integer,header,stream,数据库,测试)