0
在vba中,如何調用word文檔並獲取它包含的所有宏的列表?返回所有宏的列表
在vba中,如何調用word文檔並獲取它包含的所有宏的列表?返回所有宏的列表
這篇文章將告訴你如何做到這一點(它是Word 2000中,但我認爲有它會爲你使用任何版本工作的好機會):WD2000: Sample Macro to Return Macro and Procedure Names
這是那篇文章的代碼:
Sub ListAllMacroNames()
Dim pj As VBProject
Dim vbcomp As VBComponent
Dim curMacro As String, newMacro As String
Dim x As String
Dim y As String
On Error Resume Next
curMacro = ""
Documents.Add
For Each pj In Application.VBE.VBProjects
x = pj.FileName
y = pj.Protection
If x <> "" Then
If y <> "1" Then
Selection.InsertAfter "The " & Chr(34) & x & Chr(34) & _
" project contains " & "the following macro names:" & vbCr
Selection.InsertAfter vbCr
For Each vbcomp In pj.VBComponents
For i = 1 To vbcomp.CodeModule.CountOfLines
newMacro = vbcomp.CodeModule.ProcOfLine(Line:=i, _
prockind:=vbext_pk_Proc)
If curMacro <> newMacro Then
curMacro = newMacro
If curMacro <> "" And curMacro <> "app_NewDocument" Then
Selection.InsertAfter newMacro & vbCr
Selection.Collapse wdCollapseEnd
End If
End If
Next
Next
Else
Selection.InsertAfter "The project " & Chr(34) & x & Chr(34) & _
" is locked. " & "Macros in this locked project will not be" _
& " listed." & vbCr & vbCr
End If
Selection.InsertAfter vbCr
End If
x = ""
Next
Selection.Collapse wdCollapseEnd
End Sub
即使在代碼的第一行,vba編輯器也會抱怨。我討厭這個東西tbh:D – tom 2010-10-13 11:07:01
@tom:你是否在該鏈接中提到了「Microsoft Visual Basic for Applications Extensibility」的引用?我只將代碼複製到我的答案中,而不是其他步驟。 – 2010-10-13 11:12:01
是的,我已經有了這個設置。 – tom 2010-10-13 11:29:25