2016-04-11 21 views
0

How to search through VBA code files numaralı bağlantıda oldukça kapsamlı bir yanıt okuyorum ve geçerli proje için gayet iyi çalışıyor. Bununla birlikte, sadece diğer projeleri açarken ve kodlarını incelerken yavaş hissediyorum.Diğer Erişim Veritabanlarındaki VBA kodu dosyalarında arama

Yanıt, OpenDatabase kullanarak belirtilen ancak veritabanı ile Application.VBE.ActiveVBProject arasındaki ilişkiyi göremiyorum. Bu konuda tembel olmadım, ancak web'de arama yapmaktan 4 gün sonra seçeneklerimi tüketti.

Herhangi bir yardım gerçekten takdir edilecektir.

cevap

1

Özür dilerim. Bu işi yapmanın başka bir yolunu buldum.

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 
0

Muhtemelen accApp.Quit eklemek gerekir:

accApp.CloseCurrentDatabase 
accApp.Quit 
Set accApp = Nothing 

önce Seti accApp = uygulamayı kapatmadan hızlandırmak ve on line, bu kodu (Ortak alt FindWordInOtherModules) yürütülürken kapatmak için hiçbir şey accApp.Quit, daha sonra değil. Bilgisayarımda, accApp.Quit eklendiyse, bu tür bir Sub uygulamasının çalıştırılmasından birkaç saniye sonra fare hala aktif değil.

Fakat geçerli veritabanı "geçici başvuru oluşturarak kendisine 'bağlantılı olabilir çünkü başka veritabanını açmaya gerek yoktur: Bu, başka bir veritabanı dosyası açılırken daha hızlı sonra olmak görünüyor

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 

ama VBA güncellenmiş edilemez. ... References.Remove ref referansını kaldırır, ancak VBA klasörler hala sol panelde görülebilir ve tüm kod biraz rahatsız edici olanı, işler

Application.VBE.VBProjects.Remove VBProj çalışmıyor

. bir şeyler gerekebilir Trust'ta "VBA proje nesne modeline güven erişim" seçeneğiyle Merkezi - Access'te bulunmayan Makro Ayarları. Ancak, veritabanını kapatıp açtıktan sonra proje görünmez.