none
macro signing in python RRS feed

  • Question

  • Hi everyone,

    I would like to know if anyone knows how to sign a macro thanks to a python program. Indeed, I have more than 3000 excel files to sign and I don't know to do it manually  as it will take me more than a week... Therefor I need a python program find all .xlsm file in a folder and to sign each of them . 

    For now I have that:

    ###########################step 1############################      

    filepath =r"C:/Users/program test"
    listOfXLSM = []
    #print("Checking for XLSM in this folder...")
    for file in os.listdir(filepath):
        if fnmatch.fnmatch(file, "*.xlsm"):
            listOfXLSM.append(file)

    ###########################step 2############################      
    # Create the vba macro which will be copy/paste into the current .xlsm file to
    #sign the macro
    strcode = \
    '''
    Sub Signing()
        ActiveWorkbook.Signatures.AddNonVisibleSignature '_
        '    "{00000000-0000-0000-0000-000000000000}"
    End Sub
    '''
    com_instance = Dispatch("Excel.Application")
    com_instance.Visible = True
    com_instance.DisplayAlerts = False

    ###########################step 3##############################################      
    # Open each .xlsm file find in listOfXLSM and sign it
    for file in listOfXLSM:
        print("Processing: %s" % file)
        file_path=filepath
        file_name=file
        objworkbook = com_instance.Workbooks.Open(file)
        xlmodule = objworkbook.VBProject.VBComponents.Add(1)
        xlmodule.CodeModule.AddFromString(strcode.strip())
        objworkbook.SaveAs(file_path+file)
    com_instance.Quit()

    Thank you in advance for your help

    Wednesday, June 26, 2019 6:52 AM