Upload a file to a web using VBA

B

Bruce

There is a site on our company's INTRANET that I need to upload a file to.
This is a FORM that needs the fileName to be populated, but I have not been
able to get it to accept fileName from any means I have tried. I have ALL of
the other fields working and it is to the point all I need to do is manually
select the file, but I need to automate this.

I have been able to access the site, stuff all kinds of other info into the
websites varables, but I have NOT been able to stuff the fileName to upload.
Here is the code (including some failed attempts) to get the file name into
the web page.
First, I am opening the page and logging in with a userform ("fNTLogin")


Code:
Function ProcessScout() As Boolean
Dim oScout As Object
Dim ScoutWeb As String
Dim fileName As String
Dim newscoutname As String
Dim F As fNTLogin
'name of the file to upload
newscoutname = "h:\Scout\ScoutInPut" &
Application.Substitute(Format(Date, "yyyymmdd"), "/", "-") & ".txt"

GetAccountList 'function to get a list of account numbers
ScoutWeb =
"http://internalWebsite.com/network_monitoring/servlet/network_monitoring"
Set oScout = OpenScout(ScoutWeb) 'opens the site and gets the object
'oScout'
'waitforweb2 (oScout)
While oScout.busy
DoEvents
Wend
While oScout.ReadyState <> 4
DoEvents
Wend
spage = oScout.document.DocumentElement.outerHTML ' the the outer html
for testing purposes
webWait = oScout.document.Title

'test for logged in
If InStr(spage, "Please enter your NT user name and password") > 0 Then
'login
Do While InStr(spage, "Please enter your NT user name and password") > 0
Set F = New fNTLogin  'Creates the login form
F.Value1 = ""  'set username value to ""
F.Value2 = ""  'set password value to ""
F.Display  'shows the form
pub_UserName = F.Result1 'sets the username for other logins
pub_pw = F.Result2  'sets the pw for other logins
Set F = Nothing 'distroys the form
With oScout.document.form1
.all.Item("user").Value = pub_UserName
.all.Item("pass").Value = pub_pw
.Submit
End With
While oScout.busy
DoEvents
Wend
While oScout.ReadyState <> 4
DoEvents
Wend
spage = oScout.document.DocumentElement.outerHTML 'resetting spage for
testing if logged in
Loop

End If
' We are now logged in
oScout.Navigate
"http://internalwebsite.com:8070/network_monitoring/servlet/network_monitoring?reqType=cmLookupPage"
' move to the page to upload account info
While oScout.busy
DoEvents
Wend
While oScout.ReadyState <> 4
DoEvents
Wend
oScout.document.form1.searchType.Value = "bulkaccount"
oScout.document.form1.bulkLoadType.Value = "account"
oScout.document.getElementById("divMAC").Style.visibility = "hidden"
oScout.document.getElementById("divPhone").Style.visibility = "hidden"
oScout.document.getElementById("divAccount").Style.visibility = "hidden"
oScout.document.getElementById("divNode").Style.visibility = "hidden"
oScout.document.getElementById("divBulkMAC").Style.visibility =
"visible"
oScout.document.form1.Mac.Value = ""
oScout.document.form1.account.Value = ""
oScout.document.form1.phone.Value = ""
oScout.document.form1.Node.Value = ""
oScout.document.form1.reqType.Value = "onDemandBatch"
oScout.document.form1.Encoding = "multipart/form-data"

With oScout.document.form1  'document name = "document" form name =
"form1"
.report(3).Click 'select the third report option  'this works
.Email.Value = "[email protected]" ' this works
.FileName.Value = "H:\Scout\ScoutInPut1-7-2010.txt"    'this does
not error, but it does not stuff the name
End With

'  UploadFile oScout,  "H:\Scout\ScoutInPut1-7-2010.txt", "fileName" '
another failed attempt

