0

I have to copy on a weekly bases 500-600 cells into a website which is covered in textboxes. I know how to code so I know how to read source code but Excel is beyond me right now. How can I copy an Excel cell to be placed in a certain text field (I have broken it down below). Now I'll need help having it repeat it until it hit a blank cell.

A1 = id='firstname0'
A2 = id='middleinitial0'
A3 = id='lastname0'
A6 = id='ntlogin0'

B1 = id='firstname1'
B2 = id='middleinitial1'
B3 = id='lastname1'
B6 = id='ntlogin1'

C1 = id='firstname2'
C2 = id='middleinitial2'
C3 = id='lastname2'
C6 = id='ntlogin2'

Here is some of the source of the site:

<td>
    <input type='text' name='ntlogin[]' id='ntlogin0' />
</td>

<td>
    <input type='text' name='firstname[]' id='firstname0' />
</td>

<td class='center'>
    <input type='text' name='middleinitial[]' id='middleinitial0' size='1' />
</td>

<td>
    <input type='text' name='lastname[]' id='lastname0' />
</td>

<td>
    <input type='text' name='hiredate[]' id='hiredate0' />
</td>

The site asks me how many rows to create. The hire date section I can control with a 1 button push. I can not control the website so I can't make any changes to it.

Tiny
  • 27,221
  • 105
  • 339
  • 599
user237025
  • 3
  • 1
  • 3

1 Answers1

1

You can use the VBA library "Microsoft Internet Controls" and "Microsoft HTML Object Library" and fully automate web page interaction...

Below is a macro to do it... To enter the macro code, you can use Alt+F11 to enter the Microsoft Visual Basic for Applications environment.
- Add the library References (under the Tools>References menu)
- Add a code module (under Insert>Module)
- Paste the code below... modify they url

The code will open IE then go to the web page and wait for you to hit OK (after you have done whatever you need.. maybe logging in, navigating somewhere, entering the number of rows, ...). Then it will loop through all the columns you have used in your spreadsheet (using Find to populate the numCols) and drop the cell values into the HTML elements with the relevant IDs. It won't hit Add Employees for you, just in case something went wrong, but the library does give you the ability to .Click() on things.

I've used this for a few automation hacks and it seems quite reliable... Difficult parts can be waiting for page loads/updates - but you shouldn't have that problem.

'References needed:  "Microsoft Internet Controls" and "Microsoft HTML Object Library"
' (add under menu Tools > References)

Sub populate()

  Dim ws As Worksheet
  Set ws = Application.ActiveSheet

  Dim appIE As InternetExplorer
  Set appIE = New InternetExplorer
  appIE.Visible = True

  appIE.Navigate "http://localhost:8080/your_form"

  If vbCancel = MsgBox("Make sure the page has loaded, enter the number of ids etc. etc. Then click OK", vbOKCancel) Then
    Set appIE = Nothing
    Exit Sub
  End If

  Dim counter As Integer, index As Integer, numCols As Integer
  numCols = ws.Cells.Find(What:="*", After:=[A1], LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Column
  For counter = 1 To numCols
    'A1 = id='firstname0'
    'A2 = id='middleinitial0'
    'A3 = id='lastname0'
    'A6 = id='ntlogin0'
    index = counter - 1
    appIE.document.getElementById("firstname" & index).Value = ws.Cells(1, counter)
    appIE.document.getElementById("middleinitial" & index).Value = ws.Cells(2, counter)
    appIE.document.getElementById("lastname" & index).Value = ws.Cells(3, counter)
    appIE.document.getElementById("ntlogin" & index).Value = ws.Cells(6, counter)
  Next counter

  Set appIE = Nothing
  Set ws = Nothing

  MsgBox "All done - hit Submit if all OK!"

End Sub

VERSION FOR VALUES IN ROWS

'References needed:  "Microsoft Internet Controls" and "Microsoft HTML Object Library"
' (add under menu Tools > References)

Sub populate()

  Dim ws As Worksheet
  Set ws = Application.ActiveSheet

  Dim appIE As InternetExplorer
  Set appIE = New InternetExplorer
  appIE.Visible = True

  appIE.Navigate "http://localhost:8080/your_form"

  If vbCancel = MsgBox("Make sure the page has loaded, enter the number of ids etc. etc. Then click OK", vbOKCancel) Then
    Set appIE = Nothing
    Exit Sub
  End If

  Dim counter As Integer, index As Integer, numRows As Integer
  numRows = ws.Cells.Find(What:="*", After:=[A1], LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
  For counter = 1 To numRows
    'A1 = id='firstname0'
    'B1 = id='middleinitial0'
    'C1 = id='lastname0'
    'D1 = id='ntlogin0'
    index = counter - 1
    appIE.document.getElementById("firstname" & index).Value = ws.Cells(counter,1)
    appIE.document.getElementById("middleinitial" & index).Value = ws.Cells(counter,2)
    appIE.document.getElementById("lastname" & index).Value = ws.Cells(counter,3)
    appIE.document.getElementById("ntlogin" & index).Value = ws.Cells(counter,4)
  Next counter

  Set appIE = Nothing
  Set ws = Nothing

  MsgBox "All done - hit Submit if all OK!"

End Sub
Captain
  • 2,148
  • 15
  • 13
  • It's a starting point but I have no clue what you said :O I'll try to look at it but i have never played with excel – user237025 Oct 16 '14 at 22:34
  • @user237025 - ahha! It will be a good intro for a newbie :-) Is the website public and can you share more of your example data... I can then give you a more complete example! – Captain Oct 17 '14 at 06:55
  • No it's a internal website but I can give you more of of an example http://pastebin.com/AaWCZUJe – user237025 Oct 18 '14 at 22:57
  • It's copying but it's slightly wrong and that is my fault... I gave you A1 A2 A3 and it should have been A1 B1 C1 D1 then A2 B2 C2 D2 etc etc – user237025 Nov 01 '14 at 21:02
  • It worked last week but this week i'm getting the object invoked has disconnected from it's clients – user237025 Nov 08 '14 at 18:44
  • What code line is it crashing on? And what exact message? Any changes in code or environment since last week? – Captain Nov 09 '14 at 09:08
  • When i click on debug it's this line appIE.document.getElementById("firstname" & index).Value = ws.Cells(1, counter) that has the arrow next to it. Nothing has changed to my knowledge – user237025 Nov 13 '14 at 17:24
  • Googling for that error comes up with all sort of strange causes... Does the page load OK before `MsgBox("Make sure the page has loaded...`? There seem to be comments about resolving addresses and security certificates - but both should be fine if the page is properly loaded before you click to continue... One "solution" on this website has to use `Set appIE = New InternetExplorerMedium` (http://stackoverflow.com/questions/12965032/excel-vba-controlling-ie-local-intranet) – Captain Nov 14 '14 at 07:43
  • I did that change and it worked. I hope it all works ok tomorrow lol which is when i actually us it. – user237025 Nov 14 '14 at 20:49