Convert old excel macro to selenium

zios007

New Member
Joined
Jul 14, 2022
Messages
14
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi everyone,

I'm completely new to VBA and since IE has ended it's lifecycle support our company will remove it from our OS soon for security reasons. I've had a working macro to copy data from an excel file to our website that contained some sort of form field structure.
I'm trying to convert the below set of instructions to work with selenium. But I've not been able to do it since I do not know how to work with WebDriver, WebElements nor VBA.

Few notes:
To be able to use the FindElementById I've had to use the switchtoframe -> cd.SwitchToFrame ("ptifrmtgtframe") because otherwise it wouldn't find the IDs.
I will need to clear the input box before entering data, otherwise it will copy it next to the text that was already on the box, something like this ->
'Clear the input box to enter new text
cd.FindElementById("BUSINESS_UNIT$" & i - 2).Clear

The steps of my code will need to change slightly from the previous code because the IE code didn't need to open a new browser instance.

- Open new instance of Chrome (even thought that I would prefer if we navigate to the exact site on the website and the code can run from there, but seems that I need to run a new chrome instance with selenium each time I want to use this macro).
- Because we need to open a new instance, we need first to login to the site each time the macro is used. So before proceeding, I'm checking if the tab name is "Create/Update Journal Entries". If it matches, that will mean that the user logged in.
If better way doing the check than using a GoTo... in case the tab name doesn't match. Please change it :) I didn't know how to do the loop and the GoTo worked (I know it is not good practice, but I'm not an expert sorry).
- Switch to proper iFrame to be able to find find elements by ID, otherwise it doesn't find anything.
- I guess some variables will need to be setup.
- Then the For Each loop

This is how the site looks like:

1.PNG

We have to add as many lines as our worksheet has rows with data. So with more rows will look like this:

2.PNG


That is why the loop, to cycle to each row on the excel sheet and copy the data to this form with input boxes. And that is why the ID will need to have a variable "BUSINESS_UNIT$" & i - 2 because it increases for each line.

Line 1 will be BUSINESS_UNIT$0
Line 2 will be BUSINESS_UNIT$1

The same applies for the other web elements by ID

Also as you can see there are by default some fields that have text already (Unit and Currency), but in the excel sheet the data may change, so the input box needs to be cleared before hand and then copy whatever is on the excel sheet.

VBA Code:
Sub JournalIE()

Dim IE As InternetExplorer
Dim ieDoc As HTMLDocument
Dim tbxNameFld As HTMLInputElement
Dim winShell As New ShellWindows

For Each IE In winShell
    If IE.LocationURL Like "*https://website/*" Then
        Set ieDoc = IE.document
        Set frm = ieDoc.frames(0).document.forms
        For i = 2 To Range("G65536").End(xlUp).Row
            frm(0).all.Item("BUSINESS_UNIT$" & i - 2).Value = Cells(i, 7).Value
            If frm(0).all.Item("ACCOUNT$" & i - 2).Value = "" Then frm(0).all.Item("ACCOUNT$" & i - 2).Value = Cells(i, 8).Value
            If frm(0).all.Item("DEPTID$" & i - 2).Value = "" Then frm(0).all.Item("DEPTID$" & i - 2).Value = Cells(i, 9).Value
            If frm(0).all.Item("PRODUCT$" & i - 2).Value = "" And Cells(i, 10).Value <> "" Then frm(0).all.Item("PRODUCT$" & i - 2).Value = Cells(i, 10).Value
            If frm(0).all.Item("PROJECT_ID$" & i - 2).Value = "" And Cells(i, 11).Value <> "" Then frm(0).all.Item("PROJECT_ID$" & i - 2).Value = Cells(i, 11).Value
            If frm(0).all.Item("FOREIGN_CURRENCY$" & i - 2).Value = "" Then frm(0).all.Item("FOREIGN_CURRENCY$" & i - 2).Value = Cells(i, 12).Value
            If frm(0).all.Item("FOREIGN_AMOUNT$" & i - 2).Value = "" Then frm(0).all.Item("FOREIGN_AMOUNT$" & i - 2).Value = Replace(Cells(i, 13).Value, ".", ",")
            If frm(0).all.Item("JRNL_LN_REF$" & i - 2).Value = "" Then frm(0).all.Item("JRNL_LN_REF$" & i - 2).Value = Replace(Cells(i, 14).Value, ".", ",")
            frm(0).all.Item("LINE_DESCR$" & i - 2).Value = Cells(i, 15).Value
        Next i
        Exit For
    End If
Next IE

End Sub

This is the code I've prepared, but definitely isn't even close to achieving anything... that's where I need some help. I'm sharing it to see if it helps a little bit.

VBA Code:
Sub JournalChrome()

    Dim cd As New WebDriver
   
    cd.Start "chrome", ""
    cd.Get "https://website"
 
   'Verify browser Title
  'If Title doesn't match, come back here and check again.
SiteCheck:
        Dim SearchTitle As Selenium.WebElement

        If cd.Window.Title = "Create/Update Journal Entries" Then

         'Switch to inner iFrame to be able to search inside for IDs
        cd.SwitchToFrame ("ptifrmtgtframe")
       
         'Set variables:
''''' Some variables will need to be setup to make the loop work? '''''

''''' HERE should go the For Each loop '''''        
For Each cd In "I DO NOT KNOW WHAT GOES HERE"
        For i = 2 To Range("G65536").End(xlUp).Row
''''' Example of finding element by ID-> cd.FindElementById("BUSINESS_UNIT$" & i - 2)

              Next i
       Exit For
    End If
Next cd

        MsgBox "You need to login"
        GoTo SiteCheck

End Sub

As you may see in the code that I've been writing.. I'm quite lost. I've been finding pieces, but I do not know how to end the puzzle and how some pieces go with others.

If you can help me, I will appreciate it eternally.

Thank you very much.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

Forum statistics

Threads
1,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

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