当前位置:  开发笔记 > 编程语言 > 正文

以编程方式从Word 2007文档中提取宏(VBA)代码

如何解决《以编程方式从Word2007文档中提取宏(VBA)代码》经验,为你挑选了2个好方法。



1> Jim Harte..:

您可以将代码导出到文件,然后再将其读回.

我一直在使用下面的代码来帮助我在源代码控制下保留一些Excel宏(使用Subversion和TortoiseSVN).它基本上在我打开VBA编辑器时保存所有代码到文本文件.我把文本文件放在subversion中,这样我就可以做差异了.您应该能够适应/窃取其中一些在Word中工作.

CanAccessVBOM()中的注册表检查对应于安全设置中的"对Visual Basic项目的信任访问".

Sub ExportCode()

    If Not CanAccessVBOM Then Exit Sub ' Exit if access to VB object model is not allowed
    If (ThisWorkbook.VBProject.VBE.ActiveWindow Is Nothing) Then
        Exit Sub ' Exit if VBA window is not open
    End If
    Dim comp As VBComponent
    Dim codeFolder As String

    codeFolder = CombinePaths(GetWorkbookPath, "Code")
    On Error Resume Next
    MkDir codeFolder
    On Error GoTo 0
    Dim FileName As String

    For Each comp In ThisWorkbook.VBProject.VBComponents
        Select Case comp.Type
            Case vbext_ct_ClassModule
                FileName = CombinePaths(codeFolder, comp.Name & ".cls")
                DeleteFile FileName
                comp.Export FileName
            Case vbext_ct_StdModule
                FileName = CombinePaths(codeFolder, comp.Name & ".bas")
                DeleteFile FileName
                comp.Export FileName
            Case vbext_ct_MSForm
                FileName = CombinePaths(codeFolder, comp.Name & ".frm")
                DeleteFile FileName
                comp.Export FileName
            Case vbext_ct_Document
                FileName = CombinePaths(codeFolder, comp.Name & ".cls")
                DeleteFile FileName
                comp.Export FileName
        End Select
    Next

End Sub
Function CanAccessVBOM() As Boolean
    ' Check resgistry to see if we can access the VB object model
    Dim wsh As Object
    Dim str1 As String
    Dim AccessVBOM As Long

    Set wsh = CreateObject("WScript.Shell")
    str1 = "HKEY_CURRENT_USER\Software\Microsoft\Office\" & _
        Application.Version & "\Excel\Security\AccessVBOM"
    On Error Resume Next
    AccessVBOM = wsh.RegRead(str1)
    Set wsh = Nothing
    CanAccessVBOM = (AccessVBOM = 1)
End Function


Sub DeleteFile(FileName As String)
    On Error Resume Next
    Kill FileName
End Sub

Function GetWorkbookPath() As String
    Dim fullName As String
    Dim wrkbookName As String
    Dim pos As Long

    wrkbookName = ThisWorkbook.Name
    fullName = ThisWorkbook.fullName

    pos = InStr(1, fullName, wrkbookName, vbTextCompare)

    GetWorkbookPath = Left$(fullName, pos - 1)
End Function

Function CombinePaths(ByVal Path1 As String, ByVal Path2 As String) As String
    If Not EndsWith(Path1, "\") Then
        Path1 = Path1 & "\"
    End If
    CombinePaths = Path1 & Path2
End Function

Function EndsWith(ByVal InString As String, ByVal TestString As String) As Boolean
    EndsWith = (Right$(InString, Len(TestString)) = TestString)
End Function



2> Aardvark..:

您将不得不添加对Microsoft Visual Basic for Applications Extensibility 5.3(或您拥有的任何版本)的引用.我的盒子上有VBA SDK等等 - 所以这可能不是办公室附带的.

此外,您必须专门启用对VBA对象模型的访问 - 请参阅Word选项中的"信任中心".这是Office提供的所有其他宏安全设置的补充.

此示例将从其所在的当前文档中提取代码 - 它本身是一个VBA宏(并将显示自身和任何其他代码).还有一个Application.vbe.VBProjects集合来访问其他文档.虽然我从来没有这样做过,但我认为外部应用程序也可以使用这个VBProjects集合来打开文件.这些东西的安全性很有趣,所以它可能很棘手.

我也想知道docm文件格式现在是什么 - 像docx这样的XML?这会是一个更好的方法吗?

Sub GetCode()

    Dim prj As VBProject
    Dim comp As VBComponent
    Dim code As CodeModule
    Dim composedFile As String
    Dim i As Integer

    Set prj = ThisDocument.VBProject
        For Each comp In prj.VBComponents
            Set code = comp.CodeModule

            composedFile = comp.Name & vbNewLine

            For i = 1 To code.CountOfLines
                composedFile = composedFile & code.Lines(i, 1) & vbNewLine
            Next

            MsgBox composedFile
        Next

End Sub

推荐阅读
手机用户2502852037
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有