+ Reply to Thread
Results 1 to 7 of 7

add a reference to Microsoft Visual Basic for Applications Extensibility" at run time

Hybrid View

  1. #1
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    add a reference to Microsoft Visual Basic for Applications Extensibility" at run time

    Hi
    I've been on the WEB most of the day trying to figure this out. Is there code to "add a reference to Microsoft Visual Basic for Applications Extensibility" at run time.
    I have this code that seems to require such a reference
    ' Modified from http://www.cpearson.com/excel/vbe.aspx
    Public Sub DoModule8() 
    Dim VBProj As VBIDE.VBProject
    Dim VBComp As VBIDE.VBComponent
    Dim CodeMod As VBIDE.CodeModule
    Dim LineNum As Long
    Const DQUOTE = """" ' one " character
    Set VBProj = ActiveWorkbook.VBProject
    Set VBComp = VBProj.VBComponents("Sheet1")
    Set CodeMod = VBComp.CodeModule
    With CodeMod
    LineNum = .CreateEventProc("Activate", "Worksheet")
    LineNum = LineNum + 1
    .InsertLines LineNum, "Selection.OnAction = " & DQUOTE & "DoModule5"
    .InsertLines LineNum, "ActiveSheet.Shapes(" & DQUOTE & "Button " & 1 & DQUOTE & ").Select"
    .InsertLines LineNum, "Selection.OnAction = " & DQUOTE & "DoModule1"
    .InsertLines LineNum, "ActiveSheet.Shapes(" & DQUOTE & "Button " & 2 & DQUOTE & ").Select"
    End With
    End Sub
    And it results in this (after I set the reference manually)
    Option Explicit
    Private Sub Worksheet_Activate()
        ActiveSheet.Shapes("Button 2").Select
        Selection.OnAction = "DoModule1"
        ActiveSheet.Shapes("Button 1").Select
        Selection.OnAction = "DoModule5"
    End Sub
    Can I set the Reference pragmatically?
    As always, your help is deeply appreciated.
    John
    Last edited by jaslake; 08-18-2010 at 03:23 PM.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: add a reference to Microsoft Visual Basic for Applications Extensibility" at run

    If you add the reference manually (Tools -> References) when the file is distributed then assuming the same library is available on the other clients the library will be available.

    You can add references via the Workbook Open event but unless there are version specific issues with the library itself it's often not necessary.

  3. #3
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,021

    Re: add a reference to Microsoft Visual Basic for Applications Extensibility" at run

    You could also just late bind and not worry about the reference.
    Everyone who confuses correlation and causation ends up dead.

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: add a reference to Microsoft Visual Basic for Applications Extensibility" at run

    Hi DonkeyOte

    This is REALLY what I am asking
    You can add references via the Workbook Open event
    I was all over the Internet for hours looking for code to do just that. Alas, to know avail. Any idea what the code is to do that?

    Hi romperstomper

    I solved the issue by
    just late bind and not worry about the reference
    Thanks to both of you. I'll leave this as unsolved for a few days to see if I get feedback on the code question.

    John

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: add a reference to Microsoft Visual Basic for Applications Extensibility" at run

    You can develop early-bound, test both early- and late-bound, and then leave it whichever way you like. For example,

    #Const EarlyBound = True
    
    Function Unique(r As Range, _
                    Optional iMode As Long = 0, _
                    Optional bCaseSensitive As Boolean = False, _
                    Optional sSep As String = " ") As Variant
    
        ' blah, blah, blah ...   
    
        #If EarlyBound Then
            Dim dic     As Scripting.Dictionary
            Set dic = New Scripting.Dictionary
        #Else
            Dim dic     As Object
            Set dic = CreateObject("Scripting.Dictionary")
        #End If
    
        With dic
            ' ...
    That way you get IntelliSense during development.
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,021

    Re: add a reference to Microsoft Visual Basic for Applications Extensibility" at run

    Programmatically it would be:
        ThisWorkbook.VBProject.References.AddFromGuid "{0002E157-0000-0000-C000-000000000046}", 5, 3
    for example.

  7. #7
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: add a reference to Microsoft Visual Basic for Applications Extensibility" at run

    Hi romperstomper

    After your last post, I searched on
    VBProject.References.AddFromGuid
    and found a wealth of information. Just didn't know what to search for. Thanks for that!

    John

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1