Click here to Skip to main content
12,297,624 members (23,745 online)
Click here to Skip to main content
Add your own
alternative version


92 bookmarked

Automated IE SaveAs MHTML

, 4 Sep 2002
Rate this:
Please Sign up or sign in to vote.
This article demonstrates how to automate IE's Save As functionality


The purpose of this article is to show how to automate the fully fledged Save As HTML feature from Internet Explorer, which is normally hidden to those using the Internet Explorer API. Saving the current document as MHTML format is just one of the options available, including:

  • Save As MHTML (whole web page, images, ... in a single file)
  • Save As Full HTML (additional folder for images, ...)
  • Save HTML code only
  • Save As Text

Saving Silently as HTML Using the Internet Explorer API

In fact, the ability to save the current web page for storage without showing a single dialog box is already available to everyone under C++, using the following code, with an important restriction:

IPersistFile *lpPersistFile = NULL;

// m_ctrl is an instance of the Web Browser control
lpDispatch = m_ctrl.get_Document();
lpDispatch->QueryInterface(IID_IPersistFile, (void**)&lpPersistFile);


(caption for code above) Saving HTML code only, without dialog boxes

The restriction is that we are talking about the HTML code only, not the web page. Of course, what is interesting is to gain access to full HTML archives with images and so on.

Because there is no "public" or known way to ask for this feature without showing one or more dialog boxes from Internet Explorer, what we are going to do is hook the operating system to listen all window creations, including the dialog boxes. Then we'll ask Internet Explorer for the feature and override the file path from the dialog boxes without being seen. Finally, we'll mimic the user clicking on the Save button to validate the dialog box and unhook ourselves. That's done!

Hooking Internet Explorer to Save As HTML without popping the dialog boxes

This was the short workflow, but there are a few tricks to get along and this article is a unique opportunity to go into detail. By the way, the code is rooted by an article from MS about how to customize Internet Explorer Printing by hooking the Print dialog boxes; see here or here. In our app, we have our own Save As feature:

m_wbSaveAs.Config( CString("c:\\htmlpage.mhtml"), SAVETYPE_ARCHIVE );

// where the second parameter is the type of HTML needed :
typedef enum _SaveType
} SaveType;

We start the SaveAs() implementation by installing the hook:

// prepare SaveAs Dialog hook
g_hHook = SetWindowsHookEx(WH_CBT, CbtProc, NULL, GetCurrentThreadId());
if (!g_hHook)
    return false;

// make SaveAs Dialog appear
// cmd = OLECMDID_SAVEAS (see ./include/docobj.h)
g_bSuccess = false;
g_pWebBrowserSaveAs = this;
HRESULT hr = m_pWebBrowser->ExecWB(OLECMDID_SAVEAS, 

// remove hook
g_pWebBrowserSaveAs = NULL;
g_hHook = NULL;

The hook callback procedure is just hardcore code; see for yourself:

LRESULT CALLBACK CSaveAsWebbrowser::CbtProc(int nCode, 
    WPARAM wParam, LPARAM lParam) 
    // the windows hook sees for each new window being created :
    // - HCBT_CREATEWND : when the window is about to be created
    //      we check out if it is a dialog box (classid = 0x00008002, 
    //      see Spy++)
    //      and we hide it, likely to be the IE SaveAs dialog
    // - HCBT_ACTIVATE : when the window itself gets activited
    //      we run a separate thread, and let IE do his own init steps in 
    //      the mean time
    switch (nCode)
        case HCBT_CREATEWND:
            HWND hWnd = (HWND)wParam;
            LPCBT_CREATEWND pcbt = (LPCBT_CREATEWND)lParam;
            LPCREATESTRUCT pcs = pcbt->lpcs;
            if ((DWORD)pcs->lpszClass == 0x00008002)
                g_hWnd = hWnd;          // Get hwnd of SaveAs dialog
                pcs->x = -2 * pcs->cx;  // Move dialog off screen
        case HCBT_ACTIVATE:
            HWND hwnd = (HWND)wParam;
            if (hwnd == g_hWnd)
                g_hWnd = NULL;
                g_bSuccess = true;

                if (g_pWebBrowserSaveAs->IsSaveAsEnabled())

                    CSaveAsThread *newthread = new CSaveAsThread();
                    newthread->Config( g_pWebBrowserSaveAs->GetFilename(), 
                        g_pWebBrowserSaveAs->GetSaveAsType() );
    return CallNextHookEx(g_hHook, nCode, wParam, lParam); 

In our thread, we wait until the Internet Explorer Save As dialog is ready with filled data:

switch(    ::WaitForSingleObject( m_hComponentReadyEvent, m_WaitTime) )
     if ( ::IsWindowVisible(m_keyhwnd) )
         bSignaled = TRUE;
         bContinue = FALSE;

     MSG msg ;
     while( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) )
         if (msg.message == WM_QUIT)
              bContinue = FALSE ;
              break ;

