Trick to use Selenium without installation

6diegodiego9

Board Regular
Joined
Jan 9, 2018
Messages
80
Office Version
  1. 2016
Platform
  1. Windows
I use the following trick to use Selenium in my VBA code without requiring installation nor registration of the Selenium files on the target PCs:

VBA Code:
Function CreateObjectSelenium(strTypeName As String) As Object
'Required references: Mscoree (taken from C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscoree.tlb) and mscorlib
    Static domain As mscorlib.AppDomain
    If domain Is Nothing Then
        With New mscoree.CorRuntimeHost
            .Start:
            .GetDefaultDomain domain
        End With
    End If
    Set CreateObjectSelenium = domain.CreateInstanceFrom(ThisWorkbook.path & "\SeleniumBasic\Selenium.dll", strTypeName).Unwrap
End Function

Sub esempio()
    Dim oEdge As Object 'Selenium.EdgeDriver
    Set oEdge = CreateObjectSelenium("Selenium.EdgeDriver")
    With oEdge
        .Start "edge"
        .Get "https://www.esempio.it"
        msgbox "pausa"
        .Quit
   End With
End Sub

Now, as an additional optimization step I'd like to convert the code inside CreateObjectSelenium to only use late bindings, so that you don't need to set the two references when copying the code from my message(s), and as "best practice" of converting everything to "late binding" when deploying code to public.

However my first attempt to convert that to late binding using the usual "as object" and "createObject("xxxx.yyyyyy") failed with unexpected errors.
It appears that "as mscorlib.AppDomain" and "as mscoree.CorRuntimeHost" could not be simply substituted by "an object" or "as variant".

Any ideas about how to convert those to late bindings?
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
For example, even with the two References enabled, I get "Type Mismatch" error on these two lines:

1649613544947.png
 
Upvote 0

Forum statistics

Threads
1,215,327
Messages
6,124,280
Members
449,149
Latest member
mwdbActuary

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