简体   繁体   中英

No opeing Excel File - VBA & Python

I have some files on OneDrive. My Python program look for exactly 5 files and paste in a local folder (C:/..). After this, a big macro execute a consolidation of all data in 1 output.

On my Python program, I have this error:

(-2147023170, 'Falha na chamada de procedimento remoto.', None, None)

This is the original code:

def job(path,sheet,init, destinationi, hoje, ontem):
files = os.listdir(path)
paths = [os.path.join(path, basename) for basename in files if '.xlsx' in basename]
for i in paths:
    destination = ''
    i_div = i.split('/')
    i_name = i_div[len(i_div)-1]
    for j in namefile:
        if j in i and not i_name.startswith('~$'):
            destination = destinationi + j + hoje + '.xlsx'
            i_name = j + hoje                
    if destination != '':
        try:
            app = xw.App(add_book=False)
            app.display_alerts = False
            wb = app.books.api.Open(i, UpdateLinks=False)
            book = xw.Book(impl=xw._xlwindows.Book(xl=wb))
            print("Abriu às "+str(datetime.now())+" - "+i_name)
            try:
                book.save(destination)
                print("Salvou cópia às "+str(datetime.now())+" - "+i_name)
            except Exception as e: print(e)
            book.close()
            app.kill()
            print("Fechou às "+str(datetime.now())+" - "+i_name)
        except Exception as e: 
            print(e)

Also I have a big macro, which I execute several times in a day. However, today I have a strange error in this code that opens the files that Python should place in the folder (I executed after place manually):

For Each oArq In oPat.Files
    Workbooks.Open oArq
    nova = ActiveWorkbook.Name

    li = WorksheetFunction.CountA(Columns("F"))
    Range("A2:BT" & li).Copy
    Windows(nomeplan).Activate
    Sheets(sub1).Activate
    li2 = WorksheetFunction.CountA(Columns("F"))
    
    If li2 = 1 Then
    Range("A1").Select
    ActiveCell.Offset(1, 0).PasteSpecial xlPasteValues
    Application.CutCopyMode = False
    Application.DisplayAlerts = False
    Workbooks(nova).Close
    GoTo aqui
    End If
    
    Range("A1").End(xlDown).Select
    ActiveCell.Offset(1, 0).PasteSpecial xlPasteValues
    Application.CutCopyMode = False
    Application.DisplayAlerts = False
    Workbooks(nova).Close 
    
aqui:      
Next oArq

In resume: The macro opens others documents Excel, do a copy & paste, and close the file. Sometimes, it goto to "DeuRuim", but the Err.Number return 0. After this, the original Excel closes, I don't know why.

Anyone has the same problem? I don't have a error code or anything to help search a solution.

The problem was solved cleaning the Microsoft Office's cache.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM