Microsoft KB Archive/292485

= How To Programmatically Save an HTML Page to Disk =

Article ID: 292485

Article Last Modified on 5/11/2006

-

APPLIES TO


 * Microsoft Internet Explorer 4.0 128-Bit Edition
 * Microsoft Internet Explorer 4.01 Service Pack 2
 * Microsoft Internet Explorer 4.01 Service Pack 1
 * Microsoft Internet Explorer 4.01 Service Pack 2
 * Microsoft Internet Explorer 5.0
 * Microsoft Internet Explorer 5.01
 * Microsoft Internet Explorer (Programming) 5.01 SP1
 * Microsoft Internet Explorer 5.5
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Enterprise Edition

-



This article was previously published under Q292485



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



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(&quot;html&quot;).Item(0) htmltext = hElm.outerHTML

Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;) Set file = fso.OpenTextFile(&quot;c:\test.htm&quot;, 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&quot;c:\\test1.htm&quot;; pFile->Save(file,TRUE); }  }

