I'm trying to control Word using a Python3 script using win32com. I've imported a VBA UserForm, and am trying to show it but receiving an error. Some code:
from win32com.client.dynamic import Dispatch
from win32com.client.dynamic import ERRORS_BAD_CONTEXT
import win32com.client as win32
app = win32.gencache.EnsureDispatch('Word.Application')
app.Visible = True
app.Documents.Open('.../'+current_doc+'.docx')
try:
app.VBE.VBProjects(1).VBComponents \
.Import('.../UserForm1.frm')
app.Run('UserForm_Initialize()')
The script succesfully imports the UserForm, but upon attempting to initialize, it is throwing this error:
com_error: (-2147352567, 'Exception occurred.', (0, 'Forms.Form.1', "Can't move focus to the control because it is invisible, not enabled, or of a type that does not accept the focus.", 'fm20.hlp', 0, -2147352573), None)
Does anyone know a way to initialize the UserForm successfully? I can't use an alternative to using a VBA UserForm unfortunately, as that part is already written entirely.
Thanks!!!
Note: I've verified the app.Run() command is correct by also separately importing a module to call the UserForm, and running it. Same error. However, if I then open the VBA console and run the same module, it successfully opens the UserForm.