Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Using Com to create a MHTML (.mht) file with AHK_L


  • Please log in to reply
3 replies to this topic
DataLife
  • Members
  • 1022 posts
  • Last active: Nov 27 2015 01:09 AM
  • Joined: 27 Apr 2008
ABCza created the following code to create the MHTML (.mht) file from a given URL using Autohotkey basic and COM. Original thread here http://www.autohotke...ard/topic/76806
/*
--------------------------------------------------------------------------------------------------------------------------------
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()
}

Then fincs rewrote it for AutoHotkey_L. Original thread here http://www.autohotke...ard/topic/76806

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
}

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. When I try fincs code above I get a page not found URL.mht file.

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

Anyone know how to modify fincs code above to work with an already existing web browser object?

Here is my code. I want to replace Send {ALTDown}{f}{CtrlUp} with fincs function above.
;First I need to log in to MyPrivateURL
URL = MyPrivateURL
MyPrivateURLWindowTitle
WB := ComObjCreate("InternetExplorer.Application")
WB.Visible := true
WB.Navigate( URL )
While WB.Busy
Sleep, 100
For WB in ComObjCreate("Shell.Application").Windows
If InStr(WB.LocationName, MyPrivateURLWindowTitle )
break
iniread,PortalUsername,config.ini,credentials,PortalUsername
iniread,PortalPassword,config.ini,credentials,PortalPassword
WB.document.all.Login.value := PortalUsername
WB.document.all.Password.value := PortalPassword
WB.document.all.80.click()
While WB.Busy
Sleep, 100

;I can not navigate to the following URL unless I sign in first (above code)
Run https://%NextPrivateURL%
While WB.Busy
Sleep, 100

;After I load the "NextPrivateURL" I want to be able to create the .mht file
;Currently I am using this
Send {ALTDown}{f}{CtrlUp}
sleep 500
Send a
WinWait,Save Webpage,, 10
sleep 1000
Send, %A_ScriptDir%\URL.mht
sleep 1000
Send {enter}
;I really would like to replace the Send {ALTDown}{f}{CtrlUp} with more reliable code.

;I tried this
URL = MyPrivateURL
URL2Mhtml(URL, destPath= 0, flags=0) ;This creates page not found URL.mht file.
return
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
}




Any help would be appreciated
thanks
DataLife
Check out my scripts.  (MyIpChanger) (XPSnap) (SavePictureAs) All my scripts are tested on Windows 7, AutoHotkey 32 bit Ansi unless otherwise stated.

DataLife
  • Members
  • 1022 posts
  • Last active: Nov 27 2015 01:09 AM
  • Joined: 27 Apr 2008
Bump
Check out my scripts.  (MyIpChanger) (XPSnap) (SavePictureAs) All my scripts are tested on Windows 7, AutoHotkey 32 bit Ansi unless otherwise stated.

CodeKiller
  • Members
  • 2067 posts
  • Last active: Feb 26 2016 09:30 AM
  • Joined: 10 Jul 2008
Works perfectly... I just change the "A_WorkingDir" with "A_ScriptDir" (I still don't know why using A_WorkingDir, this var is totaly useless if you don't redefine it...).

Url2Mhtml("http://www.google.com")

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


DataLife
  • Members
  • 1022 posts
  • Last active: Nov 27 2015 01:09 AM
  • Joined: 27 Apr 2008

Works perfectly... I just change the "A_WorkingDir" with "A_ScriptDir" (I still don't know why using A_WorkingDir, this var is totaly useless if you don't redefine it...).

Url2Mhtml("http://www.google.com")

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

Can you give me an example of logging into a website then using this function?
Check out my scripts.  (MyIpChanger) (XPSnap) (SavePictureAs) All my scripts are tested on Windows 7, AutoHotkey 32 bit Ansi unless otherwise stated.