// relaunch our SaveAs class, but now everything is ready to play with
if (bSignaled)
    CSaveAsWebbrowser surrenderNow;
    surrenderNow.Config( GetFilename(), GetSaveAsType() );
    surrenderNow.UpdateSaveAs( m_keyhwnd );

// kill the thread, we don't care anymore about it
delete this;

We can now override the appropriate data:

void CSaveAsWebbrowser::UpdateSaveAs(HWND hwnd)
    // editbox : filepath (control id = 0x047c)
    // dropdown combo : filetypes (options=complete page;
    //     archive;html only;txt) (control id = 0x0470)
    // save button : control id = 0x0001
    // cancel button : control id = 0x0002

    // select right item in the combobox
    SendMessage(GetDlgItem(hwnd, 0x0470), CB_SETCURSEL, 
        (WPARAM) m_nSaveType, 0);
    SendMessage(hwnd, WM_COMMAND, MAKEWPARAM(0x0470,CBN_CLOSEUP), 
        (LPARAM) GetDlgItem(hwnd, 0x0470));

    // set output filename
    SetWindowText(GetDlgItem(hwnd, 0x047c), m_szFilename);

    // Invoke Save button
    SendMessage(GetDlgItem(hwnd, 0x0001), BM_CLICK, 0, 0);  

In the code above, it is funny to remark that to select the kind of HTML we want (full HTML, archive, code only or text format), we not only select the adequate entry in the combo-box, we also send Internet Explorer a combo-box CloseUp notification. This is because that's what Internet Explorer has subscribed for to know we want this kind of HTML. This behavior is known by hints-and-trials.


This article describes a technique to gain access to the fully fledged Save As HTML feature exposed by Internet Explorer. I have never seen an article about this topic on the 'net, whereas it's easy to figure out that it is a compelling feature for developers building web applications. Files you may use from the source code provided are:

  • SaveAsWebBrowser.h, *.cpp: hook procedure; fill the dialog box data
  • SaveAsThread.h, *.cpp: auxiliary thread for synchronization with Internet Explorer

The application is just a simple MFC-based CHtmlView application embedding the web browser control.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Addicted to reverse engineering. At work, I am developing business intelligence software in a team of smart people (independent software vendor).

Need a fast Excel generation component? Try xlsgen.

You may also be interested in...

Comments and Discussions

