Export All Modules In Current Project

  • Good day Ozgrid. I'm trying to write some code to export all the standard modules (not class or worksheet modules) in my project to a directory on the network. I'm looking for something like below, but can't quite find the syntax to get the modules collection.


    I know this syntax is not correct, but I'm sure it has to be something kind of similar to this.


    Any help would be great.


    Code
    Dim MyModules as collection
    Dim MyModule as module
    
    
    Set MyModules = currentproject.modules
    For each MyModule in MyModules
    MyModule.export "x:\directory1\Directory2\File.bas"
    
    
    next MyModule
  • Re: Export All Modules In Current Project


    i know just the place you can get the code for this VBA Express



    In your macro security you'll have to have Trust access to Visual Basic Project ticked


    HTH


    z

  • Re: Export All Modules In Current Project


    Thanks for the link, but this code doesn't appear to work.


    Besides, the best I can tell, it is only designed to export a single module.


    I need code to loop through and export all the modules regardless of how many there are.

  • Re: Export All Modules In Current Project


    [vba]Sub x()


    ' reference to extensibility library

    Dim objMyProj As VBProject
    Dim objVBComp As VBComponent

    Set objMyProj = Application.VBE.ActiveVBProject

    For Each objVBComp In objMyProj.VBComponents
    If objVBComp.Type = vbext_ct_StdModule Then
    objVBComp.Export "C:\temp\" & objVBComp.Name & ".bas"
    End If
    Next

    End Sub
    [/vba]

    [h4]Cheers
    Andy
    [/h4]

  • Re: Export All Modules In Current Project


    Thank you Andy Pope.


    One Hitch. I appear to be missing a reference. Do you know what I need to reference to have access to the VBProject variable type?


    Other then that this is EXACTLY what I was looking for. I knew it would be something like that, I just didn't know how to reference the correct collections.



    Thanks again.


    PS, do you know if this code will work in MSAccess too?

  • Re: Export All Modules In Current Project


    One Hitch. I appear to be missing a reference. Do you know what I need to reference to have access to the VBProject variable type?


    Your comment says 'extensibility library', but I can't find one with that name.

  • Re: Export All Modules In Current Project


    I'm getting an error on the following line.


    Code
    Set objMyProj = Application.VBE.ActiveVBProject


    Error says... "Method 'VBE of object '_Application' failed'


    I have added the reference to Extensibility...


    Any help?

  • Re: Export All Modules In Current Project


    Not having Trusted access to projects checked causes a different error.


    "Programmatic access to Visual Basic Project is not trusted"


    which application gives the error, excel or access?

    [h4]Cheers
    Andy
    [/h4]

  • Re: Export All Modules In Current Project


    Sorry I was too hasty.


    When I unchecked and ran the code I got the error I posted.
    On running the code again I no longer check that warning and instead I get your error.


    Shame I was hoping zimitry was going to post a flashing arrow version ::D

    [h4]Cheers
    Andy
    [/h4]

  • Re: Export All Modules In Current Project


    Thank you both for your help. Ozgrid comes through again. I love this forum. Attached is the code I needed this for. If anyone can use it, please do.


    The code is designed to save the SQL of all saved Queries in a MS Access DB as a text file.
    To save field definitions for all tables
    and


    The part you all helped me with


    Export all modules stored in the DB.


    This serves as documentation/back-up of the database design.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!