PaulSadowski.com 

PaulSadowski.com
MHT Web Archive
This simple piece of code illustrates how to use CDO to create
a 'Web Archive' (MHT) file from a web page. It needs little explanation.

The variable URL should point to the page you want to save to disk.
The variable DiskFile  is a string that should point to the place on your
disk where you want the MHT file created. This should be the full
path\filename. If you use just the file name then it will be saved in
the current working directory.

If you change the last line of the subroutine from
  Strm.SaveToFile Fn, adSaveCreateOverWrite
to
  Strm.SaveToFile Fn, adSaveCreateNotExist
then an error will be thrown if the MHT file already exists.

'MakeMHT.vbs

Const adSaveCreateNotExist = 1
Const adSaveCreateOverWrite = 2
Const adTypeBinary = 1 'Binary data
Const adTypeText = 2 '(Default) Text data

URL = "http://www.paulsadowski.com/"
DiskFile = "C:\temp\test.mht"

Set objMessage = CreateObject("CDO.Message")
objMessage.CreateMHTMLBody URL
SaveToFile objMessage, DiskFile

Sub SaveToFile(Msg, Fn)
Dim Strm, Dsk

Set Strm = CreateObject("ADODB.Stream")
Strm.Type = adTypeText
Strm.Charset = "US-ASCII"
Strm.Open
Set Dsk = Msg.DataSource
Dsk.SaveToObject Strm, "_Stream"
Strm.SaveToFile Fn, adSaveCreateOverWrite

End Sub

© 2005 by Paul R. Sadowski 
All Rights Reserved. Used By Permission.
Comments to: scripting@paulsadowski.com