How to Add/Remove reference programmatically?

前端 未结 2 1157
遇见更好的自我
遇见更好的自我 2020-12-08 12:09

My Application is built to a scan MS Access database in VB.NET.

When the Access application is distributed to end users, they may have different versions of COM comp

2条回答
  •  难免孤独
    2020-12-08 12:54

    Here is some sample code:

    Create Reference from File

      Sub AddWS()
      'Create a reference to Windows Script Host, '
      'where you will find FileSystemObject ' 
      'Reference name: "IWshRuntimeLibrary" '
      'Reference Name in references list: "Windows Script Host Object Model" '
      ReferenceFromFile "C:\WINDOWS\System32\wshom.ocx"
      End Sub
    
      Function ReferenceFromFile(strFileName As String) As Boolean
      Dim ref As Reference
    
             On Error GoTo Error_ReferenceFromFile
             References.AddFromFile (strFileName)
             ReferenceFromFile = True
    
      Exit_ReferenceFromFile:
             Exit Function
    
       Error_ReferenceFromFile:
             ReferenceFromFile = False
             Resume Exit_ReferenceFromFile
       End Function 
    

    Delete Reference

      Sub DeleteRef(RefName)
      Dim ref As Reference
    
          'You need a reference to remove '
          Set ref = References(RefName)
          References.Remove ref
    
      End Sub 
    
    
    You can use the references collection to find if a reference exists.
    

    Reference Exists

      Function RefExists(RefName)
      Dim ref As Object
    
         RefExists = False
    
         For Each ref In References
             If ref.Name = RefName Then
                 RefExists = True
             End If
         Next
    
      End Function
    

    From: http://wiki.lessthandot.com/index.php/Add,_Remove,_Check_References

    You may also wish to read http://www.mvps.org/access/modules/mdl0022.htm

提交回复
热议问题