tags:

views:

63

answers:

1

I'm deploying an early bound styled VBA module that needs Scripting.Dictionary and RegExp.

The script, predictably, fails when it runs on another computer. The user has to go to Tools->Reference in the VBA IDE and add a reference to those two libraries manually to make it work.

Hence lies the problem. Asking the non-technical end user to go to the IDE and manually add references is asking way too much of them.

The other alternative is to rewrite the whole (very long script written by someone else) to use late binding. I rather not take this path if there are other methods.

As an altervative, some people suggest adding a reference programatically like so:

Application.VBE.ActiveVBProject.References.AddFromFile [Path to library]

  1. Is this the correct solution and if so are there any downsides of this strategy?
  2. If not, are there other methods that will to enable the code to remain early bound yet does not require references to be added manually by the user.

Suggestions involving direct calls to the Win32/64 API are also welcome.

Thanks.

+1  A: 

In my own limited environment (small # of other people using spreadsheets I develop, relatively standard machine setups), if I create the file and add the references, and then give a copy to someone else, they can open it with no problems and not have to do anything, so keep that in mind with this answer. (I'm wondering why that doesn't work for you.) Also, this was with Excel.

Rather than adding a reference from a file path, you might consider using the GUID property instead.

Here is some code I once used to automatically create references in a newly created workbook. (It's part of a script that would export code, references, and unit tests on worksheets to text for use with Subversion and then later reconstitute the workbook from the text files.) You might find it useful to your situation. (EH and cleanup removed to keep it short...)

'Export refs in existing workbook to text file
Private Sub exportRefs_(srcWbk As Workbook)
    Dim fs As FileSystemObject
    Set fs = New FileSystemObject

    Dim tsout As TextStream
    Set tsout = fs.CreateTextFile(fs.BuildPath(getTargetPath_(srcWbk), "refs.refs"))

    Dim ref As Reference
    For Each ref In Application.ThisWorkbook.VBProject.References
        Call tsout.WriteLine(ref.GUID)
    Next ref

    '<EH + cleanup...>
End Sub


'Add refs to newly created workbook based on previously exported text file
Private Sub importRefs_(wbk As Workbook, path As String)
    Dim fs As FileSystemObject
    Set fs = New FileSystemObject

    Dim tsin As TextStream
    Set tsin = fs.OpenTextFile(path)

    Dim line As String
    Dim ref As Reference

    While Not tsin.AtEndOfStream
        line = tsin.ReadLine()

        Set ref = Nothing

        On Error Resume Next
            Set ref = wbk.VBProject.References.AddFromGuid(line, 0, 0)
        On Error GoTo 0

        If ref Is Nothing Then
            Debug.Print "add failed: " & line
        End If
    Wend

    '<EH + cleanup...>
End Sub

Like, I said, limited environment, but hopefully it helps.

jtolle
@jtolle : Looks interesting. I'll try it out. Thanks. ;-)
GeneQ