4

使用以下函数表示从本地 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,'','') ; 只要基本过程完成,该文件就会被锁定。但是,此文件应在处理后删除。

知道问题是什么吗?

4

1 回答 1

3

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

CreateMHTMLBody(const URL: WideString; Flags: CdoMHTMLFlags; 
                          const UserName: WideString; const Password: WideString); safecall;
于 2012-10-02T20:45:55.660 回答