We help IT Professionals succeed at work.

save a webpage to file then reload from file.

Oli999
Oli999 used Ask the Experts™
on
Spreadsheet with a webbrowser on it. Need to navigate to a web page, save it to a file locally then reload it from the file later.

This is my code:-

Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
"URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal _
szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long

Sub SaveButton_Click()
  Dim returnvalue As Long
  dim url,filename as string
 
  url = Sheet1.WebBrowser1.LocationURL
  filename = "c:\testfile.htm"
  returnvalue = URLDownloadToFile(0, url, filename, 0, 0)
End Sub

Sub LoadButton_Click()
  Sheet1.WebBrowser1.Navigate2 ("c:\testfile.htm")
End Sub

When running the Save Sub it seems to work ok and saves the file however when click to re-load from file it takes about 30-40 seconds to load. It loads most of it then freezes for ages then finally loads the last little bit. Strangely it only does this when the computer is connected to the internet. If the internet is disconnected then it re-loads the page instantly without any problems. This isn't really practical though as I need to re-load the page instantly without having to disconnect or wait for ages every time.

Tried loading the saved file into IE when online it brings up warnings about active x and scripts running, I clicked to allow content and it eventually loads after about 30 seconds.

Any ideas on how to solve this would be greatly appreciated.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Expert of the Quarter 2010
Expert of the Year 2010

Commented:
That is because URLDownloadToFileA only saves the .htm file.  Not all the linked pictures, javascript files etc.

If you open up the saved .htm file, it is going off and re-retrieving all the linked files to show you in the WebBrowser control.

A pretty extensive research on the internet doesn't reveal any way to perform a "Save as (complete)" equivalent using Web Browser, nor urlmon.  The only way I can think of would be to manually parse the document using MSXML and retrieve all the linked images, js, css etc.

Author

Commented:
The thing is that it loads perfectly ok when the internet is disconnected. It's just very slow when online.
Expert of the Quarter 2010
Expert of the Year 2010
Commented:
If it does the same thing when you open the saved file in IE, then it is not doing anything different.
When in offline mode /disconnected, it times out immediately when trying to load external js/css/images etc, so the base text of the page loads immediately.

Author

Commented:
Looks like it's not quite possible to do what I want. Thanks for the help anyway.