Buongiorno a tutti,
Ho un problema che non riesco a risolvere:
Partendo da un file Excel che chiamo Cruscotto di lancio, apro altri file excel a cascata ed uno alla volta, ed eseguo altre macro all'interno di questi file.
Anzi, per ogni file presente nel cruscotto, apro 2 volte il file excel, la prima volta esegue una macro “Aggiorna” che :
inserisce un componente aggiuntivo che mi serve per scaricare dati da sap, lancia unamacro che copia i dati in append per ogni ciclo su mese e anno su un altro foglio di lavoro. Poi chiudo e torno al cruscotto.
La seconda volta, apro per chiamare un’altra macro, "Post Refresh", che esegue delle operazioni vba ma senza il componente aggiuntivo (per velocizzare perché non mi serve più scaricare da sap )
Al secondo file, ecco l’errore :
Errore di automazione – Chiamata di sistema non riuscita
Controllando, ho visto che per il primo file esegue correttamente la macro aggiorna ed anche la macro post refresh.
Per il secondo file esegue correttamente la macro aggiorna, poi chiude il file ma non riesce ad aprirlo per fare il post refresh.
La cosa strana è che non ho problemi se il cruscotto viene eseguito su una macchina con Win server 2008 R2 con Office 2010.
Su tutte le altre macchine compreso il mio pc, con Win10 e 365 da errore. A volte, riavviando il pc, il cruscotto funziona fino alla fine, a volte no.
Vi posto il codice del cruscotto di lancio perché secondo me è qui che da errore. All'interno ho una function che "killa" gli excel aperti tranne il cruscotto di lancio, questo perchè l'istruzione Quit non funziona.
Ah poi altra cosa, facendo debug non mi porta sull’istruzione in errore ma mi rimanda all'istruzione di apertura.
In pratica, il cruscotto di lancio, all’apertura esegue :
Sheets("Settaggi").apro_file
mentre il codice apro_file è :
Sub apro_file()
'
'
Dim xl, x2
Dim myConnection
Dim xlBook, xlBook2
Dim percorso_file, nome_file, nome_attuale, valore, path_Bex, prima_elaborazione As String
Dim nx, n_file As Integer
Dim mese, peri_fiscale As String
Dim data_save, time_save As Date
Dim anno, para1 As String
Dim sys_client, sys_language, sys_number, sys_sid, sys_server_addr, sys_usr, sys_pwd As String
datasave = CDate(Date)
anno = Year(datasave)
mese = Month(datasave)
gg = Day(datasave)
Application.DisplayAlerts = False
nome_attuale = ThisWorkbook.Name
Sheets("Settaggi").Activate
' inizio cruscotto generale
time_save = Format(Now, "hh:mm:ss")
Cells(2, 15).Value = time_save
'Fine elaborazione del Cruscotto
For n = 2 To 500
'scrivo inizio del cruscotto per ogni file elaborato
Cells(n, 11).Value = mese & "/" & gg & "/" & anno
time_save = Format(Now, "hh:mm:ss")
Cells(n, 12).Value = time_save
If Cells(n, 1).Value = "FINE" Then 'Operazioni di chiusura
'Fine cruscotto in generale
time_save = Format(Now, "hh:mm:ss")
Cells(2, 16).Value = time_save
Windows(nome_attuale).Activate
ActiveWorkbook.Save
Exit Sub
End If
' ---
‘Apro con componente aggiuntivo per Sap
' ---
' Launch Excel
Set xl = CreateObject("Excel.Application")
' Make it visible otherwise things just don't work well
xl.Visible = True
' Now that Excel is open, open the BEX Analyzer Addin xla file
path_Bex = Range("path_Bex_name")
xl.Workbooks.Open (path_Bex & "BExAnalyzer.xla")
' Run the SetStart macro that comes with BEX so it pays attention to you
xl.Run ("BExAnalyzer.xla!SetStart")
sys_client = Range("client")
sys_language = Range("language")
sys_number = Range("system_number")
sys_sid = Range("sid")
sys_server_addr = Range("server_addr")
sys_usr = Range("usr")
sys_pwd = Range("pwd")
' Logon directly to BW using the sapBEXgetConnection macro
Set myConnection = xl.Run("BExAnalyzer.xla!sapBEXgetConnection")
With myConnection
.client = sys_client
.user = sys_usr
.Password = sys_pwd
.Language = sys_language
.SystemNumber = sys_number
.system = sys_sid
.applicationserver = sys_server_addr
.usesaplogonini = False
.SAProuter = ""
.logon 0, True
End With
' Now initialize the connection to make it actually usable
xl.Run ("BExAnalyzer.xla!sapBEXinitConnection")
' Ciclo di apertura file
Windows(nome_attuale).Activate
'Se non c'è la scritta fine
If Not IsEmpty(Cells(n, 1)) Then
percorso_file = Cells(n, 2).Value
nome_file = Cells(n, 3).Value
Set xlBook = xl.Workbooks.Open(percorso_file & nome_file, 0, False)
xlBook.Application.Run "'" & percorso_file & nome_file & "'!Aggiorna"
xlBook.Save
xlBook.Close
Set xlBook = Nothing
Set xl = Nothing
‘ xl.Quit
Call killOtherExcel
'----- chiamata con excel non BEX
Set x2 = CreateObject("Excel.Application")
x2.Visible = True
Set xlBook2 = x2.Workbooks.Open(percorso_file & nome_file, 0, False)
xlBook2.Application.Run "'" & percorso_file & nome_file & "'!POST_REFRESH"
xlBook2.Save
Set xlBook2 = Nothing
Set x2 = Nothing
‘ x2.Quit
Call killOtherExcel
'----- fine
End If
Next n
esci:
' --turn on delle popup
Application.DisplayAlerts = True
End Sub
Sub killOtherExcel()
Dim oServ As Object
Dim cProc As Variant
Dim oProc As Object
Set oServ = GetObject("winmgmts:")
Set cProc = oServ.ExecQuery("Select * from Win32_Process")
For Each oProc In cProc
If oProc.Name = "EXCEL.EXE" And oProc.ProcessID <> GetCurrentProcessId Then
errReturnCode = oProc.Terminate()
End If
Next
End Sub
Non so proprio cosa fare, per ora gira sulla macchina con win server ma essendo da dismettere ho assolutamente bisogno di capire come farlo funzionare su altro.
Grazie mille