'Failed attempt below
'      Set Files = oScout.document.getElementsByTagName("input")
'      For Each File In Files
'
'           If File.Type = "file" Then
'               If SetFeatureTo Then
'                   check.Checked = True
'                  ' Exit Sub
'               Else
'                  ' check.Checked = False
'                  ' Exit Sub
'               End If
'           End If
'    '   End If
'    '   r = check.Value
'   Next


oScout.document.form1.fileName.Value = "H:\Scout\ScoutInPut1-7-2010.txt"
' no errors but does not populate     Call setfeature(oScout, "billing",
True)
Call setfeature(oScout, "ecx", True)
Call setfeature(oScout, "gds", True)
Call setfeature(oScout, "bacc", True)
Call setfeature(oScout, "switch", True)
With oScout.document.form1
.all.Item("fileName").Value = "H:\Scout\ScoutInPut1-12-2010.txt"

'.all.Item("pass").Value = pub_pw
'.Submit.Click
End With

MsgBox "Please select file and pres Submit"   '<< this is to handle it
in the
'meantime until I get the stuffing of the file name working
oScout.document.form1.Submit
spage = oScout.document.DocumentElement.Title
Debug.Print spage

webWait = oScout.document.Title

End Function



Here is the webpage html code that needs to recieve the filename:

Enter Email Address:<br>
<input type=text size=25 name=email value=""><br><br>
Select File To Upload:<br>
<input type=file name=fileName><br>



Thanks
Bruce
 
T

Tim Williams

Typically the only way to assign the file on a HTML form file element is to
actually click the button and select the file.
The input element's functionality is not exposed to scripting or automation
for security reasons.

If it were possible to set it programmatically (or by pre-populating the
path in the page HTML) then it would be possible to grab files without folk
knowing. I realize this is an intranet application, but I'm not sure
there's any way to relax that security.

Tim


Bruce said:
There is a site on our company's INTRANET that I need to upload a file to.
This is a FORM that needs the fileName to be populated, but I have not
been able to get it to accept fileName from any means I have tried. I have
ALL of the other fields working and it is to the point all I need to do is
manually select the file, but I need to automate this.

I have been able to access the site, stuff all kinds of other info into
the websites varables, but I have NOT been able to stuff the fileName to
upload.
Here is the code (including some failed attempts) to get the file name
into the web page.
First, I am opening the page and logging in with a userform ("fNTLogin")


Code:
Function ProcessScout() As Boolean
Dim oScout As Object
Dim ScoutWeb As String
Dim fileName As String
Dim newscoutname As String
Dim F As fNTLogin
'name of the file to upload
newscoutname = "h:\Scout\ScoutInPut" &
Application.Substitute(Format(Date, "yyyymmdd"), "/", "-") & ".txt"

GetAccountList 'function to get a list of account numbers
ScoutWeb =
"http://internalWebsite.com/network_monitoring/servlet/network_monitoring"
Set oScout = OpenScout(ScoutWeb) 'opens the site and gets the object
'oScout'
'waitforweb2 (oScout)
While oScout.busy
DoEvents
Wend
While oScout.ReadyState <> 4
DoEvents
Wend
spage = oScout.document.DocumentElement.outerHTML ' the the outer html
for testing purposes
webWait = oScout.document.Title

'test for logged in
If InStr(spage, "Please enter your NT user name and password") > 0 Then
'login
Do While InStr(spage, "Please enter your NT user name and password") >
0
Set F = New fNTLogin  'Creates the login form
F.Value1 = ""  'set username value to ""
F.Value2 = ""  'set password value to ""
F.Display  'shows the form
pub_UserName = F.Result1 'sets the username for other logins
pub_pw = F.Result2  'sets the pw for other logins
Set F = Nothing 'distroys the form
With oScout.document.form1
.all.Item("user").Value = pub_UserName
.all.Item("pass").Value = pub_pw
.Submit
End With
While oScout.busy
DoEvents
Wend
While oScout.ReadyState <> 4
DoEvents
Wend
spage = oScout.document.DocumentElement.outerHTML 'resetting spage for
testing if logged in
Loop