GeneralRe: Save the modified page Pin
Stephane Rodriguez.20-Nov-04 9:04
memberStephane Rodriguez.20-Nov-04 9:04 
GeneralRe: Save the modified page Pin
Berkeley Wong20-Nov-04 18:48
memberBerkeley Wong20-Nov-04 18:48 
GeneralRe: Save the modified page Pin
mcbain3-May-07 10:18
membermcbain3-May-07 10:18 
Generalwin98 Pin
NorrieTaylor29-Aug-04 20:02
memberNorrieTaylor29-Aug-04 20:02 
GeneralWin98 Different DialogID Pin
TDK90000010-May-04 5:16
memberTDK90000010-May-04 5:16 
GeneralRe: Win98 Different DialogID Pin
Anonymous11-May-04 0:10
sussAnonymous11-May-04 0:10 
GeneralRe: Win98 Different DialogID Pin
Stephane Rodriguez.11-May-04 1:18
memberStephane Rodriguez.11-May-04 1:18 
GeneralRe: Win98 Different DialogID Pin
Anonymous29-Aug-04 16:57
sussAnonymous29-Aug-04 16:57 
GeneralRe: Win98 Different DialogID Pin
NorrieTaylor30-Aug-04 19:12
memberNorrieTaylor30-Aug-04 19:12 
GeneralRe: Win98 Different DialogID Pin
Stephane Rodriguez.30-Aug-04 20:36
memberStephane Rodriguez.30-Aug-04 20:36 
GeneralRe: Win98 Different DialogID Pin
NorrieTaylor31-Aug-04 18:57
memberNorrieTaylor31-Aug-04 18:57 
GeneralRe: Win98 Different DialogID Pin
NorrieTaylor31-Aug-04 20:16
memberNorrieTaylor31-Aug-04 20:16 
GeneralReference Page is missing Pin
internal27-Apr-04 17:09
memberinternal27-Apr-04 17:09 
QuestionMFC70D.dll problem..? Pin
ajay sonawane26-Apr-04 19:02
memberajay sonawane26-Apr-04 19:02 
AnswerRe: MFC70D.dll problem..? Pin
Stephane Rodriguez.26-Apr-04 19:08
memberStephane Rodriguez.26-Apr-04 19:08 
QuestionHow to save only one image ? Pin
rrrado16-Apr-04 3:56
memberrrrado16-Apr-04 3:56 
AnswerRe: How to save only one image ? Pin
Anonymous17-Apr-04 5:22
sussAnonymous17-Apr-04 5:22 
GeneralRe: How to save only one image ? Pin
rrrado18-Apr-04 21:08
memberrrrado18-Apr-04 21:08 
GeneralRe: How to save only one image ? Pin
angello3-Mar-05 12:57
memberangello3-Mar-05 12:57 
GeneralRe: How to save only one image ? Pin
twinssen14-Jan-06 13:36
membertwinssen14-Jan-06 13:36 
GeneralUsing in .Exe to get IE to save current page Pin
Neville Franks21-Mar-04 23:37
memberNeville Franks21-Mar-04 23:37 
GeneralRe: Using in .Exe to get IE to save current page Pin
Stephane Rodriguez.27-Mar-04 7:53
memberStephane Rodriguez.27-Mar-04 7:53 
QuestionCan I get the Internet shortcuts in the IE history with other methods? Pin
xieweihua23-Feb-04 21:57
memberxieweihua23-Feb-04 21:57 
AnswerRe: Can I get the Internet shortcuts in the IE history with other methods? Pin
Stephane Rodriguez.24-Feb-04 2:12
memberStephane Rodriguez.24-Feb-04 2:12 
QuestionHow to set URL of IE through program? Pin
shri_3118-Feb-04 0:44
membershri_3118-Feb-04 0:44 
AnswerRe: How to set URL of IE through program? Pin
Stephane Rodriguez.18-Feb-04 1:48
memberStephane Rodriguez.18-Feb-04 1:48 
GeneralC# code Pin
Ralf251113-Jan-04 2:07
memberRalf251113-Jan-04 2:07 
GeneralRe: C# code Pin
Stephane Rodriguez.13-Jan-04 2:30
memberStephane Rodriguez.13-Jan-04 2:30 
GeneralRe: C# code Pin
Ralf251113-Jan-04 3:42
memberRalf251113-Jan-04 3:42 
Amul6-Jan-04 3:39
memberAmul6-Jan-04 3:39 
GeneralModal Print Dialog Pin
kkaps26-Nov-03 5:18
memberkkaps26-Nov-03 5:18 
GeneralRe: Modal Print Dialog Pin
Steph Rodriguez26-Nov-03 5:42
sussSteph Rodriguez26-Nov-03 5:42 
GeneralRe: Modal Print Dialog Pin
kkaps26-Nov-03 9:25
memberkkaps26-Nov-03 9:25 
GeneralRe: Modal Print Dialog Pin
Steph Rodriguez27-Nov-03 0:02
sussSteph Rodriguez27-Nov-03 0:02 
GeneralRe: Modal Print Dialog Pin
kkaps27-Nov-03 18:46
memberkkaps27-Nov-03 18:46 
Generalwhy use thread? (Some Delphi code) Pin
kmicic17-Nov-03 18:49
memberkmicic17-Nov-03 18:49 
GeneralRe: why use thread? (Some Delphi code) Pin
Stephane Rodriguez.17-Nov-03 20:51
memberStephane Rodriguez.17-Nov-03 20:51 
GeneralA few more comments Pin
kmicic8-Dec-03 8:17
memberkmicic8-Dec-03 8:17 
GeneralError Msg: "The web page could not be saved to the selected location" Pin
banxuegang23-Aug-03 17:14
memberbanxuegang23-Aug-03 17:14 
GeneralRe: Error Msg: "The web page could not be saved to the selected location" Pin
Stephane Rodriguez.23-Aug-03 20:15
memberStephane Rodriguez.23-Aug-03 20:15 
GeneralRe: Error Msg: "The web page could not be saved to the selected location" Pin
Anonymous24-Aug-03 17:35
sussAnonymous24-Aug-03 17:35 
GeneralRe: Error Msg: "The web page could not be saved to the selected location" Pin
Nelis Willers11-Oct-03 10:29
memberNelis Willers11-Oct-03 10:29 
GeneralRe: Error Msg: "The web page could not be saved to the selected location" Pin
Stephane Rodriguez.11-Oct-03 10:50
memberStephane Rodriguez.11-Oct-03 10:50 
GeneralRe: Error Msg: "The web page could not be saved to the selected location" Pin
exe2mht29-Mar-04 20:32
sussexe2mht29-Mar-04 20:32 
Questionurl2mht or some c+ code? Pin
docksudbbs20-Jun-03 7:10
memberdocksudbbs20-Jun-03 7:10 
QuestionHow to change the output directory? Pin
hhdsq29-May-03 10:45
memberhhdsq29-May-03 10:45 
QuestionConvert to COM model? Pin
docksudbbs27-May-03 12:38
memberdocksudbbs27-May-03 12:38 
AnswerRe: Convert to COM model? Pin
Anonymous27-May-03 19:41
sussAnonymous27-May-03 19:41 
GeneralIt is not Working in Win98 Pin
rashvi9-May-03 2:43
memberrashvi9-May-03 2:43 
GeneralRe: It is not Working in Win98 Pin
.S.Rod.9-May-03 3:50
member.S.Rod.9-May-03 3:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160525.2 | Last Updated 5 Sep 2002
Article Copyright 2002 by Stephane Rodriguez.
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid