Option Compare Database

Option Private Module
Option Explicit


' Import References from a CSV, true=SUCCESS
Public Function ImportReferences(ByVal obj_path As String) As Boolean
    Dim FSO As Object
    Dim InFile As Object
    Dim Line As String
    Dim item() As String
    Dim GUID As String
    Dim Major As Long
    Dim Minor As Long
    Dim filename As String
    Dim refName As String
    Dim count As Integer
    count = 0
    filename = dir$(obj_path & "references.csv")
    If Len(filename) = 0 Then
        ImportReferences = False
        Exit Function
    End If
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set InFile = FSO.OpenTextFile(obj_path & filename, iomode:=ForReading, create:=False, Format:=TristateFalse)
    
On Error GoTo failed_guid
    Do Until InFile.AtEndOfStream
        Line = InFile.readline
        item = Split(Line, ",")
        If UBound(item) = 2 Then 'a ref with a guid
          GUID = Trim$(item(0))
          Major = CLng(item(1))
          Minor = CLng(item(2))
          Application.References.AddFromGuid GUID, Major, Minor
          count = count + 1
        Else
          refName = Trim$(item(0))
          Application.References.AddFromFile refName
          count = count + 1
        End If
go_on:
    Loop
On Error GoTo 0
    InFile.Close
    Set InFile = Nothing
    Set FSO = Nothing
    logger "ImportReferences", "INFO", count & " imported from " & filename
    ImportReferences = True
    Exit Function
    
failed_guid:
    If err.number = 32813 Then
        'The reference is already present in the access project - so we can ignore the error
        Resume Next
    Else
        logger "ImportReferences", "ERROR", "Failed to register " & GUID
        Resume go_on
    End If
    
End Function

' Export References to a CSV
Public Sub ExportReferences(ByVal obj_path As String)
    Dim FSO As Object
    Dim OutFile As Object
    Dim Line As String
    Dim ref As Reference
    Dim count As Integer
    count = 0
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set OutFile = FSO.CreateTextFile(obj_path & "references.csv", overwrite:=True, unicode:=False)
    For Each ref In Application.References
        If ref.GUID <> vbNullString Then ' references of types mdb,accdb,mde etc don't have a GUID
            If Not ref.BuiltIn Then
                Line = ref.GUID & "," & CStr(ref.Major) & "," & CStr(ref.Minor)
                OutFile.WriteLine Line
                logger "ExportReferences", "DEBUG", "> Reference " & Line & " exported"
                count = count + 1
            End If
        Else
            Line = ref.FullPath
            OutFile.WriteLine Line
            logger "ExportReferences", "DEBUG", "> Reference " & Line & " exported"
            count = count + 1
        End If
    Next
    OutFile.Close
    
    logger "ExportReferences", "INFO", count & " references exported to" & obj_path & "references.csv"
End Sub