VBA - Saving a web page locally.

BratFire

Board Regular
Joined
Aug 2, 2005
Messages
99
Code:
Dim IE As Object 
Set IE = CreateObject("InternetExplorer.Application") 
IE.Visible = True 
IE.navigate "http://www.MrExcel.com" 
Do 
If IE.readyState = 4 Then 
IE.Visible = False 
Exit Do 
Else 
DoEvents 
End If 
Loop 
MsgBox "Done" 
IE.Visible = True
Using the above code to open a web page, is there a way to 'save as' the web page to a local drive using VB code?
 
To work the code above must be used as posted or it may fail!

This next code, below. Is an API to paste a jpg of the Web Page to Sheet2 of the Active workbook!

It may be better for some sites?

All the posted code above and here must be run from a Standard Module, like: Module1!




Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal _
bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Const VK_KEYUP = &H2
Private Const VK_SNAPSHOT = &H2C
Private Const VK_MENU = &H12
Dim ws As Worksheet

Declare Function apiShowWindow Lib "user32" Alias "ShowWindow" _
(ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

Global Const SW_MAXIMIZE = 3
Global Const SW_SHOWNORMAL = 1
Global Const SW_SHOWMINIMIZED = 2

Private Sub Print_Screen()

keybd_event VK_MENU, 0, 0, 0
DoEvents

keybd_event VK_SNAPSHOT, 0, 0, 0
DoEvents

keybd_event VK_SNAPSHOT, 0, VK_KEYUP, 0
DoEvents

keybd_event VK_MENU, 0, VK_KEYUP, 0
DoEvents

ws.Paste Destination:=ws.Range("A1")
DoEvents
End Sub

Sub pasteWebPCopy()
Dim IE As Object

Set ws = ActiveSheet

Const strURL As String = "http://www.MrExcel.Com"

Set IE = CreateObject("InternetExplorer.Application")

IE.Visible = True
IE.Navigate strURL
apiShowWindow IE.hwnd, SW_MAXIMIZE

Do While IE.ReadyState <> 4
DoEvents
Loop

Print_Screen
IE.Visible = False
End Sub


'Note: You may be able to add code to take the picture of the web page and save it as it's own file?
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
I think the OP wants to save a complete HTML page with all relative objects. The identical action a user takes when choosing SaveAs from IE's menu and saving as HTML Complete.

I searched the web as well. Found some examples using C but I don't know C. This code will bring up the SaveAs dialog but that still does not automate the process. I'll work on an API solution because this may come in handy in the future.

Code:
Sub Example()
    Dim Url As String, IE As New InternetExplorer
    
    IE.Visible = True
    MsgBox IIf(SavePage(Url, ThisWorkbook.Path & "\", IE), "Page Saved", "Page Not Saved")
     
End Sub

Private Function SavePage(Url As String, SaveToPath As String, IE As InternetExplorer) As Boolean
    On Error GoTo Err_SavePage
    
    IE.navigate "http://www.MrExcel.com"
    'you should provide a timeout
    Do
        DoEvents
    Loop While Not IE.readyState = 4
    
    IE.ExecWB OLECMDID_SAVEAS, OLECMDEXECOPT_DODEFAULT
    SavePage = True
    
    Exit Function
Err_SavePage:
End Function

Tom
 
Upvote 0
Right_Click said:
I think the OP wants to save a complete HTML page with all relative objects. The identical action a user takes when choosing SaveAs from IE's menu and saving as HTML Complete.

*sniped*

Tom
Tom, you are correct.

I figured it would be a simple command stringing together an object and some methods or what not. My searches have led me to nothing that will duplicate the a SaveAs from IE using VB/A code. The code you just posted is as close as I have come.

EDIT: In Office 2000, I get a user-defined type not defined error. I've tried moving the code amongst different modules (module1, woorkbook, sheet1) no luck.
 
Upvote 0
Yeah. We are probably missing something easy out there but I have enjoyed working this out. Give me a few minutes and I'll wrap up your solution.
 
Upvote 0
Excellent! That would be sweet.

I just edited my previous post with an error message, presumably after you posted.
 
Upvote 0
As for your error. You will need to set a reference to Microsoft Internet Controls...

Download Example Here.

I'll post the code some other time. For now, download the above zipfile which contains the two workbooks. This was the only way I could figure out a way to keep this an ALL Excel solution. Extract and keep the workbooks together in the same path. You will only need to open "SaveAsHtmlPageIeAutomated.xls". Do not open or run the code in "IeDownloadHelper.xls". If you do, disable macros or the workbook will simply close it'self.

Open "SaveAsHtmlPageIeAutomated.xls". The URLs go anywhere in column A with the associative name that you wish to give the local file in column B. Click on the button to see several examples.

Tom
 
Upvote 0
Hi all,

Looking through Internet related API functions , I found the URLDownloadToFile which as its name implies does just what the OP needs and it is incredibly simple !

Here is an example :

In a Standard Module:

Code:
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


Function DownloadFile(URL As String, LocalFilename As String) As Boolean
    Dim lngRetVal As Long
    lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
    If lngRetVal = 0 Then DownloadFile = True
End Function


Sub test()

    DownloadFile "http://www.MrExcel.com", "c:\CoolXL.htm"
    
End Sub

Regards.


Late edit: This will only save Text but not Images on the web page :-(
 
Upvote 0
rafaaj2000 said:
Hi all,

Looking through Internet related API functions , I found the URLDownloadToFile which as its name implies does just what the OP needs and it is incredibly simple !

*snip*
Regards.


Late edit: This will only save Text but not Images on the web page :-(
Thanks rafaaj, getting closer to what I am looking for.

Do these functions need to be placed into any specific module? Mod1, Sheet1, or the workBook mod?
 
Upvote 0
rafaaj2000 said:
Looking through Internet related API functions , I found the URLDownloadToFile which as its name implies does just what the OP needs and it is incredibly simple !
I've gotta learn how to search those better!

BF, Standard Module, not a sheet module. VBE | Insert | Module.
 
Upvote 0
Right_Click said:
As for your error. You will need to set a reference to Microsoft Internet Controls...

*snip*

Tom
Thanks Tom, this combo of files gives me the results I am looking for in a round about way. At least I have something to work with until the URLDownloadToFile function can give me the same results in a simpler fasion.

Thanks for the help everyone. But I don't think I am finished with this thred until we find a function to achieve similar results.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top