【问题标题】:Search through VBA code files in other Access Databases在其他 Access 数据库中搜索 VBA 代码文件
【发布时间】:2016-04-11 20:30:29
【问题描述】:

我阅读了链接How to search through VBA code files 中的一个非常详尽的回复,它对当前项目非常有效。但是,我只是觉得打开其他项目并查看他们的代码很慢。

使用 OpenDatabase 提到的响应,但我没有看到有关数据库和 Application.VBE.ActiveVBProject 之间关联的示例。我对此并不懒惰,但 4 天的网络搜索已经用尽了我的选择。

任何帮助将不胜感激。

【问题讨论】:

    标签: ms-access vba ms-access-2013


    【解决方案1】:

    我很抱歉。找到了其他方法来完成这项工作。

    Public Sub FindWordInOtherModules(ByVal pSearchWord As String, sApplicationFilePath As String)
    Dim objComponent As VBComponent
        ' VBComponent requires reference to Microsoft Visual Basic
        ' for Applications Extensibility; use late binding instead:
    Dim lStartLine As Long
    Dim lEndLine As Long
    Dim lStartColumn As Long
    Dim lEndColumn As Long
    Dim accApp As Access.Application
    
    Set accApp = New Access.Application
    
    With accApp
        .Visible = True
        .OpenCurrentDatabase (sApplicationFilePath)
        .UserControl = True
        'MsgBox .VBE.ActiveVBProject.VBComponents.Count
        'MsgBox .CurrentDb.Name
    
        For Each objComponent In .VBE.ActiveVBProject.VBComponents
            If objComponent.CodeModule.Find(pSearchWord, lStartLine, lStartColumn, lEndLine, lEndColumn, _
            FindWholeWord, MatchCase, PatternSearch) = True Then
               MsgBox "Found text " & StringToFind & vbCrLf _
                   & "Start line: " & lStartLine & vbCrLf _
                   & "Line text:  " & objComponent.CodeModule.Lines(lStartLine, lEndLine - lStartLine + 1), vbOKOnly, objComponent.CodeModule.Name
        End If
    Next objComponent
    End With
    
    accApp.CloseCurrentDatabase
    Set accApp = Nothing
    
    End Sub
    

    【讨论】:

      【解决方案2】:

      您可能应该添加 accApp.Quit:

      accApp.CloseCurrentDatabase
      accApp.Quit
      Set accApp = Nothing
      

      before Set accApp = Nothing 以加快关闭应用程序并在执行此代码(Public Sub FindWordInOtherModules)期间关闭它,在 accApp.Quit 行上,而不是稍后。如果没有添加 accApp.Quit,在我的计算机上执行这种 Sub 几秒钟后鼠标仍然处于非活动状态。

      但是没有必要打开另一个数据库,因为当前数据库只能通过创建临时引用来“链接”到它:

      Private Sub FindWordInOtherModules2()
      
      Dim objComponent As VBComponent
      ...
      ...
      Dim lEndColumn As Long
      
      Dim ref As Reference
      Dim RefName As String
      
      Const FileName = "C:\Users\....mdb"
      
      With Application 'instead of accApp
      
          .References.AddFromFile FileName
          '.References.Count because the new one is supposed be the last one (?)
          RefName = .References(.References.Count).Name 
      
          Dim VBProj As VBProject
      
          For Each VBProj In .VBE.VBProjects
              If VBProj.FileName <> .CurrentDb.Name Then Exit For
          Next
      
          For Each objComponent In VBProj.VBComponents
              'Debug.Print objComponent.Name
              ...
              ...
          Next
      
          Set objComponent = Nothing '?
          Set VBProj = Nothing '?
      
          Set ref = .References(RefName)
          .References.Remove ref
          Set ref = Nothing '??
      
      End With
      
      End Sub
      

      这似乎比打开另一个数据库文件要快,但无法更新 VBA。 References.Remove ref 删除了引用,但 VBA 文件夹在左侧面板中仍然可见,并且所有代码都有效,有点令人不安......

      Application.VBE.VBProjects.Remove VBProj 不起作用。它可能与信任中心 - 宏设置中的“信任对 VBA 项目对象模型的访问”选项有关,该选项在 Access 中不可用。 但是关闭和打开数据库后项目是不可见的。

      【讨论】:

        猜你喜欢
        • 2012-04-24
        • 2015-02-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多