Answer a question I open several different workbooks (excel xlsx format) in COM, and mess with them. As the program progresses I wish to close one specific workbook but keep the rest open. How do I cl
I open several different workbooks (excel xlsx format) in COM, and mess with them. As the program progresses I wish to close one specific workbook but keep the rest open.
How do I close ONE workbook? (instead of the entire excel application)
xl = Dispatch("Excel.Application")
xl.Visible = False
try:
output = xl.Workbooks.Open(workbookName)
output2 = xl.Workbooks.Open(workbook2Name)
except com_error:
print "you screwed up blahblahblah"exit()
#work on some stuff#close output but keep output2 open
Answers
The the Workbook COM object has a Close() method. Basically, it should be something like:
xl = Dispatch('Excel.Application')
wb = xl.Workbooks.Open('New Workbook.xlsx')
# dosome stuff
wb.Close(True) # save the workbook
The above was just a skeleton here's some code that works on my machine against Office 2010:
所有评论(0)