Save HTML source code from VBA?

jflurrie

New Member
Joined
Sep 22, 2008
Messages
43
Hi,

I would need to get HTML source code from any given page. I know how to open a HTML page from Excel and I can do it with VBA, but how to get for example this page's source code?

I would start with making a sub that takes a string (the address) as an input parameter and finish with saving the source code of that address as an text file like c:\code.txt

So something like

Dim webaddress as string

Sub GetSourceCode(webaddress)
'then some code to save the source code
End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Re: How to save HTML source code from VBA?

It's contained in the Document.Body.innerHTML property of the InternetExplorer Application, assuming that's what you're using to navigate to the web page.
 
Upvote 0
Re: How to save HTML source code from VBA?

Can you post the code you are currently using?

Also could you explain why you want/need to get the HTML source code?
 
Upvote 0
Re: How to save HTML source code from VBA?

Can you post the code you are currently using?

Also could you explain why you want/need to get the HTML source code?

Well the code is pretty much there already.

The reason is to be able to parse through whole html file, to find starting points (many times tags, not always) and ending points (might be a letter W or number 7 or some tag or what ever user wants). So user gives source (like www.google.com or www.denverpost.com) and tells program to find specific thing from page. It can be like "return all info between first found mark £ to first found string "Weather of Amazon"" or anything like that. I think that the only thing I can't do is to "transform" the given web page into a text file automatically, in other words, to read the source code. I would be able to do this pretty easily from a text file, but the missing link is automatically saving the source code. I can do it easily manually from Firefox or Internet Explorer, all it takes is to choose "View page source" from menus and then save it as a text file.

It seems pretty hard to handle from workbook if it's just directly opened from Excel.
 
Last edited:
Upvote 0
Re: How to save HTML source code from VBA?

Well can you post the current code?

It might help us help you.:)

I would also suggest you may want to look into another method rather than trying to parse the HTML source code.

It is possible obviously but I can imagine it could be rather tricky, especially if you are dealing with different websites/pages.
 
Upvote 0
Re: How to save HTML source code from VBA?

The current code is as simple as:


Dim webaddress as string

Sub GetSourceCode(webaddress)
'then some code to save the source code
End Sub



So what I need is to know that do I need to connect to IE object model and if or if not then what?

I also know that manually this thing is extremely simple, but is it wise to use just send keys, like in firefox ctrl+t, www.cnn.com, enter, alt+u, ctrl+a, ctrl+c, alt+tab, (to activate a text file), ctrl+v, alt+F4, enter, enter? Is there some way to do it "directly" from VBA? I have understood that there is a rule "if possible avoid send keys" among VBA programmers.

The other method that you mentioned would be nice, but this code is supposed finally to work with any text file too. So I just thought it might be the simplest way to just use a text file to parse from, regardless of where this text file actually came from. Web page is just an example, and probably will be the most popular way to use this program once it's ready.
 
Last edited:
Upvote 0
Re: How to save HTML source code from VBA?

Eh?.:eek:

What code?

You haven't posted any code so far, that's just a sub code stub.:)
 
Upvote 0
Re: How to save HTML source code from VBA?

Eh?.:eek:

What code?

You haven't posted any code so far, that's just a sub code stub.:)

Exactly. That's how far I got. Because I know that opening a web page from Excel is extremely simple, I just wonder whether I can open it in "source code mode" just as easily.

This is how I open page in "normal mode":

Sub OpenPage()

Dim oBk As Workbook
Set oBk = Workbooks.Open("http://www.cnn.com")

End Sub

(I believe it's simple enough not to use code tags, after all, it's like one line)

Can I feed some parameter to open it in "source code mode", so that instead of showing all the pictures there would just be a lines like < img > pic.jpg < /img >?
 
Last edited:
Upvote 0
Re: How to save HTML source code from VBA?

Sorry but you're sort of losing me here.:eek:

Sure that code might open a webpage but I don't really think it's going to give you any control over it, or access the HTML source code.:)

If you search this forum then you should find examples that show how to automate IE, open websites etc
 
Upvote 0
Re: How to save HTML source code from VBA?

Sorry but you're sort of losing me here.:eek:

Sure that code might open a webpage but I don't really think it's going to give you any control over it, or access the HTML source code.:)

If you search this forum then you should find examples that show how to automate IE, open websites etc

I thought it won't give me any control over opening that page, but that's the reason why my code is so short right now. In other words, I don't know if I need to automate IE or if I can just alter that only code line there to
Set oBk = Workbooks.Open("http://www.kemi.fi" mode:=htmlcode)
or something like that. That would do it, if I just would find out what parameters to use.

Does Excel save the web page it opens in some temporary file? Would that help me?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,264
Messages
6,171,081
Members
452,377
Latest member
bradfordsam

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