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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to open a view source and save it? 1

Status
Not open for further replies.

AFI247365

Programmer
Jul 12, 2001
13
US
I've been working on this one for some time now and have run ito a brick wall.
This is my code so far:

Dim WebBrowser As New InternetExplorer

Private Sub Command1_Click()

Dim n, pathname

n = 1

Const navOpenInNewWindow = 1

WebBrowser.Visible = False

Do While Not n = 15

WebBrowser.Navigate ("view-source: & n), navOpenInNewWindow

n = n + 1

Loop

End Sub


So i can open a browse and open the source code but i cant seem to find a way to save the file.
I have tried to look for the temp file that is made but it saved in different directoried under
C:\Windows\temopreryinternetfiles\content.ie5 and then in one of 12 different sub directories and they never apperar in just one of these directories.

i really need some help!

thanx
 
Why don't you just download the file instead of using a viewer to get it downloaded? There are API calls for downloading files. Look at for examples of how to do this.

Best regards
 

I have done something similar to what you are doing with the webbrowser control and the XML object that you can download from microsoft.

What I do is navigate to a url with the webbrowser control and when I get a success status from it I use the XML Object to request the same url.

To me, it looks like what it does is check the cache first for the requested file/document and will find it there since the WB previously downloaded it.

Once it is found I then save the file to whatever name and location I want.

Good Luck
 
im running a loop over this program and need to have the text file closed because it has an error when I try the File copy method. Is there a way to close an open text file that is just open from the cache?
 
I have the XML object but I dont realy know how to use it.
is there a site that i can got to to get some examples of how to use the object.
THANX.
 

Here is some code to get you started. You should be able to modify this as you see fit.

Public Sub XMLRequestSave(URLToReq As String, SaveLocation As String)

Dim X As XMLHTTP
Dim A As ADODB.Stream

Set X = New XMLHTTP
Set A = New ADODB.Stream

X.open "GET", URLToReq, True

Do While X.readyState <> 4
DoEvents
Loop

If X.Status = 200 Then 'http code for ok

A.Type = adTypeBinary
A.open
A.Write X.responseBody
A.SaveToFile SaveLocation, adSaveCreateOverWrite

Else

'unable to get file for some reason

End If

End Sub



Good Luck
 
Everything seems to work accept that X.status and x.responseBody keep giving me system errors.

any suggestions?

here is my code so far.
------------------------------------------------------------

Dim WebBrowser As New InternetExplorer
Dim X As XMLHTTP
Dim A As ADODB.Stream
Dim SaveLocation
Dim URLtoReq

Public Sub Command1_Click()
Dim n
n = 1
Const navOpenInNewWindow = 1

Set X = New XMLHTTP
Set A = New ADODB.Stream

Do While X.ReadyState <> 4 And n <= 2
URLtoReq = &quot;view-source: & n
WebBrowser.Navigate (&quot;view-source: & n), navOpenInNewWindow
X.Open &quot;GET&quot;, URLtoReq, True

SaveLocation = &quot;C:\biocarta\&quot; & n & &quot;.txt&quot;

Debug.Print X.Status
If X.Status = 200 Then 'http code for ok

A.Type = adTypeBinary
A.Open
A.Write X.responseBody
A.SaveToFile SaveLocation, adSaveCreateOverWrite

Else
Debug.Print X
'unable to get file for some reason

End If
n = n + 1
Loop
End Sub

P.s. is there a way to close the web browser and text file?
 
With the webbrowser control there is a document complete event that I use. I do something like the following.

Dim Wait As Boolean

Private Sub Command1_Click()
WB.Navigate &quot;Do While Wait = False
DoEvents
Loop
XMLRequestSave &quot; &quot;my path&quot;
End Sub

Private Sub WB_DocumentComplete(ByVal pDisp As Object, URL As Variant)
Wait = True
End Sub

Public Sub XMLRequestSave(URLToReq As String, SaveLocation As String)

Dim X As XMLHTTP
Dim A As ADODB.Stream

Set X = New XMLHTTP
Set A = New ADODB.Stream

X.open &quot;GET&quot;, URLToReq, True

Do While X.readyState <> 4
DoEvents
Loop

If X.Status = 200 Then 'http code for ok

A.Type = adTypeBinary
A.open
A.Write X.responseBody
A.SaveToFile SaveLocation, adSaveCreateOverWrite

Else

'unable to get file for some reason

End If

End Sub


The Reason that you are getting an error in your code is you have the request in the do loop for both the webbrowers control and the XML object and you have the saving of the file in the same place. If the file is not here it cannot be saved i.e. no data. With the webbrowser control you only need to make the request once and the same with the XML object.

Please look at my code a little more closer. It goes something like this psudo code.

webbrowser.navigate to url
wait on webbrowser for document to download completly
once complete request with XML
wait on xml to complete download
once xml is done check status for success
if success then save file to a known location
open file in known location for parsing



to answer your last question close both objects with the use of .close and set=nothing (A and X, ADODB.Stream and XMLHTTP). I do not know if you really need to close the webbrowser control at all. But I do have the suggesion that you check to see if it is presently working before trying to close the form its on. It may not allow you to close your program correctly if it is presently downloading a page.

I hope this helps and good luck
 
I refer you to the somewhat simpler solution I gave two days ago to your question in the Visual Basic(Microsoft) ActiveX Controls and DLLs Forum:

thread708-343318
 
everything except the X.status and A.responsebody. I have everything set up as you instructed but it keeps getting hung up on that theose two problems and having unknown System errors.
 

That should be X.responsebody.

x.status is the http response codes i.e. 404 for not found, 403 access denied, 200 response ok etc.

if for some reason you have not waited long enough for the x.readystate to equal 4 then this may be why you are having other problems.

I looked at my code last night and there are two events that I wait on from the webbrowser control. The document complete event and the progresschange event.

Here is the results of a test that I just ran here. This illustrates progresschange, filedownload,downloadcomplete, and the filedownload events as they are fired when requesting a page.

100 10000
100 10000
FileDownload
Download complete
150 10000
10000 10000
-1 10000
Download complete
10000 10000
document complete
0 0

as you can see the document complete event fires just before the progresschange event resets itself.


I hope this helps you and good luck
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top