Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How To Programmatically Save an HTML Page to Disk


View products that this article applies to.

Summary

This article demonstrates how to programmatically save to disk an HTML page that has been loaded into Internet Explorer without prompting the user.

↑ Back to the top


More information

As a Web browser control host, or an application that is automating Internet Explorer, you may find it useful to be able to programmatically save the currently loaded document to disk without user intervention. The following code samples illustrate how to accomplish this in both Visual C++ and Visual Basic. While the Visual Basic solution also works in Visual C++, you cannot implement the Visual C++ solution in Visual Basic.

Visual Basic Solution

You can use the Internet Explorer Document Object Model to capture all of the HTML sources into a string variable. The string variable can then be written out to a text file and saved to disk.
    Dim hElm As IHTMLElement
    Dim htmltext As String
    Dim fso As Object
    Dim file As Object
        
    Set hElm = WebBrowser1.Document.all.tags("html").Item(0)
    htmltext = hElm.outerHTML

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set file = fso.OpenTextFile("c:\test.htm", 2, True)
    file.write htmltext
    file.Close

    Set file = Nothing
    Set fso = Nothing
				

Visual C++ Solution

Accomplishing this task from a Visual C++ host is very straightforward. You can use an IWebBrowser2 interface to call the QueryInterface method for the IHTMLDocument2 interface. After you obtain a pointer to the document, then call QueryInterface for the IPersistFile interface. After you obtain this interface pointer, you can call the save method to save the file to disk.
    HRESULT          hr    = E_FAIL;
    IDispatch*       pDisp = NULL;
    IHTMLDocument2*  pDoc  = NULL;
	
    pDisp                  = m_webOC.GetDocument();

   if(SUCCEEDED(hr = pDisp->QueryInterface(IID_IHTMLDocument2,(void**)&pDoc)))
   {
       IPersistFile*	pFile	=	NULL;
       if(SUCCEEDED(pDoc->QueryInterface(IID_IPersistFile,(void**)&pFile)))
       {
	LPCOLESTR	file = L"c:\\test1.htm";
	pFile->Save(file,TRUE);
       }
   }
				

↑ Back to the top


References

For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:
206891 PRB: Value of outerHTML Does Not Match Original File
244757 How To Download a File Without Prompting
271868 BUG: IPersistStreamInit Not Available for a FRAME in a FRAMESET
For more information about the IPersistFile interface, see the following MSDN Web site: For more information about developing Web-based solutions for Microsoft Internet Explorer, visit the following Microsoft Web sites:

↑ Back to the top


Keywords: KB292485, kbieobj, kbhowto

↑ Back to the top

Article Info
Article ID : 292485
Revision : 4
Created on : 10/24/2012
Published on : 10/24/2012
Exists online : False
Views : 425