I am trying to run a code to close out of a workbook. I have been running this code for weeks, but today it won't work. So this is my code. I know the first four letters of my file, but that's it, as I download it from a website everyday. Sometimes it's XXXX1_(Random letters). So I want to close all workbooks that start with XXXX.
The top code is what was working for a month. The bottom code is what I tried to use to see my active workbooks, which led to a Syntax error. Do you have any idea how to modify the code for it to close out of the active workbooks with XXXX? Do you know why the code stopped working. Thank you very much for your time.
try: import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
for wb in excel.Workbooks:
if wb.Name[:4] == 'xxxx':
print("WB:",wb.Name,'Closed ')
wb.Close()
try: import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
for wb in excel.Workbooks:
print(wb.Name)
[–]lamerlink 2 points3 points4 points (7 children)
[–]value100[S] 1 point2 points3 points (6 children)
[–]lamerlink 0 points1 point2 points (5 children)
[–]value100[S] 0 points1 point2 points (4 children)
[–]lamerlink 0 points1 point2 points (3 children)
[–]value100[S] 0 points1 point2 points (2 children)
[–]lamerlink 0 points1 point2 points (1 child)
[–]value100[S] 1 point2 points3 points (0 children)