win32com 模块传参报异常。
我的vba代码如下:
Sub 图片导入()
'
' 图片导入 宏
'
'
a = InputBox("同APP下打开的world文档名称?")
b = InputBox("需要插入图片的位置?")
Windows(a).Activate
Selection.Find.ClearFormatting
With Selection.Find
.Text = b
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.PasteExcelTable False, False, False
Selection.Tables(1).Rows.Alignment = wdAlignRowCenter
Selection.Tables(1).AutoFitBehavior (wdAutoFitContent)
Selection.Tables(1).AutoFitBehavior (wdAutoFitContent)
End Sub
Sub 图片导入2(a, b)
'
' 图片导入 宏
'
'
Windows(a).Activate
Selection.Find.ClearFormatting
With Selection.Find
.Text = b
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.PasteExcelTable False, False, False
Selection.Tables(1).Rows.Alignment = wdAlignRowCenter
Selection.Tables(1).AutoFitBehavior (wdAutoFitContent)
Selection.Tables(1).AutoFitBehavior (wdAutoFitContent)
End Sub
在word文件中运行vba程序正常,但是在python中庸win32调用vba运行一直报错,但是调用其他不需要传参的vba可以正常运行。python代码如下:
docApp = win32com.client.DispatchEx('Word.Application')
doc = docApp.Documents.Open(doc_path)
vba_doc = docApp.Documents.Open(vba_path_world)
for pic in pictures:
print(pic)
sheet.range(pic.split('-')[1]).api.Copy()
docApp.Application.Run(f"{vba_path_world}!图片导入2",'a.docx',pic)
print('ok')
doc.Save()
vba_doc.Save()
docApp.Quit()
vba_xlsm_wb.close()
错误代码如下:
Traceback (most recent call last):
File "E:\个人\python\python_excel\新建文件夹\Excel转换为world.py", line 176, in
docApp.Application.Run(f"{vba_path_world}!图片导入2",'T$$-c61') #,doc_save_name,pic
File "<COMObject >", line 8, in Run
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\site-packages\win32com\client\dynamic.py", line 314, in ApplyTypes
result = self.oleobj.InvokeTypes(*(dispid, LCID, wFlags, retType, argTypes) + args)
pywintypes.com_error: (-2147352567, '发生意外。', (0, None, None, None, 0, -2147352573), None)
Process finished with exit code 1
查了半天也没有找出问题,请大家指点,谢谢。