最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Trouble building a DLL in Visual Studio that I can use as a Microsoft Access reference without a "Can't add a r

programmeradmin3浏览0评论

I am trying to write code that goes to a title company website enter the information and return the title fee quote.

I am writing the code in Visual Studio using Visual Basic and the nuget selenium packages.

I can run the code from a console window in Visual Studio and it works perfectly but when I create the dll and then try to use it as a reference in Microsoft Access, I get an error

Can't add a reference to the specified file

I have tried copying all the dependencies into the same folder as the dll which is in the same folder as my Microsoft Access database.

Can anybody point me in the right direction as to what I have to do? I have tried different .NET versions, I have compatibility checked, and I have built the dll as an administrator by opening visual studio in administrator mode.

Thank you.

  • if get a build error then need to open the file as administrator and then build it
  • if rebuilding then delete the bin file where it currently is
Imports OpenQA.Selenium
Imports OpenQA.Selenium.Edge
Namespace TitleFeeCalculatorNamespace
    Public Class TitleFeeCalculator
        Public Function returnTitleFee(curCostofProperty As String, curMortgageAmount As String) As String
            ' Set up the Edge WebDriver
            Dim options As New EdgeOptions()
            options.AddArgument("headless") 'so opens in the background
            Dim driver As IWebDriver = New EdgeDriver(options)

            Try
                ' Navigate to the Title Fees Calculator form
                driver.Navigate().GoToUrl(".Site/Frontend/Calculators/TitleCalculator.aspx?state=NJ")

                ' Wait for the page to load (adjust delay as needed)
                Threading.Thread.Sleep(500)

                ' Select "Residential" from the Property Type dropdown
                Dim propertyTypeDropdown As IWebElement = driver.FindElement(By.Id("ddlPropertyType"))
                Dim propertyTypeOption As IWebElement = propertyTypeDropdown.FindElement(By.XPath("//option[text()='Residential']"))
                propertyTypeOption.Click()

                ' Select "Basic" from the Transaction Type dropdown
                Dim transactionTypeDropdown As IWebElement = driver.FindElement(By.Id("ddlTransactionType"))
                Dim transactionTypeOption As IWebElement = transactionTypeDropdown.FindElement(By.XPath("//option[text()='Purchase']"))
                transactionTypeOption.Click()

                ' Enter the Purchase Amount
                Dim purchaseAmountField As IWebElement = driver.FindElement(By.Id("txtPurchaseAmt"))
                purchaseAmountField.Clear()
                purchaseAmountField.SendKeys(curCostofProperty)

                ' Enter the Mortgage Amount
                Dim mortgageAmountField As IWebElement = driver.FindElement(By.Id("txtMortgageAmt"))
                mortgageAmountField.Clear()
                mortgageAmountField.SendKeys(curMortgageAmount)

                ' Click the "MTA as closing agent" checkbox
                Dim mtaCheckbox As IWebElement = driver.FindElement(By.Id("chkMTAAgent"))
                If Not mtaCheckbox.Selected Then
                    mtaCheckbox.Click()
                End If

                ' Submit the form
                Dim submitButton As IWebElement = driver.FindElement(By.Id("btnSubmit"))
                submitButton.Click()

                ' Wait to observe the result (adjust delay as needed)
                Threading.Thread.Sleep(500)

                ' Extract the Total Fees from the result table
                Dim totalFeesElement As IWebElement = driver.FindElement(By.XPath("//tr[contains(., 'TOTAL FEES')]/td[2]"))
                Dim totalFees As String = totalFeesElement.Text
                Return totalFees

            Catch ex As Exception
                ' Handle any exceptions
                Console.WriteLine("An error occurred: " & ex.Message)
                Return "0"

            Finally
                ' Close the browser
                driver.Quit()
            End Try
        End Function
    End Class
End Namespace

I am trying to write code that goes to a title company website enter the information and return the title fee quote.

I am writing the code in Visual Studio using Visual Basic and the nuget selenium packages.

I can run the code from a console window in Visual Studio and it works perfectly but when I create the dll and then try to use it as a reference in Microsoft Access, I get an error

Can't add a reference to the specified file

I have tried copying all the dependencies into the same folder as the dll which is in the same folder as my Microsoft Access database.

Can anybody point me in the right direction as to what I have to do? I have tried different .NET versions, I have compatibility checked, and I have built the dll as an administrator by opening visual studio in administrator mode.

Thank you.

  • if get a build error then need to open the file as administrator and then build it
  • if rebuilding then delete the bin file where it currently is
