I am trying to run a macro in the Personal.XLSB using python.
When I run the macro PERSONAL.XLSB!PIVOTS myself from the excel workbook it works. Also, if I copy and paste the vba code into "This Workbook" and run xlApp.Run('Pivots') it works.
However, when I use xlApp.Run('C:\\Users\\AppData\\Roaming\\Microsoft\\Excel\\XLSTART\\PERSONAL.XLSB!Pivots') it won't work. I need it to run in 'PERSONAL.XLSB' as I will be using the same macro in several files.
from __future__ import print_function
import unittest
import os.path
import win32com.client
class ExcelMacro(unittest.TestCase):
def test_excel_macro(self):
try:
xlApp = win32com.client.DispatchEx('Excel.Application')
xlsPath = os.path.expanduser('C:\\Users\\colm_mcsweeney\\Documents\\Attachments\\Dash.09.05.19.xlsm')
wb = xlApp.Workbooks.Open(Filename=xlsPath)
xlApp.Visible = True
xlApp.Run("C:\\Users\\AppData\\Roaming\\Microsoft\\Excel\\XLSTART\\PERSONAL.XLSB!Pivots")
wb.Save()
xlApp.Quit()
print("Macro ran successfully!")
except:
print("Error found while running the excel macro!")
xlApp.Quit()
if __name__ == "__main__":
unittest.main()
Run('Pivots')in VBA. The objects, functions, are exactly the same in both casesRunonly runs macros, it doesn't load the files. You should add another call toxlApp.Workbooks.Opento load the second workbook before you use.Run("PERSONAL.XLSB"!PIVOTS")