Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

saving a web page to your local hard drive

Status
Not open for further replies.

juno101

Programmer
Nov 7, 2005
1
US
i would like to save a web page from the internet to my C: drive utilizing VBA.

Any help would be apreshiated.

Thank you,
 
Below is some code as a starter:

Code:
'Option Compare Database
'Option Explicit

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' Reply to: saving a web page to your local hard drive
'' [URL unfurl="true"]http://www.tek-tips.com/viewthread.cfm?qid=1147900&page=1[/URL]
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''
'' How To Download a File Without Prompting
'' [URL unfurl="true"]http://support.microsoft.com/default.aspx?scid=kb;en-us;244757[/URL]
'' "Note that when downloading HTML files, embedded content like images and objects will not be downloaded."
''
'' API:
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

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' MS Windows Vers: Win2kPro
'' MS Office Vers: Off2kPro
''
'' References:
'' Microsoft Internet Controls (\system32\shdocvw.dll)
''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' Procedure to:
'' 1. Start new instance of Internet Explorer,
'' 2. open spec [URL unfurl="true"]www page[/URL] (strURL),
'' 3. save as *.htm (strFile),
'' 3a. to directory *.* (strDir),
''
'' I. Automatically using "URLDownloadToFile"
'' or 
'' II. Manually using "Save Web Page" dialog
''
'' 4. Close IE
''
Sub IEstartsave()
On Error GoTo Err_IEstartsave

    Dim objIE As SHDocVw.InternetExplorer
    Dim strURL As String, strDir As String, strFile As String
    
    Const OLECMDID_CLOSE = 45 ' (&H2D)
    Const OLECMDID_SAVEAS = 4
    Const OLECMDEXECOPT_DONTPROMPTUSER = 2
    
    Set objIE = New InternetExplorer
        '
        strURL = "[URL unfurl="true"]http://support.microsoft.com/default.aspx?scid=kb;en-us;244757"[/URL]
        
        objIE.Navigate (strURL)
    
            '' While page is loading...
            Do Until objIE.ReadyState = 4   '' READYSTATE_COMPLETE
                                            
                objIE.Visible = False
                
            Loop
                '' get focus
                objIE.Visible = True
                                           
                        '' Save web page to:
                        strDir = "C\"
                        strFile = "DwnldFileWOprompting" & Format(Date, "mmddyy") & ".htm"
                            
                            '' See Err_IEstartsave for details:
                                Call URLDownloadToFile(0, strURL, strDir & strFile, 0, 0)
                                    '' Close IE
                                    objIE.ExecWB 45, 2
                        
Exit_IEstartsave:

    Set objIE = Nothing
    
    Exit Sub
    
Err_IEstartsave:
    
    '' NOTE:
    '' "URLDownloadToFile()" avoids the "Save Web Pge" dialog & related issues, eg:
        '' objIE.ExecWB OLECMDID_SAVEAS = 4 may cause "Runtime Error -2147221248 (80040100)"
        '' when Save is canceled!
    
    If Err.Number = -2147221248 Then
        Resume Exit_IEstartsave
    Else
        MsgBox Err.Description
        Resume Exit_IEstartsave
    End If
    
End Sub

Please post back with your results,


TomCologne
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top