This procedure, AddAllNeededReferences, will dynamically add references to VBA, STDOLE, DAO, Excel, Word and Office libraries. If you are writing an application that needs to interact with MS Excel, Word, enter data into MS Access tables using SQL and VBA – you can use this procedure to create references programmatically.
Public Sub AddAllNeededReferences()
'Name: VBA
'GUID: {000204EF-0000-0000-C000-000000000046}
Call addObjectLibraryReference("{000204EF-0000-0000-C000-000000000046}")
'Name: Access
'GUID: {4AFFC9A0-5F99-101B-AF4E-00AA003F0F07}
Call addObjectLibraryReference("{4AFFC9A0-5F99-101B-AF4E-00AA003F0F07}")
'Name: stdole
'GUID: {00020430-0000-0000-C000-000000000046}
Call addObjectLibraryReference("{00020430-0000-0000-C000-000000000046}")
'Name: DAO
'GUID: {4AC9E1DA-5BAD-4AC7-86E3-24F4CDCECA28}
Call addObjectLibraryReference("{4AC9E1DA-5BAD-4AC7-86E3-24F4CDCECA28}")
'Name: Excel
'GUID: {00020813-0000-0000-C000-000000000046}
Call addObjectLibraryReference("{00020813-0000-0000-C000-000000000046}")
'Name: Word
'GUID: {00020905-0000-0000-C000-000000000046}
Call addObjectLibraryReference("{00020905-0000-0000-C000-000000000046}")
'Name: Office
'GUID: {2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}
Call addObjectLibraryReference("{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}")
End Sub
Sub addObjectLibraryReference(strGUID As String)
'Dim strGUID As String
'Microsoft Visual Basic For Application Extensibility GUID
'strGUID = "{0002E157-0000-0000-C000-000000000046}"
'Check if reference is already added to the project, if not add it
If F_isReferenceAdded(strGUID) = False Then
Application.References.AddFromGuid strGUID, 0, 0
End If
End Sub
Function F_isReferenceAdded(referenceGUID As String) As Boolean
Dim varRef As Variant
'Loop through VBProject references if input GUID found return TRUE otherwise FALSE
For Each varRef In Application.References
If varRef.GUID = referenceGUID Then
F_isReferenceAdded = True
Exit For
End If
Next varRef
End Function
This is a complimentary article written by the MAARS team for the MAARS user community. Code in this article drives the operation of MAARS (MS Access Application wRiting Software). MAARS is an intelligent automation program that speeds up MS Access Application Development by 10x, 20x or 100x times. To learn more about MAARS, click here.
Disclaimer:
Some information included in this article may have been sourced from other publicly available websites and blogs. In such cases, credit goes to those authors for the original ideas and thoughts, but we do take credit for putting valuable information together and improve the efficiency of other office developers.