因此,我有一个VBA脚本将excel文件的内容上载到我的SharePoint网站,我试图从我的Python脚本运行该脚本,该脚本处理数据到该excel工作簿的传输。宏主要靠自己工作,使用Python脚本移动数据也是如此,但是为了易于使用,我希望脚本在移动数据并保存工作簿后尝试启动宏。以下是Python脚本的代码。
from openpyxl import load_workbook
from tkinter import Tk
from tkinter.filedialog import askopenfilename
from collections import defaultdict
import collections
import datetime
import time
import os, os.path
import win32com.client
class TransferSheet:
def __init__(self):
'''
Initializes all Variables for use within the ClassProgram
'''
Tk().withdraw()
self.utbook = load_workbook(filename = 'Untimed Report2019-06-17.xlsx')
self.macbook = load_workbook(filename = 'UploadUntimed.xlsm', keep_vba= True)
def transfer_report(self):
'''
This creates the sheets for the two workbooks, clears the contents of the Upload Document
Then once cleared it transfers the content of the UT. Report.
'''
utsheet = self.utbook.worksheets[2]
macsheet = self.macbook.worksheets[0]
for data_rows in macsheet.iter_rows():
for data_cell in data_rows:
data_cell.value = None
for row in utsheet.iter_rows():
macsheet.append([cell.value for cell in row])
def save_sheet(self):
'''
Saves the Work Book.
'''
self.macbook.save(filename= 'UploadUntimed.xlsm')
def run_macro(self):
'''
Runs the Macro from the excel sheet.
'''
#Launch Excel and Open Wrkbook
xl=win32com.client.Dispatch("Excel.Application")
xl.Workbooks.Open(Filename="C:\\Users\\USERNAME\\Desktop\\Python_Project\\UploadUntimed.xlsm") #opens workbook in readonly mode.
#Run Macro
xl.Application.Run("UploadUntimed.xlsm!Module2.AddNew_SP")
#Save Document and Quit.
xl.Application.Save()
xl.Application.Quit()
#Cleanup the com reference.
del xl
def main():
x = TransferSheet()
x.transfer_report()
x.save_sheet()
x.run_macro()
print("Done!")
if __name__ == "__main__": main()
因此,此脚本的作用是在其目录中包含两个文件(带有数据和Macroworkbook的“我的报表”,它将所需的数据从表格之一移至Macro工作簿,然后Macro本身将参与并发送数据发送到我的SharePoint列表。VBA脚本利用连接可靠地对自己进行身份验证,然后发送数据(如果可以使用python,我花了数周的时间检查该解决方案,并确定了当前访问权限我有足够的资源和资源来使用它,将来继续处理它会更有效率)
Option Explicit
Sub AddNew_SP()
Dim cnt As ADODB.Connection
Dim rst As ADODB.Recordset
Dim mySQL As String
Dim i As Integer
Dim LastRow As Integer
Set cnt = New ADODB.Connection
Set rst = New ADODB.Recordset
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
mySQL = "SELECT * FROM [1];"
'This uses ADO to open the SP and add data I based this solution off of the video below
'Youtube Link: https://youtu.be/1qu6u2x7Kms
'GUID : {-}
'Site : --
With cnt
.ConnectionString = _
"Provider=Microsoft.ACE.OLEDB.12.0;WSS;IMEX=0;RetrieveIds=Yes;DATABASE=MYSITE;LIST={MYGUID};"
.Open
End With
rst.Open mySQL, cnt, adOpenDynamic, adLockOptimistic
For i = 2 To LastRow 'This sets the second rowa as i (not counting heading row) and makes loop to go from 2 to the LastRow.
With ActiveSheet.Rows(i) 'This is with the current sheet at row i
rst.AddNew
rst.Fields("Department") = .Cells(1).Value
rst.Fields("Machine#") = .Cells(2).Value
rst.Fields("Operation#") = .Cells(3).Value
rst.Fields("Part#") = .Cells(4).Value
rst.Fields("Program") = .Cells(12).Value
End With
rst.UpdateBatch
Next i 'changes rows by incrementing i.
If CBool(rst.State And adStateOpen) = True Then rst.Close
Set rst = Nothing
If CBool(cnt.State And adStateOpen) = True Then cnt.Close
Set cnt = Nothing
End Sub
所以,我的问题是我希望脚本上载我的数据,但是我可以告诉它在尝试启动我得到的宏时它已经运行了代码的某些部分:
“运行时错误'-2147217885(80040e23)”:
行句柄指向已删除的行或标记为删除的行”
出于某种原因,我在考虑我的Excel工作簿没有检查其中的数据,因此当它尝试运行宏时,它会运行得好像没有数据要发送一样。但是我知道应该是因为在工作簿中传输了数据之后,我在脚本中运行了宏功能。当我点击“调试”时,它指向我在VBA脚本中的rst.UpdateBatch。