如果您的兴趣是在Access数据库文件中搜索代码模块,则可以使用VBE对象模型。此示例在当前数据库的ActiveVBProject
的所有模块中搜索单词。如果数据库包括超过一个VBProject,您可以枚举VBProjects收集和按名称搜索项目一次一个:
For Each objComponent In Application.VBE.VBProjects(ProjName).VBComponents
或者如果你喜欢用数字来引用该项目,而不是名字,只是注意编号为1,而不是0
Public Sub findWordInModules(ByVal pSearchWord As String)
'Dim objComponent As VBComponent
' VBComponent requires reference to Microsoft Visual Basic
' for Applications Extensibility; use late binding instead:
Dim objComponent As Object
Dim strMessage As String
Dim strModuleList As String
strModuleList = vbNullString
For Each objComponent In Application.VBE.ActiveVBProject.VBComponents
If objComponent.CodeModule.Find(pSearchWord, 1, 1, -1, -1) = True Then
strModuleList = strModuleList & "; " & objComponent.Name
End If
Next objComponent
strMessage = "Text '" & pSearchWord & "' found in "
If Len(strModuleList) > 0 Then
strMessage = strMessage & "modules: " & Mid(strModuleList, 3)
Else
strMessage = strMessage & "no modules"
End If
Debug.Print strMessage
End Sub
查看该Find
方法访问帮助主题开始;你可能更喜欢不同的选择,而不是我用过
如果要定位多个db文件并搜索每个文件中的模块,可以使用OpenDatabase
方法自动执行此操作。我将把这部分的细节留给你。
没有,如果它在接取相同的,但这里的想法就是我会做这在Excel中:http://www.cpearson.com/Excel/vbe.aspx – 2012-04-12 15:13:09
来看看这CodeProject上项目点子 - http://www.codeproject.com/Articles/18029/SourceTools-xla。查看保存项目功能以及它们如何解析vba项目中的所有源文件。 – ja72 2012-04-12 19:12:56
@ ja72如果这个add-inn可以用于Access,或者我可以修改它,那么如果我们见面,我会给你一个双喜。 – 2012-04-12 19:35:30