Jump to content


Photo

[FUNCTION] Create a MHTML (.mht) from a given URL


  • Please log in to reply
12 replies to this topic

#1 ABCza

ABCza
  • Members
  • 124 posts

Posted 07 February 2012 - 09:49 PM

Hola. This function creates a .mht from the given URL. AutoHotkey Basic, requires Sean's COM Standard Library: <!-- m -->http://www.autohotke...pic.php?t=22923<!-- m -->

There are some encoding problems. If anyone skilled would like to help, it'll be awesome.

/*
--------------------------------------------------------------------------------------------------------------------------------
FUNCTION: Url2Mhtml
--------------------------------------------------------------------------------------------------------------------------------
Creates a MHTML file from the given URL.
Requires Sean's COM Standard Library:  http://www.autohotkey.com/forum/viewtopic.php?t=22923

PARAMETERS:
~~~~~~~~~~~
URL         - Working URL.
destPath    - Destination file path (eg. C:\Users\XYZ\Desktop\file.mht)
flags       - MHTML flags, it can be the OR of the followings (leave it empty to suppress nothing):
              CdoSuppressImages         := 1
              CdoSuppressBGSounds       := 2
              CdoSuppressFrames         := 4
              CdoSuppressObjects        := 8
              CdoSuppressStyleSheets    := 16
              CdoSuppressAll            := 31
--------------------------------------------------------------------------------------------------------------------------------
COM INFO:
--------------------------------------------------------------------------------------------------------------------------------
IMessage Interface                                              - http://msdn.microsoft.com/en-us/library/ms526453(v=vs.85).aspx
CreateMHTMLBody Method                                          - http://msdn.microsoft.com/en-us/library/ms527024(v=vs.85).aspx
CdoMHTMLFlags Enum                                              - http://msdn.microsoft.com/en-us/library/ms526977(v=vs.85).aspx
GetStream Method                                                - http://msdn.microsoft.com/en-us/library/ms527348(v=vs.85).aspx
SaveToFile Method                                               - http://msdn.microsoft.com/en-us/library/ms676152(v=vs.85).aspx
SaveOptionsEnum                                                 - http://msdn.microsoft.com/en-us/library/ms676152(v=vs.85).aspx
--------------------------------------------------------------------------------------------------------------------------------
*/
Url2Mhtml(URL, destPath=0, flags=0) {
    ; Initializes COM and creates the CDO.IMessage object
    COM_Init()
    comObj := COM_CreateObject("{CD000001-8B95-11D1-82DB-00C04FB1625D}") ; IMessage Interface

    ; Parses the URL and creates the MHTML
    COM_Invoke( comObj
                , "CreateMHTMLBody"
                , URL
                , flags )
                
    ;, Obtains the ADODB.Stream object
    stream := COM_Invoke( comObj, "GetStream" )
    
    ; Saves the stream creating the .mht file
    COM_Invoke( stream
                , "SaveToFile"
                , (destPath) ? destPath : A_WorkingDir . "\url.mht"
                , adSaveCreateOverWrite:=2 )

    ; Releases the object and terminates COM
    COM_Release(comObj)
    COM_Term()
}

Usage example:
; Save the URL to a .mht file into the working directory
Url2Mhtml("http://www.hwupgrade.it")

; Save the URL to a .mht file into the given directory, suppressing images, background sounds and frames
Url2Mhtml("http://www.howtogeek.com", "C:\Users\XYZ\Desktop\rotfl.mht", 1|2|4)


#2 fincs

fincs
  • Fellows
  • 1532 posts

Posted 07 February 2012 - 10:15 PM

For us with newer AutoHotkey versions (untested):
Url2Mhtml(URL, destPath=0, flags=0)
{
	comObj := ComObjCreate("{CD000001-8B95-11D1-82DB-00C04FB1625D}")
	comObj.CreateMHTMLBody(URL, flags)
	comObj.GetStream().SaveToFile((destPath) ? destPath : A_WorkingDir . "\url.mht", 2) ; adSaveCreateOverWrite:=2
}


#3 ABCza

ABCza
  • Members
  • 124 posts

Posted 07 February 2012 - 10:36 PM

Thank you fincs :D

#4 DataLife

DataLife
  • Members
  • 745 posts

Posted 28 April 2012 - 03:25 PM

For us with newer AutoHotkey versions (untested):

