Importing/exporting Excel VB modules

Mark Carter cartermark46 at ukmail.com
Thu Jun 19 11:06:22 EDT 2003


cartermark46 at ukmail.com (Mark Carter) wrote in message news:<d3c9c04.0306190106.79af7c3e at posting.google.com>...
> I have the excellent win32com python module. What I'm trying to do is
> import and export VBA modules into Excel programmatically.
> 
> Does anyone know how to enumerate the modules?
> 
> From within the VBA editor I tried:
>   x = ThisWorkbook.Modules.Count
> but it came up with the number 0 (which isn't correct).

Actually, I think I've sorted this out. Something like this will do (in VBA):

Public Sub ExportModules()
    
    Dim wb As Workbook
    Set wb = ThisWorkbook
    Dim VBComp 'As codemodule
    Dim D As String
    Dim N
    
    
    D = ThisWorkbook.Path
    For Each VBComp In wb.VBProject.VBComponents
        If (VBComp.Type = 1) Then 'standard module
            N = D + "\" + VBComp.Name + ".bas"
            VBComp.Export N
        End If
    Next
    'Set VBComp = wb.VBProject.VBComponents("modModules")
    'VBComp.Export pt + "\modModules.bas"
    
End Sub

It's not actually in python - but I think that a port is easy enough.




More information about the Python-list mailing list