Python打开Excel VBA脚本,保存问题

时间:2018-10-03 14:36:46

标签: python vba excel-vba

我正在尝试让python启动VBA脚本。我已经使用了该论坛上一篇文章中的以下代码。该代码有效,除了我无法保存文档,并且无法弄清楚原因。错误消息发布在下面。

# -*- coding: iso-8859-1 -*-

"""
Created on Tue Jun 12 09:48:34 2018

@author: Mr.X
"""
import os
import win32com.client

#Launch Excel and Open Workbook
xl=win32com.client.Dispatch("Excel.Application")
xl.Workbooks.Open(Filename="C:\Users\mr.x\Desktop\Python\Python open vba\macro.xlsm") #opens workbook in readonly mode.

#Run Macro
xl.Application.Run("runme")

#Save Document and Quit.

xl.Application.Save()
xl.Application.Quit()

#Cleanup the com reference.
del xl

这是错误消息:

Traceback (most recent call last):
  File "C:/Users/mr.x/Desktop/Python/Python open vba/openvba.py", line 20, in <module>
    xl.Application.Save("test2222222")
  File "<COMObject <unknown>>", line 2, in Save
pywintypes.com_error: (-2147352567, 'Undantag intr\xe4ffade.', (0, u'Microsoft Excel', u'Save-metoden i Application-klassen misslyckades', u'xlmain11.chm', 0, -2146827284), None)

Process finished with exit code 1

0 个答案:

没有答案