Python Programmatic access to VBA in Excel

Well, I kept playing around and figured it out myself. Turns out that the key modification needs to happen before you even open the Excel application. So like this:

Python Programmatic access to VBA in Excel

Well, I kept playing around and figured it out myself. Turns out that the key modification needs to happen before you even open the Excel application. So like this:

import win32com
import win32com.client
import pythoncom
import tkinter as tk
from tkinter import filedialog
import ctypes
import win32api
import win32con

key = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,
                            "Software\\Microsoft\\Office\\16.0\\Excel"
                            + "\\Security", 0, win32con.KEY_ALL_ACCESS)
win32api.RegSetValueEx(key, "AccessVBOM", 0, win32con.REG_DWORD, 1)

excel = win32com.client.Dispatch("Excel.Application")
workbook = excel.Workbooks.Open(Filename=fs)
excelModule=workbook.VBProject.VBComponents.Add(1)
excelModule.CodeModule.AddFromString(macro)