Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I programatically add a reference to a VBA project?

Tags:

vba

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.

like image 567
GeneQ Avatar asked Oct 01 '10 08:10

GeneQ


People also ask

How do I add a reference library in VBA?

In Microsoft Excel, you can add a library reference to the VBA project by clicking the Tools > References… manually. It will open the following dialog box which will help you to select from existing references or browse your library yourself.

How do I reference in VBA?

If the Excel VBA Range object you want to refer to is a single cell, the syntax is simply “Range(“Cell”)”. For example, if you want to make reference to a single cell, such as A1, type “Range(“A1″)”.

How do I add a missing reference in VBA?

Press ALT+F11 to open the Visual Basic Editor. On the Tools menu, click References. Click to clear the check box for the type library or object library marked as MISSING: <referencename>.


1 Answers

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.

like image 66
jtolle Avatar answered Oct 17 '22 12:10

jtolle