Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add excel 2010 macro programmatically

Is there any method to add macro to Excel file programmatically ?
I have too many Excel files I want to add a macro to them. Adding manually (by hand) seems impossible. I need to create a tool to do this.

like image 220
Aloha Avatar asked Jan 24 '13 03:01

Aloha


People also ask

Does Excel 2010 have VBA?

You can access the VBA environment in Excel 2010 by opening the Microsoft Visual Basic for Applications window. First, be sure that the Developer tab is visible in the toolbar in Excel.


1 Answers

Yes, you can do this programatically, you can access the VB Integrated Development Environment through code. The following website are excellent for learning about the VBIDE, I've used them to put together this code. If you run WorkbookModuleImporttwo open dialog boxes will pop up, the first asking for workbooks to import the modules into, the second to select the modules for importing.

Sub WorkbookModuleImport()
    Dim ii As Integer, vFileNames As Variant, vModules As Variant

    'We'll use the Application.GetOpenFilename to get a list of all the excel     workbooks we want to import into
    vFileNames = Application.GetOpenFilename(",*.xls;*.xlsx;*.xlsm", , "Select Workbooks to Import Modules To", , True)
    'If the result is not an array it means the cancel button has been pressed
    If Not IsArray(vFileNames) Then Exit Sub

    'Use the same method to get all the modules/classes/forms to input
    vModules = Application.GetOpenFilename(",*.cls, *.bas, *.frm", , "Select Modules/Forms/Class Modules to Import", , True)
    If Not IsArray(vModules) Then Exit Sub

    'Now loop through all the workbooks to import the modules
    For ii = LBound(vFileNames) To UBound(vFileNames)
        Call ImportModules(VBA.CStr(vFileNames(ii)), vModules)
    Next ii

End Sub



Public Sub ImportModules(sWorkbookName As String, vModules As Variant)
    Dim cmpComponents As VBIDE.VBComponents, ii As Integer
    Dim wkbTarget As Excel.Workbook

    'We need to open the workbook in order to be able to import the code module
     Set wkbTarget = Workbooks.Open(sWorkbookName)

    'If the project is protected with a password we can't import so just set tell us in the immediate window
    If wkbTarget.VBProject.Protection = 1 Then
        'Give a message
        Debug.Print wkbTarget.Name & " has a protected project, cannot import module"
        GoTo Cancelline
    End If

    'This is where we set the reference to the components of the Visual Basic project
    Set cmpComponents = wkbTarget.VBProject.VBComponents

   'Loop through all the modules to import
    For ii = LBound(vModules) To UBound(vModules)
        cmpComponents.Import vModules(ii)
    Next ii


Cancelline:
   'If it's in Excel 2007+ format but doesn't already have macros, we'll have to save it as a macro workbook
   If wkbTarget.FileFormat = xlOpenXMLWorkbook Then
       wkbTarget.SaveAs wkbTarget.Name, xlOpenXMLWorkbookMacroEnabled
       wkbTarget.Close SaveChanges:=False
   Else
       'Otherwise, just save the workbook and close it
        wkbTarget.Close SaveChanges:=True
   End If

   'I don't trust excel, so set the workbook object to nothing
   Set wkbTarget = Nothing
End Sub

These webpages are great references: http://www.cpearson.com/excel/vbe.aspx and http://www.rondebruin.nl/vbaimportexport.htm. I used Ron's as a starting point.

like image 191
CuberChase Avatar answered Oct 12 '22 09:10

CuberChase