Results 1 to 2 of 2

Thread: Backup all modules, class modules and userforms to a selectable folder

  1. #1
    Member
    Join Date
    Jul 2013
    Posts
    40
    Rep Power
    0

    Backup all modules, class modules and userforms to a selectable folder

    I do not know if this is already shared but since I have found a part of it online and made some additions from older downloaded code I thought to share it.

    This will save all modules, class modules and forms to a selected folder.

    To the mods: I suspect that this might not be the proper subforum to share the code and I apologize but I didn't know where exactly to share.
    Please by all means transfer it to the proper location if needed.

    Code:
    Option Explicit
    
    ' ---------------------- Directory Choosing Helper Functions -----------------------
    ' Excel and VBA do not provide any convenient directory chooser or file chooser
    ' dialogs, but these functions will provide a reference to a system DLL
    ' with the necessary capabilities
    Private Type BROWSEINFO ' used by the function GetFolderName
        hOwner As Long
        pidlRoot As Long
        pszDisplayName As String
        lpszTitle As String
        ulFlags As Long
        lpfn As Long
        lParam As Long
        iImage As Long
    End Type
    
    Private Declare Function SHGetPathFromIDList Lib "shell32.dll" _
        Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
    Private Declare Function SHBrowseForFolder Lib "shell32.dll" _
        Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
    
    Function GetFolderName(Msg As String) As String
    ' returns the name of the folder selected by the user
    Dim bInfo As BROWSEINFO, Path As String, R As Long
    Dim x As Long, pos As Integer
        bInfo.pidlRoot = 0& ' Root folder = Desktop
        If IsMissing(Msg) Then
            bInfo.lpszTitle = "Select a folder."
            ' the dialog title
        Else
            bInfo.lpszTitle = Msg ' the dialog title
        End If
        bInfo.ulFlags = &H1 ' Type of directory to return
        x = SHBrowseForFolder(bInfo) ' display the dialog
        ' Parse the result
        Path = Space$(512)
        R = SHGetPathFromIDList(ByVal x, ByVal Path)
        If R Then
            pos = InStr(Path, Chr$(0))
            GetFolderName = Left(Path, pos - 1)
        Else
            GetFolderName = ""
        End If
    End Function
    '---------------------- END Directory Chooser Helper Functions ----------------------
    
    
    Sub ExportMods()
    ' reference to extensibility library
    
    'Tip : you need to set the VBA Extension reference. In your workbook go into the VBA screen and Select the Tools Menu and References
    'then search for Micrsoft Visual Basic For Applications Extensibility 5 and tick the box then you can run the code
    
    Dim Path As String
    Dim objMyProj As VBProject
    Dim objVBComp As VBComponent
    
    Set objMyProj = Application.VBE.ActiveVBProject
    
    Path = GetFolderName("Choose the folder to export BAS files to:")
    If Path = "" Then
        MsgBox ("You didn't choose an export directory. Nothing will be exported.")
        Exit Sub
    End If
    
    For Each objVBComp In objMyProj.VBComponents
    If objVBComp.Type = vbext_ct_ClassModule Or vbext_ct_MSForm Or vbext_ct_StdModule Then
    objVBComp.Export Path & "\" & objVBComp.Name & ".bas"
    End If
    Next
    End Sub
    Keep in mind all vba I know has been googled...

  2. #2
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,122
    Rep Power
    10
    Hi

    Thanks! Moved to Excel Tips forum.
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

Similar Threads

  1. Replies: 2
    Last Post: 11-10-2013, 11:48 PM
  2. Replies: 1
    Last Post: 02-14-2013, 12:09 PM
  3. Array Class Module
    By Rajan_Verma in forum Rajan Verma's Corner
    Replies: 3
    Last Post: 12-20-2012, 11:22 AM
  4. Pass Values Between Multiple Userforms
    By Excel Fox in forum Excel and VBA Tips and Tricks
    Replies: 0
    Last Post: 07-24-2011, 03:25 AM
  5. Find Parent Folder From Given Folder / File Path
    By Excel Fox in forum Excel and VBA Tips and Tricks
    Replies: 1
    Last Post: 05-28-2011, 03:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •