Passing an argument from python script to Word Macro VBA (pywintypes.com_error: (-2147352567, 'Exception occurred.')

Passing an argument from python script to Word Macro VBA (pywintypes.com_error: (-2147352567, 'Exception occurred.')

我有一个 Word 宏 VBA 脚本,可以在 word 文档中插入一个 docx 文件:

Public Sub Macro(path As String)
Selection.InsertFile FileName:=path, Range:="", ConfirmConversions:=False, Link:=True, Attachment:= False

ActiveDocument.Close_ SaveChanges:=wdSaveChanges, _ OriginalFormat:=wdOriginalDocumentFormat

Aplication.Quit SaveChanges:=wdSaveChanges

我想创建一个 python 脚本来调用宏,并将要插入到 VBA 脚本中的文件路径作为参数传递。

import win32com.client as win32

word = win32.DispatchEx('Word.Application')
word.Visible = True

doc = word.Documents.Open(r'path to file .docx')
   
word.Application.Run("Complete.Macro.Name", "path_to_file_docx")

 

但我收到以下错误:

pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147352567), None)

我读到如果 Sub 收到一个参数,VBA 就不再是宏了。有谁知道如何克服这个问题?

正在使用

import sys
import pywintypes

try:
    ...
except pywintypes.com_error:
    sys.exit(1)

似乎解决了问题。