Wrox Programmer Forums
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 23rd, 2006, 06:33 PM
Registered User
 
Join Date: Oct 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Saving a Web Page

Hi, 1st post so bare with me....

I have opened a web page in vba from Excel as an object, I now wantw want to save the page as a text file on my C:, what do I do help?????

M

 
Old October 24th, 2006, 02:51 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 173
Thanks: 0
Thanked 3 Times in 3 Posts
Default

The following should do you. You'll need project references to Microsoft Scripting Runtime (C:\WINDOWS\system32\scrrun.dll), Microsoft HTML Object Library (C:\WINDOWS\system32\MSHTML.TLB) & Mcirosoft Internet Controls (C:\WINDOWS\system32\shdocvw.dll).

Code:
Sub ExplorerCont********************()

Dim IExp As SHDocVw.InternetExplorer
Dim hDoc As MSHTML.HTMLDocument
Dim fsoFSO As Scripting.FileSystemObject
Dim fsoFile As Scripting.TextStream

    Set IExp = New SHDocVw.InternetExplorer
    ' You don't have to show the app window
    'IExp.Visible = True
    IExp.Navigate "http://www.google.com"

    ' Wait for the page to load
    Do Until IExp.Busy = False
        DoEvents
    Loop

    ' Set the HTML Document
    Set hDoc = IExp.Document

    ' Set up the FileSystemObject and create a text file
    Set fsoFSO = New Scripting.FileSystemObject
    Set fsoFile = fsoFSO.CreateTextFile(Filename:="C:\Webpage source code.txt")

    ' Write the source code of the webpage to the text file
    fsoFile.WriteLine Text:=hDoc.Body.innerHTML
    fsoFile.Close

    ' Quit Internet Explorer
    IExp.Quit

    ' De-reference variables
    Set IExp = Nothing
    Set hDoc = Nothing
    Set fsoFSO = Nothing
    Set fsoFile = Nothing

End Sub
 
Old October 24th, 2006, 02:28 PM
Registered User
 
Join Date: Oct 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'll give that a go cheers matey....



 
Old October 25th, 2006, 05:57 PM
Registered User
 
Join Date: Oct 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That worked nicely but I have another question.

Can you save a web page as a text file as if you were literally going File/SaveAs ??????.txt

So far i've got

Public Sub Retrieve()
Dim MyPage As Object
Dim IeApp As InternetExplorer

Set IeApp = New InternetExplorer

IeApp.Visible = True
IeApp.Navigate "http://www.google.com"
Do: Loop Until IeApp.readyState = READYSTATE_COMPLETE

End Sub


What do i need to add to save the site as a text file on my C:

Thanks

M

 
Old October 26th, 2006, 02:35 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 173
Thanks: 0
Thanked 3 Times in 3 Posts
Default

That's a pretty simple amendment from my original code. You just need to use the InnerText property rather than the InnerHTML property. Example as follows:

Code:
Sub ExplorerCont********************()

Dim IExp As SHDocVw.InternetExplorer
Dim hDoc As MSHTML.HTMLDocument
Dim fsoFSO As Scripting.FileSystemObject
Dim fsoFile As Scripting.TextStream

    Set IExp = New SHDocVw.InternetExplorer
    ' You don't have to show the app window
    'IExp.Visible = True
    IExp.Navigate "http://www.google.com"

    ' Wait for the page to load
    Do Until IExp.Busy = False
        DoEvents
    Loop

    ' Set the HTML Document
    Set hDoc = IExp.Document

    ' Set up the FileSystemObject and create a text file
    Set fsoFSO = New Scripting.FileSystemObject
    Set fsoFile = fsoFSO.CreateTextFile(Filename:="C:\Webpage text.txt")

    ' Write the source code of the webpage to the text file
    fsoFile.WriteLine Text:=hDoc.Body.innerText
    fsoFile.Close

    ' Quit Internet Explorer
    IExp.Quit

    ' De-reference variables
    Set IExp = Nothing
    Set hDoc = Nothing
    Set fsoFSO = Nothing
    Set fsoFile = Nothing

End Sub
 
Old October 26th, 2006, 02:39 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 173
Thanks: 0
Thanked 3 Times in 3 Posts
Default

NB you can also use the OuterHTML and OuterText properties although in my experience they basically do the same things.

Maccas

 
Old November 29th, 2006, 10:23 PM
Registered User
 
Join Date: Nov 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

how do I get project references to microsoft scripting runtime?:)

 
Old November 30th, 2006, 04:59 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 173
Thanks: 0
Thanked 3 Times in 3 Posts
Default

IN the VBE select Tools -> References... From the ensuing dialogue box, scroll down the list of check box options till you get to "Microsoft Scripting Runtime," check it and click ok. You should be done.

If for some reason "Microsoft Scripting Runtime" is not in the list of options you could also browse for the file - it should be at roughly this sort of location "C:\WINDOWS\system32\scrrun.dll"

Maccas






Similar Threads
Thread Thread Starter Forum Replies Last Post
how to prevent a web page/window page from copying pradeeppatnaik81 C# 2 April 9th, 2008 03:36 AM
How to diplay web page inside another web page win cyberjoe C# 2 March 1st, 2007 05:35 AM
Page.Unload Personalization Saving wewald BOOK: Professional Web Parts and Custom Controls ASP.NET ISBN: 0-7645-7860-X 3 January 17th, 2006 10:38 AM
Saving web page to file treasacrowe Classic ASP Databases 4 October 21st, 2004 09:59 AM
How to format web page to a printable page? peter2004 HTML Code Clinic 2 May 3rd, 2004 01:34 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.