如何用Delphi创建并保存*.mht格式的网络档案文件?

2026-04-10 17:012阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计232个文字,预计阅读时间需要1分钟。

如何用Delphi创建并保存*.mht格式的网络档案文件?

使用以下函数可以从本地HTML文件生成Web归档:

csharpfunction TLessonConstructor2.CreateMHT(const FileName: string): boolean;var oMSG: IMessage; oConfig: IConfiguration; sFileName: string; Stream: _Stream;begin // 初始化 CoInitializeEx(nil, COINIT_APARTMENTTHREADED); // ...end;

使用以下函数表示从本地html文件生成Web归档

function TLessonConstructor2.CreateMHT( const FileName : string):boolean ; var oMSG:IMessage; oConfig: IConfiguration; sFileName: string; Stream: _Stream; begin //CoInitializeEx(nil, COINIT_APARTMENTTHREADED); //CoInitialize(nil); try Result := false; sFileName := ChangeFileExt(FileName, '.mht'); DeleteFile(PAnsiChar(sFileName)); try oConfig := CoConfiguration.Create(); oMSG := CoMessage.Create(); oMSG.Configuration := oConfig; oMSG.CreateMHTMLBody(FileName,CdoSuppressNone,'',''); Stream:=oMSG.GetStream; Stream.SaveToFile(sFileName,adSaveCreateOverWrite); Stream.Cancel; Stream.Close; Result := True; except on E: Exception do begin Result := false; MessageDlg(E.Message, mtError, [mbOK], 0); end; end; finally // CoUnInitialize; Stream:=nil; oConfig:=nil; oMSG:=nil; end; end;

FileName – html的完整路径.

执行oMSG.CreateMHTMLBody(FileName,CdoSuppressNone,”,”)之后;只要基本过程完成,该文件就会被锁定.但是,处理后应删除此文件.

知道问题是什么吗?

CreateMHTMLBody需要URL,因此对于本地文件,请确保以file:///开头

CreateMHTMLBody(const URL: WideString; Flags: CdoMHTMLFlags; const UserName: WideString; const Password: WideString); safecall;

如何用Delphi创建并保存*.mht格式的网络档案文件?

本文共计232个文字,预计阅读时间需要1分钟。

如何用Delphi创建并保存*.mht格式的网络档案文件?

使用以下函数可以从本地HTML文件生成Web归档:

csharpfunction TLessonConstructor2.CreateMHT(const FileName: string): boolean;var oMSG: IMessage; oConfig: IConfiguration; sFileName: string; Stream: _Stream;begin // 初始化 CoInitializeEx(nil, COINIT_APARTMENTTHREADED); // ...end;

使用以下函数表示从本地html文件生成Web归档

function TLessonConstructor2.CreateMHT( const FileName : string):boolean ; var oMSG:IMessage; oConfig: IConfiguration; sFileName: string; Stream: _Stream; begin //CoInitializeEx(nil, COINIT_APARTMENTTHREADED); //CoInitialize(nil); try Result := false; sFileName := ChangeFileExt(FileName, '.mht'); DeleteFile(PAnsiChar(sFileName)); try oConfig := CoConfiguration.Create(); oMSG := CoMessage.Create(); oMSG.Configuration := oConfig; oMSG.CreateMHTMLBody(FileName,CdoSuppressNone,'',''); Stream:=oMSG.GetStream; Stream.SaveToFile(sFileName,adSaveCreateOverWrite); Stream.Cancel; Stream.Close; Result := True; except on E: Exception do begin Result := false; MessageDlg(E.Message, mtError, [mbOK], 0); end; end; finally // CoUnInitialize; Stream:=nil; oConfig:=nil; oMSG:=nil; end; end;

FileName – html的完整路径.

执行oMSG.CreateMHTMLBody(FileName,CdoSuppressNone,”,”)之后;只要基本过程完成,该文件就会被锁定.但是,处理后应删除此文件.

知道问题是什么吗?

CreateMHTMLBody需要URL,因此对于本地文件,请确保以file:///开头

CreateMHTMLBody(const URL: WideString; Flags: CdoMHTMLFlags; const UserName: WideString; const Password: WideString); safecall;

如何用Delphi创建并保存*.mht格式的网络档案文件?