Imports OpenQA.Selenium
Imports OpenQA.Selenium.Edge
Namespace TitleFeeCalculatorNamespace
    Public Class TitleFeeCalculator
        Public Function returnTitleFee(curCostofProperty As String, curMortgageAmount As String) As String
            ' Set up the Edge WebDriver
            Dim options As New EdgeOptions()
            options.AddArgument("headless") 'so opens in the background
            Dim driver As IWebDriver = New EdgeDriver(options)

            Try
                ' Navigate to the Title Fees Calculator form
                driver.Navigate().GoToUrl("https://www.anytitleco/Title.Site/Frontend/Calculators/TitleCalculator.aspx?state=NJ")

                ' Wait for the page to load (adjust delay as needed)
                Threading.Thread.Sleep(500)

                ' Select "Residential" from the Property Type dropdown
                Dim propertyTypeDropdown As IWebElement = driver.FindElement(By.Id("ddlPropertyType"))
                Dim propertyTypeOption As IWebElement = propertyTypeDropdown.FindElement(By.XPath("//option[text()='Residential']"))
                propertyTypeOption.Click()

                ' Select "Basic" from the Transaction Type dropdown
                Dim transactionTypeDropdown As IWebElement = driver.FindElement(By.Id("ddlTransactionType"))
                Dim transactionTypeOption As IWebElement = transactionTypeDropdown.FindElement(By.XPath("//option[text()='Purchase']"))
                transactionTypeOption.Click()

                ' Enter the Purchase Amount
                Dim purchaseAmountField As IWebElement = driver.FindElement(By.Id("txtPurchaseAmt"))
                purchaseAmountField.Clear()
                purchaseAmountField.SendKeys(curCostofProperty)

                ' Enter the Mortgage Amount
                Dim mortgageAmountField As IWebElement = driver.FindElement(By.Id("txtMortgageAmt"))
                mortgageAmountField.Clear()
                mortgageAmountField.SendKeys(curMortgageAmount)

                ' Click the "MTA as closing agent" checkbox
                Dim mtaCheckbox As IWebElement = driver.FindElement(By.Id("chkMTAAgent"))
                If Not mtaCheckbox.Selected Then
                    mtaCheckbox.Click()
                End If

                ' Submit the form
                Dim submitButton As IWebElement = driver.FindElement(By.Id("btnSubmit"))
                submitButton.Click()

                ' Wait to observe the result (adjust delay as needed)
                Threading.Thread.Sleep(500)

                ' Extract the Total Fees from the result table
                Dim totalFeesElement As IWebElement = driver.FindElement(By.XPath("//tr[contains(., 'TOTAL FEES')]/td[2]"))
                Dim totalFees As String = totalFeesElement.Text
                Return totalFees

            Catch ex As Exception
                ' Handle any exceptions
                Console.WriteLine("An error occurred: " & ex.Message)
                Return "0"

            Finally
                ' Close the browser
                driver.Quit()
            End Try
        End Function
    End Class
End Namespace
Share Improve this question edited Mar 31 at 6:27 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Mar 30 at 12:00 Emanuel LernerEmanuel Lerner 112 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

First, I would get a test .dll and test project working. Keep it super simple - say like some "Hello World" type of program.

First things first.

Create a new vb project, framework, NOT core.

Choose this type of project (a simple class project).

Hence this:

We can delete, or rename the default class - let's delete and create.

So, then:

Now, let's add a class called HelloTest

Hence:

We now have this:

OK, now we need to set the bit size of the project to match ms-access.

You do NOT want to use "Any CPU", but HAVE to match the bit version of MS-Access. Access now comes in 2 flavors, and you need to match that bit size. I'm using MS Access x32 bits, so then we need to change project settings:

Create a new config, and setup a x86 (32 bit project).

So, we now have this:

OK, we need a few more settings.

Under project settings, choose Application.

Then click on the "Assembly" button - this:

And then on compile tab, we need to check this box:

OK, now we can write some test code.

Imports System.Runtime.InteropServices

<ClassInterface(ClassInterfaceType.AutoDual)>
Public Class HelloTest

    Public Function HelloFromVB() As String

        Dim sTest As String = ""
        sTest = "This text is from vb" & vbCrLf &
                "Time from  = " & DateTime.Now.ToLongTimeString

        Return sTest

    End Function


End Class

Note in above, we do import Interop Services. And we also (in this case) to keep things nice and simple, let generate the com interface with AutoDual.

OK, that's it! If we build the project, it will create the .dll, and register it as a valid windows COM object.

So, after we build? Then say in Access, we now simply set a reference to the COM object from VBA. So, let's create/add a new code module in Access, and then add a reference to our library.

Hence from VBA choose tools->References.

And now select our COM object. We called it HelloWorldTest.

So, in references from VBA, it should appear in the list of choices.

Hence this:

OK, now we can use that COM object in VBA. So, say this test stub in our Module1 (standard VBA code module).

Note very closely how now even in VBA, we get IntelliSense, showing our public method (function).

So, we now have this code, and hitting F5 to run this code, we have this result:

Option Compare Database
Option Explicit

Sub MyComTest()


    Dim MyNetCode As New HelloWordTest.HelloTest
    
    MsgBox MyNetCode.HelloFromVB, vbInformation
    


End Sub

And hitting F5, then we see this:

So, before you do any kind of "inter op" from VBA to , it stands to reason that you want to get a sample and simple project working. Once you get above working?

Then you can add your existing VB code to above, or even start a new project.

Like all software endeavors? Break this down into smaller and smaller steps. This first step being to get a working COM object. Get a COM object working, test some code, and THEN start to progressively become more complex.

Last but not least?

On the computer you are working on, since you checked the box "register for COM interop"? All that setting does is execute a "regasm" post build. For that .dll to work on other computers, then you have to execute a regasm to register the .dll on each computer.

When you create the COM visible file it is also create a .tlb extension file with the same name.
When you reference it in Access you need to select the .tlb extension not the .dll.
This will make the reference for the file.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论