End If
' We are now logged in
oScout.Navigate
"http://internalwebsite.com:8070/network_monitoring/servlet/network_monitoring?reqType=cmLookupPage"
' move to the page to upload account info
While oScout.busy
DoEvents
Wend
While oScout.ReadyState <> 4
DoEvents
Wend
oScout.document.form1.searchType.Value = "bulkaccount"
oScout.document.form1.bulkLoadType.Value = "account"
oScout.document.getElementById("divMAC").Style.visibility = "hidden"
oScout.document.getElementById("divPhone").Style.visibility = "hidden"
oScout.document.getElementById("divAccount").Style.visibility =
"hidden"
oScout.document.getElementById("divNode").Style.visibility = "hidden"
oScout.document.getElementById("divBulkMAC").Style.visibility =
"visible"
oScout.document.form1.Mac.Value = ""
oScout.document.form1.account.Value = ""
oScout.document.form1.phone.Value = ""
oScout.document.form1.Node.Value = ""
oScout.document.form1.reqType.Value = "onDemandBatch"
oScout.document.form1.Encoding = "multipart/form-data"

With oScout.document.form1  'document name = "document" form name =
"form1"
.report(3).Click 'select the third report option  'this works
.Email.Value = "[email protected]" ' this works
.FileName.Value = "H:\Scout\ScoutInPut1-7-2010.txt"    'this does
not error, but it does not stuff the name
End With

'  UploadFile oScout,  "H:\Scout\ScoutInPut1-7-2010.txt", "fileName" '
another failed attempt

'Failed attempt below
'      Set Files = oScout.document.getElementsByTagName("input")
'      For Each File In Files
'
'           If File.Type = "file" Then
'               If SetFeatureTo Then
'                   check.Checked = True
'                  ' Exit Sub
'               Else
'                  ' check.Checked = False
'                  ' Exit Sub
'               End If
'           End If
'    '   End If
'    '   r = check.Value
'   Next


oScout.document.form1.fileName.Value =
"H:\Scout\ScoutInPut1-7-2010.txt" ' no errors but does not populate
Call setfeature(oScout, "billing", True)
Call setfeature(oScout, "ecx", True)
Call setfeature(oScout, "gds", True)
Call setfeature(oScout, "bacc", True)
Call setfeature(oScout, "switch", True)
With oScout.document.form1
.all.Item("fileName").Value = "H:\Scout\ScoutInPut1-12-2010.txt"

'.all.Item("pass").Value = pub_pw
'.Submit.Click
End With

MsgBox "Please select file and pres Submit"   '<< this is to handle it
in the
'meantime until I get the stuffing of the file name working
oScout.document.form1.Submit
spage = oScout.document.DocumentElement.Title
Debug.Print spage

webWait = oScout.document.Title

End Function



Here is the webpage html code that needs to recieve the filename:

Enter Email Address:<br>
<input type=text size=25 name=email value=""><br><br>
Select File To Upload:<br>
<input type=file name=fileName><br>



Thanks
Bruce
 
J

joel

Have you been able to enter you email address?


The tag "Input" is an array You may be putting filename in the wron
index of the array. Indexing will alway start with 0 (which is item 1)
I can't tell from your code how many Input tags are on your page.

One thing that I've been able to open the webpage manually (thi
doesn't work when the page is called from VBa) and then enter the
string into the input box. I usually just put my name into the box I'
trying to located. Then use the developers tool on the IE explorer b
typing F12. There is a search box in the developers tool where you ca
search for the string that you put into the box
 
B

Bruce

I have been able to populate email...
I think Tim is gonna be right, I have seen several other referances to
that...
I will try the array method and see if that works.
I may have to try sendkey, if I can figure out how to place the curser in
the right box...

Thanks
Bruce
 

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