Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50094

Retain VBE Reference Library Globally

$
0
0
Attempting to set a reference to the Microsoft Visual Basic for Applications Extensibility 5.3 per C. Pearson instructions RE: using vba to write to create an event procedure in another VBProject. Upon re-open (of base workbook, or even blank workbook), the reference is gone.

Quote:

In order to use the code on this page in your projects, you must change two settings.

First, you need to set an reference to the VBA Extensibility library. The library contains the definitions of the objects that make up the VBProject. In the VBA editor, go the the Tools menu and choose References. In that dialog, scroll down to and check the entry for Microsoft Visual Basic For Applications Extensibility 5.3. If you do not set this reference, you will receive a User-defined type not defined compiler error.


Next, you need to enable programmatic access to the VBA Project. In Excel 2003 and earlier, go the Tools menu (in Excel, not in the VBA editor), choose Macros and then the Security item. In that dialog, click on the Trusted Publishers tab and check the Trust access to the Visual Basic Project setting.

In Excel 2007, click the Developer item on the main Ribbon and then click the Macro Security item in the Code panel. In that dialog, choose Macro Settings and check the Trust access to the VBA project object model.
I can verify that I have completed both steps above, and yet I am met with a User-Defined Type Not Available at the line highlighted below:
Code:

Sub AddToThisWB()

Dim VBProj As VBIDE.Project
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Dim LineNum As Long

Set VBProj = ActiveWorkbook.VBProject
Set VBComp = VBProj.VBComponents("ThisWorkbook")
Set CodeMod = VBComp.CodeModule

    With CodeMod
        LineNum = .CreateEventProc("Open", "Workbook")
        LineNum = LineNum + 1
        .InsertLines LineNum, "Call Test"
    End With
   
End Sub

As a test and in a blank workbook with no others open, I set the reference, saved and closed. Upon re-open, the reference is again inactive.

Viewing all articles
Browse latest Browse all 50094

Trending Articles