Help for Download web data use "Save As" to C: dri

nanjing

New Member
Joined
Dec 7, 2006
Messages
3
I can use userid and passwd to auto login a web site by using the following VBA codes.
After that, there are many files on this web page.
File12012006.txt
File12022006.txt

I can't find the web path (http:) for the file. When I click on the txt file, it will open as a txt file with no path.

How should I save this file to my C: drive by using VBA?

Thank you.


Sub Main()
Dim objIE As Object
Dim strWebSite As String
Dim intReadyState As Integer

Set objIE = CreateObject("InternetExplorer.Application")

strWebSite = "http://www.sss.com"

objIE.Visible = 1

objIE.Navigate strWebSite

Do While Not intReadyState = 4
intReadyState = objIE.ReadyState
Loop

Set ipf = objIE.Document.all.Item("txtUsername")

ipf.Value = "XXX"

Set ipf = objIE.Document.all.Item("txtPassword")

ipf.Value = "XXX"

Set ipf = objIE.Document.all.Item("cmdOK")

ipf.Click

'ipf.Submit


End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Declare Function URLDownloadToFile _
Lib "urlmon" Alias "URLDownloadToFileA" _
(ByVal pCaller As Long, ByVal szURL As String, _
ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long


Private Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" _
Alias "DeleteUrlCacheEntryA" _
(ByVal lpszUrlName As String) As Long


Private Const dlSeccess As Long = 0
Private Const newestVer As Long = &H10

Function wpURLtest2(ByVal strURL2 As String) As Boolean
Dim wpHTTP As Object

'On Error Resume Next
Set wpHTTP = CreateObject("Microsoft.XMLHTTP")

wpHTTP.Open "GET", strURL2, False

PauseTime = 5
Start = Timer

Do While Timer < Start + PauseTime
DoEvents
If Timer > Start + (PauseTime - 1) Then wpURLtest = True: wpHTTP.Abort
wpHTTP.Send
DoEvents
Loop

If wpHTTP.StatusText <> "OK" Then wpURLtest = True: wpHTTP.Abort

Set wpHTTP = Nothing
End Function

Function wpURLtest(ByVal strURL2 As String) As Boolean
Dim wpHTTP As Object

On Error Resume Next
Set wpHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
If wpHTTP Is Nothing Then
Set wpHTTP = CreateObject("WinHttp.WinHttpRequest.5")
End If
On Error GoTo 0

wpHTTP.Open "GET", strURL2, False

wpHTTP.SetTimeouts 0, 0, 10, 0
wpHTTP.Send

If wpHTTP.StatusText <> "OK" Then wpURLtest = True

Set wpHTTP = Nothing
End Function

Function DownloadFile(myURL As String, strHTML As String) As Boolean

DoEvents
DownloadFile = URLDownloadToFile(0&, _
myURL, _
strHTML, _
newestVer, _
0&) = dlSeccess

If dlSeccess <> 0 Then MsgBox "Failed to download Web Page!"
End Function

Sub saveWebToHTML()
Dim strURL$, URLMsg$, URLTitle$, URLDefault$
Dim strHTML$, HTMLMsg$, HTMLTitle$, HTMLDefault$
Dim bfile&, htmCode$

URLTitle = "Get Web Page!"
URLMsg = "Enter an ""URL"" to the Web Page to copy, below:" & _
vbLf & vbLf & "As the: Uniform Resource Locator (URL): [Protocol: ""HTTP"" + " & _
"Domain (Web Server): ""IP address"" (as the file location): (path) and/or name.]" & vbLf & _
vbLf & "Syntax: " & vbLf & _
"""scheme://user:password@host:port/url-path;params?query#fragment""" & _
vbLf & vbLf & "Like the default URL given below!"

URLDefault = "http://www.MrExcel.com"

HTMLTitle = "Get File Storage Location!"
HTMLMsg = "Enter the: ""Drive:\Path\FileName.htm""" & vbLf & vbLf & _
"for your Web Page to be copied to, below!"

HTMLDefault = "C:\myWebFile.htm"

strURL$ = InputBox(URLMsg, URLTitle, URLDefault)
strHTML$ = InputBox(HTMLMsg, HTMLTitle, HTMLDefault)

If wpURLtest(strURL) = True Then GoTo myErr

On Error GoTo myErr
Call DeleteUrlCacheEntry(strURL)

DoEvents
If DownloadFile(strURL, strHTML) Then
bfile = FreeFile
Open strHTML For Input As #bfile
htmCode = Input$(LOF(bfile), bfile)

'Option to view htm code!
'MsgBox htmCode

Close #bfile
End If

If dlSeccess <> 0 Then GoTo myErr

MsgBox "URL: """ & strURL & """" & vbLf & vbLf & _
"Storage Location: """ & strHTML & """" & vbLf & vbLf & vbLf & _
"Web Page Successfully Stored!"
GoTo myEnd

myErr:
MsgBox "File download failed!" & vbLf & vbLf & _
"You entered:" & vbLf & vbLf & _
"URL: """ & strURL & """" & vbLf & vbLf & _
"Storage Location: """ & strHTML & """" & vbLf & vbLf & _
"Check information and try again!"

myEnd:
End Sub
 
Upvote 0
You e-mailed me indicating that you do not have IP web address to put in the code?
I do not have a clue what you are talking about?
Even the basic code you posted needs an IP?
 
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,726
Members
448,987
Latest member
marion_davis

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