Skip to content

Instantly share code, notes, and snippets.

@ghotz
Created July 29, 2014 10:02
Show Gist options
  • Save ghotz/98003e2e650f878abcc4 to your computer and use it in GitHub Desktop.
Save ghotz/98003e2e650f878abcc4 to your computer and use it in GitHub Desktop.
Save web page in MHTML format with old technique using CDO and ADODB objects
#
# Save to MHTML file
# Adapted from http://stackoverflow.com/questions/995339/saving-webpage-as-mhtm-file
#
# COM SaveOptionsEnum
# Const adSaveCreateNotExist = 1
# Const adSaveCreateOverWrite = 2
#
# COM StreamTypeEnum
# Const adTypeBinary = 1
# Const adTypeText = 2
$SourceURL = "http://www.ghotz.com";
$DestinationFile = "D:\Temp\test.mhtml";
$CDOMessage = New-Object -ComObject CDO.Message;
$ADODBStream = New-Object -ComObject ADODB.Stream;
$ADODBStream.Type = 2;
$ADODBStream.Charset = "US-ASCII";
$ADODBStream.Open();
$CDOMessage.CreateMHTMLBody($SourceURL, 0);
$CDOMessage.DataSource.SaveToObject($ADODBStream, "_Stream");
$ADODBStream.SaveToFile($DestinationFile, 2);
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($CDOMessage);
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ADODBStream);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment