首页  编辑  

保存HTML为MHT文档

Tags: /超级猛料/OS.操作系统/IE.扩展/   Date Created:

将网页保存为mht文件~~

如果使用CDO.Message转换HTML为MHT,在转换本地文件的时候,如果无法删除原HTML文件,那么请在HTML文件名前面添加 file:/// 前缀。

procedure HTMLToMHT(mURL, DestFile: string);

var

   vCDOMessage:   OleVariant;

   Strs: TStringList;

   Stream: _Stream;

begin

   vCDOMessage   :=   CreateOleObject('CDO.Message');

   vCDOMessage.Configuration   :=   CreateOleObject('CDO.Configuration');

   vCDOMessage.MimeFormatted := True;

   try

       vCDOMessage.CreateMHTMLBody(mURL, 0, 'domain\username','password');

       vCDOMessage.GetStream.SaveToFile(DestFile, 2);

 except

     raise;

 end;

end;

----------------------------------------

(*//

标题:将网页保存为mht文件

说明:接口只定义需要使用的部分,如要研究请参考其他文档

设计:Zswang

支持:wjhu111@21cn.com

日期:2004-01-05

//*)

///////Begin Source

uses ComObj;

///////Begin 接口定义

// Constants for enum SaveOptionsEnum

type

 SaveOptionsEnum = TOleEnum;

const

 adSaveCreateNotExist = $00000001;

 adSaveCreateOverWrite = $00000002;

// Constants for enum CdoMHTMLFlags

type

 CdoMHTMLFlags = TOleEnum;

const

 cdoSuppressNone = $00000000;

 cdoSuppressImages = $00000001;

 cdoSuppressBGSounds = $00000002;

 cdoSuppressFrames = $00000004;

 cdoSuppressObjects = $00000008;

 cdoSuppressStyleSheets = $00000010;

 cdoSuppressAll = $0000001F;

// Constants for enum StreamTypeEnum

type

 StreamTypeEnum = TOleEnum;

const

 adTypeBinary = $00000001;

 adTypeText = $00000002;

// Constants for enum LineSeparatorEnum

type

 LineSeparatorEnum = TOleEnum;

const

 adLF = $0000000A;

 adCR = $0000000D;

 adCRLF = $FFFFFFFF;

// Constants for enum ObjectStateEnum

type

 ObjectStateEnum = TOleEnum;

const

 adStateClosed = $00000000;

 adStateOpen = $00000001;

 adStateConnecting = $00000002;

 adStateExecuting = $00000004;

 adStateFetching = $00000008;

// Constants for enum ConnectModeEnum

type

 ConnectModeEnum = TOleEnum;

const

 adModeUnknown = $00000000;

 adModeRead = $00000001;

 adModeWrite = $00000002;

 adModeReadWrite = $00000003;

 adModeShareDenyRead = $00000004;

 adModeShareDenyWrite = $00000008;

 adModeShareExclusive = $0000000C;

 adModeShareDenyNone = $00000010;

 adModeRecursive = $00400000;

// Constants for enum StreamOpenOptionsEnum

type

 StreamOpenOptionsEnum = TOleEnum;

const

 adOpenStreamUnspecified = $FFFFFFFF;

 adOpenStreamAsync = $00000001;

 adOpenStreamFromRecord = $00000004;

// Constants for enum StreamWriteEnum

type

 StreamWriteEnum = TOleEnum;

const

 adWriteChar = $00000000;

 adWriteLine = $00000001;

 stWriteChar = $00000000;

 stWriteLine = $00000001;

// Constants for enum CdoDSNOptions

type

 CdoDSNOptions = TOleEnum;

const

 cdoDSNDefault = $00000000;

 cdoDSNNever = $00000001;

 cdoDSNFailure = $00000002;

 cdoDSNSuccess = $00000004;

 cdoDSNDelay = $00000008;

 cdoDSNSuccessFailOrDelay = $0000000E;

// Constants for enum CdoReferenceType

type

 CdoReferenceType = TOleEnum;

const

 cdoRefTypeId = $00000000;

 cdoRefTypeLocation = $00000001;

type

 IBodyPart = interface(IDispatch)

   ['{CD000021-8B95-11D1-82DB-00C04FB1625D}']

 end;

 IBodyParts = interface(IDispatch)

   ['{CD000023-8B95-11D1-82DB-00C04FB1625D}']

 end;

 _Collection = interface(IDispatch)

   ['{00000512-0000-0010-8000-00AA006D2EA4}']

 end;

 Fields15 = interface(_Collection)

   ['{00000506-0000-0010-8000-00AA006D2EA4}']

 end;

 Fields20 = interface(Fields15)

   ['{0000054D-0000-0010-8000-00AA006D2EA4}']

 end;

 Fields = interface(Fields20)

   ['{00000564-0000-0010-8000-00AA006D2EA4}']

 end;

 IDataSource = interface(IDispatch)

   ['{CD000029-8B95-11D1-82DB-00C04FB1625D}']

 end;

type

 _Stream = interface(IDispatch)

   ['{00000565-0000-0010-8000-00AA006D2EA4}']

   function  Get_Size: Integer; safecall;

   function  Get_EOS: WordBool; safecall;

   function  Get_Position: Integer; safecall;

   procedure Set_Position(pPos: Integer); safecall;

   function  Get_Type_: StreamTypeEnum; safecall;

   procedure Set_Type_(ptype: StreamTypeEnum); safecall;

   function  Get_LineSeparator: LineSeparatorEnum; safecall;

   procedure Set_LineSeparator(pLS: LineSeparatorEnum); safecall;

   function  Get_State: ObjectStateEnum; safecall;

   function  Get_Mode: ConnectModeEnum; safecall;

   procedure Set_Mode(pMode: ConnectModeEnum); safecall;

   function  Get_Charset: WideString; safecall;

   procedure Set_Charset(const pbstrCharset: WideString); safecall;

   function  Read(NumBytes: Integer): OleVariant; safecall;

   procedure Open(Source: OleVariant; Mode: ConnectModeEnum; Options: StreamOpenOptionsEnum;

                  const UserName: WideString; const Password: WideString); safecall;

   procedure Close; safecall;

   procedure SkipLine; safecall;

   procedure Write(Buffer: OleVariant); safecall;

   procedure SetEOS; safecall;

   procedure CopyTo(const DestStream: _Stream; CharNumber: Integer); safecall;

   procedure Flush; safecall;

   procedure SaveToFile(const FileName: WideString; Options: SaveOptionsEnum); safecall;

 end;

 IConfiguration = interface(IDispatch)

   ['{CD000022-8B95-11D1-82DB-00C04FB1625D}']

 end;

 IMessage = interface(IDispatch)

   ['{CD000020-8B95-11D1-82DB-00C04FB1625D}']

   function  Get_BCC: WideString; safecall;

   procedure Set_BCC(const pBCC: WideString); safecall;

   function  Get_CC: WideString; safecall;

   procedure Set_CC(const pCC: WideString); safecall;

   function  Get_FollowUpTo: WideString; safecall;

   procedure Set_FollowUpTo(const pFollowUpTo: WideString); safecall;

   function  Get_From: WideString; safecall;

   procedure Set_From(const pFrom: WideString); safecall;

   function  Get_Keywords: WideString; safecall;

   procedure Set_Keywords(const pKeywords: WideString); safecall;

   function  Get_MimeFormatted: WordBool; safecall;

   procedure Set_MimeFormatted(pMimeFormatted: WordBool); safecall;

   function  Get_Newsgroups: WideString; safecall;

   procedure Set_Newsgroups(const pNewsgroups: WideString); safecall;

   function  Get_Organization: WideString; safecall;

   procedure Set_Organization(const pOrganization: WideString); safecall;

   function  Get_ReceivedTime: TDateTime; safecall;

   function  Get_ReplyTo: WideString; safecall;

   procedure Set_ReplyTo(const pReplyTo: WideString); safecall;

   function  Get_DSNOptions: CdoDSNOptions; safecall;

   procedure Set_DSNOptions(pDSNOptions: CdoDSNOptions); safecall;

   function  Get_SentOn: TDateTime; safecall;

   function  Get_Subject: WideString; safecall;

   procedure Set_Subject(const pSubject: WideString); safecall;

   function  Get_To_: WideString; safecall;

   procedure Set_To_(const pTo: WideString); safecall;

   function  Get_TextBody: WideString; safecall;

   procedure Set_TextBody(const pTextBody: WideString); safecall;

   function  Get_HTMLBody: WideString; safecall;

   procedure Set_HTMLBody(const pHTMLBody: WideString); safecall;

   function  Get_Attachments: IBodyParts; safecall;

   function  Get_Sender: WideString; safecall;

   procedure Set_Sender(const pSender: WideString); safecall;

   function  Get_Configuration: IConfiguration; safecall;

   procedure _Set_Configuration(const pConfiguration: IConfiguration); safecall;

   procedure Set_Configuration(const pConfiguration: IConfiguration); safecall;

   function  Get_AutoGenerateTextBody: WordBool; safecall;

   procedure Set_AutoGenerateTextBody(pAutoGenerateTextBody: WordBool); safecall;

   function  Get_EnvelopeFields: Fields; safecall;

   function  Get_TextBodyPart: IBodyPart; safecall;

   function  Get_HTMLBodyPart: IBodyPart; safecall;

   function  Get_BodyPart: IBodyPart; safecall;

   function  Get_DataSource: IDataSource; safecall;

   function  Get_Fields: Fields; safecall;

   function  Get_MDNRequested: WordBool; safecall;

   procedure Set_MDNRequested(pMDNRequested: WordBool); safecall;

   function  AddRelatedBodyPart(const URL: WideString; const Reference: WideString;

                                ReferenceType: CdoReferenceType; const UserName: WideString;

                                const Password: WideString): IBodyPart; safecall;

   function  AddAttachment(const URL: WideString; const UserName: WideString;

                           const Password: WideString): IBodyPart; safecall;

   procedure CreateMHTMLBody(const URL: WideString; Flags: CdoMHTMLFlags;

                             const UserName: WideString; const Password: WideString); safecall;

   function  Forward: IMessage; safecall;

   procedure Post; safecall;

   function  PostReply: IMessage; safecall;

   function  Reply: IMessage; safecall;

   function  ReplyAll: IMessage; safecall;

   procedure Send; safecall;

   function  GetStream: _Stream; safecall;

   function  GetInterface(const Interface_: WideString): IDispatch; safecall;

   property BCC: WideString read Get_BCC write Set_BCC;

   property CC: WideString read Get_CC write Set_CC;

   property FollowUpTo: WideString read Get_FollowUpTo write Set_FollowUpTo;

   property From: WideString read Get_From write Set_From;

   property Keywords: WideString read Get_Keywords write Set_Keywords;

   property MimeFormatted: WordBool read Get_MimeFormatted write Set_MimeFormatted;

   property Newsgroups: WideString read Get_Newsgroups write Set_Newsgroups;

   property Organization: WideString read Get_Organization write Set_Organization;

   property ReceivedTime: TDateTime read Get_ReceivedTime;

   property ReplyTo: WideString read Get_ReplyTo write Set_ReplyTo;

   property DSNOptions: CdoDSNOptions read Get_DSNOptions write Set_DSNOptions;

   property SentOn: TDateTime read Get_SentOn;

   property Subject: WideString read Get_Subject write Set_Subject;

   property To_: WideString read Get_To_ write Set_To_;

   property TextBody: WideString read Get_TextBody write Set_TextBody;

   property HTMLBody: WideString read Get_HTMLBody write Set_HTMLBody;

   property Attachments: IBodyParts read Get_Attachments;

   property Sender: WideString read Get_Sender write Set_Sender;

   property Configuration: IConfiguration read Get_Configuration write _Set_Configuration;

   property AutoGenerateTextBody: WordBool read Get_AutoGenerateTextBody write Set_AutoGenerateTextBody;

   property EnvelopeFields: Fields read Get_EnvelopeFields;

   property TextBodyPart: IBodyPart read Get_TextBodyPart;

   property HTMLBodyPart: IBodyPart read Get_HTMLBodyPart;

   property BodyPart: IBodyPart read Get_BodyPart;

   property DataSource: IDataSource read Get_DataSource;

   property Fields: Fields read Get_Fields;

   property MDNRequested: WordBool read Get_MDNRequested write Set_MDNRequested;

 end;

const

 CLASS_Message: TGUID = '{CD000001-8B95-11D1-82DB-00C04FB1625D}';

 CLASS_Configuration: TGUID = '{CD000002-8B95-11D1-82DB-00C04FB1625D}';

///////End 接口定义

procedure SaveWholePage(mURL: string; mFileName: TFileName);

var

 vMessage: IMessage;

 vConfiguration: IConfiguration;

 vStream: _Stream;

begin

 vMessage := CreateComObject(CLASS_Message) as IMessage;

 vConfiguration := CreateComObject(CLASS_Configuration) as IConfiguration;

 try

   vMessage.Configuration := vConfiguration;

   vMessage.CreateMHTMLBody(mURL, cdoSuppressAll, '', '');

   vStream := vMessage.GetStream;

   vStream.SaveToFile(mFileName, adSaveCreateOverWrite);

 finally

   vMessage := nil;

   vConfiguration := nil;

   vStream := nil;

 end;

end;

///////End Source

//Example

procedure TForm1.Button1Click(Sender: TObject);

begin

 SaveWholePage('http://www.eping.net/fourm/', 'c:\temp\temp.mht');

end;