Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 23rd, 2006, 06:33 PM
Registered User
 
Join Date: Oct 2006
Location: Billericay, Essex, United Kingdom.
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

Reply With Quote
  #2 (permalink)  
Old October 24th, 2006, 02:51 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: London, , United Kingdom.
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
Reply With Quote
  #3 (permalink)  
Old October 24th, 2006, 02:28 PM
Registered User
 
Join Date: Oct 2006
Location: Billericay, Essex, United Kingdom.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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



Reply With Quote
  #4 (permalink)  
Old October 25th, 2006, 05:57 PM
Registered User
 
Join Date: Oct 2006
Location: Billericay, Essex, United Kingdom.
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

Reply With Quote
  #5 (permalink)  
Old October 26th, 2006, 02:35 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: London, , United Kingdom.
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
Reply With Quote
  #6 (permalink)  
Old October 26th, 2006, 02:39 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: London, , United Kingdom.
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

Reply With Quote
  #7 (permalink)  
Old November 29th, 2006, 10:23 PM
Registered User
 
Join Date: Nov 2006
Location: Sydney, NSW, Australia.
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Reply With Quote
  #8 (permalink)  
Old November 30th, 2006, 04:59 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: London, , United Kingdom.
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

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 06:26 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.