Url2Mhtml(URL, destPath=0, flags=0)
{
	comObj := ComObjCreate("{CD000001-8B95-11D1-82DB-00C04FB1625D}")
	comObj.CreateMHTMLBody(URL, flags)
	comObj.GetStream().SaveToFile((destPath) ? destPath : A_WorkingDir . "\url.mht", 2) ; adSaveCreateOverWrite:=2
}

fincs

This is exactly what I am looking for.

I am using Autohotkey_L com to log into a webpage and click on a link.

Then I need to get the .mht from that page.

I am sure it has something to do with using the current pointer to the Web Browser object instead of creating a new one.

Can you help me?

thanks
DataLife

#5 fincs

fincs
  • Fellows
  • 1532 posts

Posted 28 April 2012 - 03:31 PM

Well, the only thing I did was to convert the OP's code to AutoHotkey_L; I have no idea how the underlying API the COM objects use works. You're better off asking in the "Support" forums or joining the IRC channel.

#6 azure

azure
  • Members
  • 1203 posts

Posted 28 April 2012 - 06:37 PM

For us with newer AutoHotkey versions (untested):

Url2Mhtml(URL, destPath=0, flags=0)
{
	comObj := ComObjCreate("{CD000001-8B95-11D1-82DB-00C04FB1625D}")
	comObj.CreateMHTMLBody(URL, flags)
	comObj.GetStream().SaveToFile((destPath) ? destPath : A_WorkingDir . "\url.mht", 2) ; adSaveCreateOverWrite:=2
}


for me with Autohotkey_L, this is only what I need? any documentation about the flags??

#7 ABCza

ABCza
  • Members
  • 124 posts

Posted 28 April 2012 - 10:41 PM

For us with newer AutoHotkey versions (untested):

Url2Mhtml(URL, destPath=0, flags=0)
{
	comObj := ComObjCreate("{CD000001-8B95-11D1-82DB-00C04FB1625D}")
	comObj.CreateMHTMLBody(URL, flags)
	comObj.GetStream().SaveToFile((destPath) ? destPath : A_WorkingDir . "\url.mht", 2) ; adSaveCreateOverWrite:=2
}


for me with Autohotkey_L, this is only what I need? any documentation about the flags??


Read my code, it's fullly documented :)

#8 DataLife

DataLife
  • Members
  • 745 posts

Posted 28 April 2012 - 10:48 PM

For us with newer AutoHotkey versions (untested):

Url2Mhtml(URL, destPath=0, flags=0)
{
	comObj := ComObjCreate("{CD000001-8B95-11D1-82DB-00C04FB1625D}")
	comObj.CreateMHTMLBody(URL, flags)
	comObj.GetStream().SaveToFile((destPath) ? destPath : A_WorkingDir . "\url.mht", 2) ; adSaveCreateOverWrite:=2
}


for me with Autohotkey_L, this is only what I need? any documentation about the flags??


Read my code, it's fullly documented :)

I am using Autohotkey_L and I do not know how to translate it from basic to _L.

#9 ABCza

ABCza
  • Members
  • 124 posts

Posted 28 April 2012 - 10:55 PM

I am using Autohotkey_L and I do not know how to translate it from basic to _L.


I'm referring to the COM Objects and the flags. In my snippet there are various MSDN links.
However this is complex stuff, you need to dig deeper and understand at least how COM is bound to the various AutoHotkey versions.

#10 xoomaster

xoomaster
  • Members
  • 6 posts

Posted 20 June 2012 - 04:12 AM

I know this is an old post but I have tried searching for a method to download a text file from a URL with no success. Can this Function be changed to download a URL as a TEXT file instead of MHTML ? Somehow URLDownloadToFile is blocked on my server at work, but the above function works well.

Your help is appriciated.

Xoomaster

#11 azure

azure
  • Members
  • 1203 posts

Posted 18 July 2012 - 12:12 AM

an example please?

#12 Guests

  • Guests

Posted 18 July 2012 - 06:46 AM

@azure: see first post, two examples :D

#13 azure

azure
  • Members
  • 1203 posts

Posted 18 July 2012 - 11:40 AM

thanks!

as for downloading a list of urls from a txt file, do you suggest a reading/parsing loop? or any better method?

if there is a problem with the download, it was not complete, it failed, etc, how can we know? esp. when we parse a big url list