Using VBA to enter filename on a Webpage

jace808

New Member
Joined
Jan 14, 2010
Messages
7
I have a simple text box I need to fill in with my email which I have managed to do very simply with this code;
Code:
Dim IE As InternetExplorer
Dim sIEWindowName As String
Set IE = New InternetExplorer
 
IE.Visible = True
 
sIEWindowName = "Some Webpage - Windows Internet Explorer"
 
IE.Navigate "[URL]http://somewebsite.iuse.com[/URL]"
'
Do While IE.Busy: DoEvents: Loop
Do While IE.ReadyState <> 4: DoEvents: Loop
 
IE.Document.form1.Email.Value = "[EMAIL="my.name@someisp.com"]my.name@someisp.com[/EMAIL]"

The layout looks like this with my email.value entered in.



I'm using Access 2007, but that is irrelevant for the VBA.

My problem is I can't enter the filename as easily. I have to use SENDKEYS to TAB over then a bunch of other tacky SENDKEY commands to load my file. Even though it works I don't feel it's reliable and would rather just fill the file name in a bit easier.

I used IE developer to show before and after using the BROWSE button to load the file location as seen in pic 1 and pic 2.





The code below has been my best attempt. It runs without error but it does not give me the result I need which is a file location for the application to pull.

Code:
    IE.Document.form1.FileName.Value = "C:\New Folder\aFileToUpload.txt"
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
7,346
Your HTML form has an < input type="file"> tag, but IE ignores the element value as a security precaution.

The < form> tag probably has METHOD=post and ENCTYPE="multipart/form-data" attributes. If so, you could use something like http://www.motobit.com/tips/detpg_uploadvbaie/ to upload a file without user interaction, as well as setting the other form input fields.
 

jace808

New Member
Joined
Jan 14, 2010
Messages
7
I don't quite understand the code used here and if used as is my other controls and textboxes are lost since this sub wants to "navigate" to the url clearing my inputs.
 

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
7,346
I don't understand your last post.

As I said, IE ignores any value you set for < input type="file" >, whether set by VBA (e.g. your IE.Document.form1.FileName.Value = "C:\New Folder\aFileToUpload.txt"), Javascript, or VBScript etc. The only ways I know to upload a file via a web page are by clicking the Browse button and selecting the file (by normal manual user interaction or SendKeys) or by using the form upload method in the link I posted. You are right about SendKeys: it isn't reliable and generally not recommended, so you are lucky that it has worked for you.

Can't really help you further without knowing the URL or at least the HTML code between the < form> and < /form> tags to see whether the motobit technique is applicable.
 

jace808

New Member
Joined
Jan 14, 2010
Messages
7
Your link did lead me to something MUCH easier and faster and works by automation and no SENDKEYS.

Twebst

The lite version of this app will record like a macro and spit out code to use. The only code it will create in the lite version is VBScript which I've pasted in to my project and does the trick quite nicely. Matter of fact I will probably get my boss to purchase the pro version (a little pricey) as well because it is just too good to be true.

In a round about way you did help me so thanks. :)
 

agentsmith128

New Member
Joined
Feb 27, 2014
Messages
1
Twebst has become open source and you can get it for free at: https://github.com/codecentrix/open-twebst/releases

Your link did lead me to something MUCH easier and faster and works by automation and no SENDKEYS.

Twebst

The lite version of this app will record like a macro and spit out code to use. The only code it will create in the lite version is VBScript which I've pasted in to my project and does the trick quite nicely. Matter of fact I will probably get my boss to purchase the pro version (a little pricey) as well because it is just too good to be true.

In a round about way you did help me so thanks. :)
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,445
Messages
5,837,287
Members
430,493
Latest member
CPG2970

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
Top