+ Reply to Thread
Results 1 to 2 of 2

auto-export VBA modules

  1. #1
    Christopher Merrill
    Guest

    auto-export VBA modules

    I'd like to be able to automatically export all VBA modules from any
    workbook I create whenever I save. I created the following function that
    does the export correctly, but now I want this code to be AUTOMATICALLY
    loaded into EVERY .xls I create. Is there a clean way to do this?

    Thanks in advance

    ------------------------

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
    Boolean)
    Dim VBComp As VBIDE.VBComponent
    Dim Sfx As String

    For Each VBComp In ActiveWorkbook.VBProject.VBComponents
    Select Case VBComp.Type
    Case vbext_ct_ClassModule, vbext_ct_Document
    Sfx = ".cls"
    Case vbext_ct_MSForm
    Sfx = ".frm"
    Case vbext_ct_StdModule
    Sfx = ".bas"
    Case Else
    Sfx = ""
    End Select
    If Sfx <> "" Then
    VBComp.Export _
    Filename:=ActiveWorkbook.Path & "\" & VBComp.Name & Sfx
    End If
    Next VBComp
    End Sub



  2. #2
    Bob Phillips
    Guest

    Re: auto-export VBA modules

    Put this code in a handy workbook such as Personal.xls

    Option Explicit

    Private WithEvents app As Application

    Private Sub app_WorkbookBeforeSave(ByVal Wb As Workbook, ByVal SaveAsUI As
    Boolean, Cancel As Boolean)
    Dim VBComp As VBIDE.VBComponent
    Dim Sfx As String

    For Each VBComp In ActiveWorkbook.VBProject.VBComponents
    Select Case VBComp.Type
    Case vbext_ct_ClassModule, vbext_ct_Document
    Sfx = ".cls"
    Case vbext_ct_MSForm
    Sfx = ".frm"
    Case vbext_ct_StdModule
    Sfx = ".bas"
    Case Else
    Sfx = ""
    End Select
    If Sfx <> "" Then
    VBComp.Export _
    Filename:=ActiveWorkbook.Path & "\" & VBComp.Name & Sfx
    End If
    Next VBComp
    End Sub

    Private Sub Workbook_Open()
    Set app = Application
    End Sub

    'This is workbook event code.
    'To input this code, right click on the Excel icon on the worksheet
    '(or next to the File menu if you maximise your workbooks),
    'select View Code from the menu, and paste the code



    --
    HTH

    Bob Phillips

    "Christopher Merrill" <[email protected]> wrote in message
    news:%[email protected]...
    > I'd like to be able to automatically export all VBA modules from any
    > workbook I create whenever I save. I created the following function that
    > does the export correctly, but now I want this code to be AUTOMATICALLY
    > loaded into EVERY .xls I create. Is there a clean way to do this?
    >
    > Thanks in advance
    >
    > ------------------------
    >
    > Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
    > Boolean)
    > Dim VBComp As VBIDE.VBComponent
    > Dim Sfx As String
    >
    > For Each VBComp In ActiveWorkbook.VBProject.VBComponents
    > Select Case VBComp.Type
    > Case vbext_ct_ClassModule, vbext_ct_Document
    > Sfx = ".cls"
    > Case vbext_ct_MSForm
    > Sfx = ".frm"
    > Case vbext_ct_StdModule
    > Sfx = ".bas"
    > Case Else
    > Sfx = ""
    > End Select
    > If Sfx <> "" Then
    > VBComp.Export _
    > Filename:=ActiveWorkbook.Path & "\" & VBComp.Name & Sfx
    > End If
    > Next VBComp
    > End Sub
    >
    >




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1