Question

Okay so I'm trying to package a DLL being used by my spreadsheet with the excel file. My spreadsheet will be copied to different PC's and used from different places and I'm trying to avoid having the user run regasm every time the worksheet (with the DLL) is copied to a new PC. I've resorted to doing a shell command to register the DLL with regasm on startup. Here is my code:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim strWinCmd As String
    Dim retVal As Double
    strWinCmd = "cmd.exe %SystemRoot%\Microsoft.NET\Framework\v2.0.50727\regasm.exe /u /codebase /tlb """ & Application.ActiveWorkbook.Path & "\JART\JART.dll"""
    retVal = Shell(strWinCmd, vbHide)
End Sub

Private Sub Workbook_Open()
    Dim strWinCmd As String
    Dim retVal As Double
    strWinCmd = "cmd.exe /c %SystemRoot%\Microsoft.NET\Framework\v2.0.50727\regasm.exe /codebase /tlb """ & Application.ActiveWorkbook.Path & "\JART\JART.dll"""
    Call Shell(strWinCmd, vbNormalFocus)

    Call Button_Handlers.Sleep(1500)

    Call Button_Handlers.Initialize
End Sub

For reference the Button_Handlers.Sleep just calls the system sleep method and Button_Handlers.Initialize does this:

Sub Initialize()
    'This JART.MainJobControl is the main class in the JART DLL
    Set JART_Instance = New JART.MainJobControl
    'Use JART_Instance
End Sub

So basically I'm trying to register the DLL at start-up and un-register it on close. My problem is that when I open the spreadsheet on a new PC I get an error in Button_Handlers.Initialize. It tells me that I'm trying to use an undefined class (JART.MainJobControl), as if the DLL wasn't referenced. If I try to reopen the file everything works fine???

The way I'm doing this is manually adding the reference to the DLL on a machine that already has it registered with regasm. I then save this excel file and transport it to a machine that hasn't had the DLL registered and try to open it and run it. I think that since the reference is already added to the excel file, all's the code has to do is register it with regasm. Does anyone know why this wouldn't work? Am I not sleeping long enough.

Was it helpful?

Solution

Well I just ended up working around the quirk I mention in the question. I had to move the initialize into a different method so that it is not called within the same action as the regasm. This seems to work well. I haven't had any trouble with it yet.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top