如何在使用python运行MSword宏时消除com_错误?

2024-09-30 05:27:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用python运行MS word VBA宏,但一直出现错误

import win32com.client as win32

WrdApp = win32.GetActiveObject("Word.Application")

#Call current file
WrdDoc = WrdApp.ActiveDocument

#Insert Caption
WrdApp.Selection.InsertCaption(Label='number')
#^This line gives the error

错误消息

com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Word', 'Command failed', 'wdmain11.chm', 36966, -2146824090), None)

任何熟悉Python和MS word宏的人都可以告诉我为什么会出现此错误


Tags: importclientapplicationas错误errorcallvba
1条回答
网友
1楼 · 发布于 2024-09-30 05:27:08

如果要在标签中使用除“等式”、“图形”、“表格”或WdCaptionLabelID以外的其他标签,则必须定义新标签(请参见https://docs.microsoft.com/en-us/office/vba/api/word.selection.insertcaptionhttps://docs.microsoft.com/en-us/office/vba/api/word.captionlabels):

import win32com.client as win32

WrdApp = win32.GetActiveObject("Word.Application")

# Call current file
WrdDoc = WrdApp.ActiveDocument

cLabel = "number"
# define new label
WrdApp.CaptionLabels.Add(Name=cLabel)
# Insert Caption
WrdApp.Selection.InsertCaption(Label=cLabel)
# no error

相关问题 更多 >

    热门问题