Tweaking to pull a local HTML page instead of a server page?

K

Ker_01

The code below is part of my code that cycles through server pages to parse
them. It takes so long to work (much less get to the pages of interest) that
I'm having difficulty testing, so I found one page that has content of
interest and saved it to my desktop. Now I want to (temporarily) load that
page instead of a page from the server to test regex expressions for parsing
the pages.

Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
objHTTP.setTimeouts 0, 0, 0, 0
URL = "C:\Documents and Settings\User\Desktop\SamplePage1.htm"
objHTTP.Open "GET", URL, False
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible;
MSIE 6.0; Windows NT 5.0)"
objHTTP.send ("")
SourceHTMLText = objHTTP.responseText

Unfortuately, on the objHTTP.Open "GET", URL, False line I get the following
error:
Run-time error '-2147467269 (80004005)': unspecified error, presumably
because I'm not connecting through a server?

If I can't open the page as an HTML file, the other option would be to open
it as a flat file- what I really need is the multiline mega-string of the
file text so I can manipulate it with my test regex statements, so I don't
want to load a line at a time and don't want Excel to add any best guesses
at delimiters. When I look at sample code it all seems to read a line at a
time (opentext method, etc)- is there an easier way to just load the entire
..htm file at once as a single string?

Thanks!
Keith
 
J

JP

What are you declaring 'objHTTP' as? Are you early or late-binding the
objects?

Can you browse to the page using Microsoft Internet Controls/HTML
Object Library instead?


--JP
 
K

Ker_01

To be honest, I'm hobbling code together from snippets I've gotten from this
group and the web, and I'm new to everything HTML- so I probably can't
answer technical questions beyond the code itself that was in the original
post (re-posted here for anyone joining the thread). Everything is early
binding to the best of my knowledge. The code below is in the only sub in my
workbook.

Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
objHTTP.setTimeouts 0, 0, 0, 0
URL = AllPages(ProcessRegEx)
objHTTP.Open "GET", URL, False
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible;
MSIE 6.0; Windows NT 5.0)"
objHTTP.send ("")
SourceHTMLText = objHTTP.responseText



What are you declaring 'objHTTP' as? Are you early or late-binding the
objects?

Can you browse to the page using Microsoft Internet Controls/HTML
Object Library instead?


--JP
 
K

Ker_01

I've got it now (or close enough) using code posted by others- I've just
kept trying new search terms until I found something compatible. Apparently
originally from RB Smissaert, which I found re-posted by Tom Ogilvy.
Thanks!
Keith

in my main sub:
Dim fname As String
Dim sVal As String
fname = "C:\Documents and Settings\RucksK\Desktop\SamplePage1.htm"
sVal = OpenTextFileToString2(fname)
'Debug.Print sVal
MsgBox sVal

and a new function:
Function OpenTextFileToString2(ByVal strFile As String) As String
' RB Smissaert - Author
Dim hFile As Long
hFile = FreeFile
Open strFile For Input As #hFile
OpenTextFileToString2 = Input$(LOF(hFile), hFile)
Close #hFile
End Function
 
J

JP

Completely understood, you're not the only one who trolls for code :)
Glad to hear it worked out for you.


Thx,
JP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top