VBA宏导出宏

时间:2014-07-23 10:28:14

标签: vba excel-vba excel

我想编写一个宏来导出VBA编辑器项目视图中存在的宏,因为无法导出多个文件

我之所以这样做是因为我需要.bas.cls文件来创建doxygen文档。

如果有人知道更简单的解决方案,请告诉我。否则:如何从项目中一次导出所有VBA代码文件?

致以最诚挚的问候,谢谢!

2 个答案:

答案 0 :(得分:0)

首先,不用说,无论哪个代码“触及您的代码”都必须信任

是的,可以使用VBProject和VBComponent来实现它。请查看herehere。这些链接很好地解释了如何做你所要求的。

答案 1 :(得分:0)

工作示例,导出.bas,。cls.frm模块:

Option Explicit

Public Sub MakeDoxy()

    Dim rootDir As String
    Dim sourceDir As String

    rootDir = GetFolder("C:\") & "\"
    sourceDir = rootDir & "source\"

    If Dir(rootDir, vbDirectory) = "" Then
        MkDir rootDir
    End If

    If Dir(sourceDir, vbDirectory) = "" Then
        MkDir sourceDir
    End If

    ExportVBAModules (sourceDir)

End Sub

Private Sub ExportVBAModules(ByVal sourceDir As String)

    Dim objVBComp As VBComponent
    Dim objVBProj As VBProject
    Dim ext As String

    Set objVBProj = ThisWorkbook.VBProject

    For Each objVBComp In objVBProj.VBComponents

        ' We don't export THIS module
        If objVBComp.Name = "MakeDoxygen" Then GoTo Skip

        If Dir(sourceDir & objVBComp.Name, vbDirectory) = "" Then
            MkDir sourceDir & objVBComp.Name
        End If

        Select Case objVBComp.Type
            Case vbext_ct_ClassModule: ext = ".cls"
            Case vbext_ct_Document: GoTo Skip
            Case vbext_ct_StdModule: ext = ".bas"
            Case vbext_ct_MSForm: ext = ".frm"
            Case Else: GoTo Skip
        End Select

        objVBComp.Export sourceDir & objVBComp.Name & "\" & objVBComp.Name & ext
Skip:
    Next

End Sub


Private Function GetFolder(strPath As String) As String

    Dim fldr As FileDialog
    Dim sItem As String
    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)

    With fldr
        .Title = "Select ''VBADoxy'' Root Folder"
        .AllowMultiSelect = False
        .InitialFileName = strPath
        If .Show <> -1 Then GoTo NextCode
        sItem = .SelectedItems(1)
    End With

NextCode:
    GetFolder = sItem
    Set fldr = Nothing

End Function