L'ouverture d'un file Excel permet manuellement de créer des formules, l'ouverture d'un file excel avec VBScript ou PowerShell ou Win32com de Python ne fonctionne pas

J'ai un problème avec un script qui ne met pas à jour un file Excel et je l'ai réduit au problème suivant:

Si j'ouvre un file excel, je peux aller dans l'onglet Formules et cliquer sur "Calculer maintenant" et il va passer un peu de time à mettre à jour tous les calculs.

Si je cours un VBScript juste pour ouvrir le file (voir le code suivant), si je passe dans l'onglet Formules et click "Calculer maintenant", il va se rafraîchir immédiatement et rien ne changera.

Dim objXLApp, objXLWb, objXLWs Set objXLApp = CreateObject("Excel.Application") objXLApp.Visible = True Set objXLWb = objXLApp.Workbooks.Open(file_path.xls) 

J'ai essayé toutes sortes de choses comme:

 objXLApp.Calculation = xlAutomatic objXLApp.Calculate objXLApp.CalculateFull objXLApp.CalculateFullRebuild objXLWb.RefreshAll objXLWs.EnableCalculation = True objXLWs.Calculate objXLApp.Calculation = xlAutomatic objXLApp.Calculation = xlAutomatic objXLApp.Calculate objXLApp.CalculateFull objXLApp.CalculateFullRebuild objXLWb.RefreshAll objXLWs.EnableCalculation = True objXLWs.Calculate objXLApp.Calculate objXLApp.Calculation = xlAutomatic objXLApp.Calculate objXLApp.CalculateFull objXLApp.CalculateFullRebuild objXLWb.RefreshAll objXLWs.EnableCalculation = True objXLWs.Calculate objXLApp.CalculateFull objXLApp.Calculation = xlAutomatic objXLApp.Calculate objXLApp.CalculateFull objXLApp.CalculateFullRebuild objXLWb.RefreshAll objXLWs.EnableCalculation = True objXLWs.Calculate objXLApp.CalculateFullRebuild objXLApp.Calculation = xlAutomatic objXLApp.Calculate objXLApp.CalculateFull objXLApp.CalculateFullRebuild objXLWb.RefreshAll objXLWs.EnableCalculation = True objXLWs.Calculate objXLWb.RefreshAll objXLApp.Calculation = xlAutomatic objXLApp.Calculate objXLApp.CalculateFull objXLApp.CalculateFullRebuild objXLWb.RefreshAll objXLWs.EnableCalculation = True objXLWs.Calculate objXLWs.EnableCalculation = True objXLApp.Calculation = xlAutomatic objXLApp.Calculate objXLApp.CalculateFull objXLApp.CalculateFullRebuild objXLWb.RefreshAll objXLWs.EnableCalculation = True objXLWs.Calculate 

Mais ceux-ci semblent faire la même chose que dans l'onglet et en cliquant sur "Calculer", ce qui entraîne un rafraîchissement rapide et la page Excel ne tente pas de mettre à jour.

La même chose est vraie lors de l'utilisation du module win32com de python. Je ne peux pas exécuter des calculs dans le file ouvert.

 import win32com.client as win32 excel = win32.Dispatch('Excel.Application') excel.Visible = True excel_workbook = excel.Workbooks.Open(file_path.xls) importer win32com.client comme win32 import win32com.client as win32 excel = win32.Dispatch('Excel.Application') excel.Visible = True excel_workbook = excel.Workbooks.Open(file_path.xls) excel = win32.Dispatch ('Excel.Application') import win32com.client as win32 excel = win32.Dispatch('Excel.Application') excel.Visible = True excel_workbook = excel.Workbooks.Open(file_path.xls) excel.Visible = True import win32com.client as win32 excel = win32.Dispatch('Excel.Application') excel.Visible = True excel_workbook = excel.Workbooks.Open(file_path.xls) 

La même chose est vraie en utilisant PowerShell.

 $excel = New-Object -com excel.application $excel.Visible = $True $workbook = $excel.Workbooks.Open( $file_path ) $ excel = New-Object -com Excel.application $excel = New-Object -com excel.application $excel.Visible = $True $workbook = $excel.Workbooks.Open( $file_path ) $ excel.Visible = $ True $excel = New-Object -com excel.application $excel.Visible = $True $workbook = $excel.Workbooks.Open( $file_path ) 

Alors pourquoi l'ouverture d'un file avec ces langues a-t-elle coupé la capacité de calculer les formules?

Essaye ça. Juste pour les tests.

 ' Launch Excel... CreateObject("WScript.Shell").Run "excel.exe" ' Wait for it to load... WScript.Sleep 5000 ' Get the running instance... Set Excel = GetObject(, "Excel.Application") ' Open your workbook... Excel.Workbooks.Open strPath ' Now go and click the Calculate button and see if it works. 'Lancer Excel ... ' Launch Excel... CreateObject("WScript.Shell").Run "excel.exe" ' Wait for it to load... WScript.Sleep 5000 ' Get the running instance... Set Excel = GetObject(, "Excel.Application") ' Open your workbook... Excel.Workbooks.Open strPath ' Now go and click the Calculate button and see if it works. CreateObject ("WScript.Shell"). Exécutez "excel.exe" ' Launch Excel... CreateObject("WScript.Shell").Run "excel.exe" ' Wait for it to load... WScript.Sleep 5000 ' Get the running instance... Set Excel = GetObject(, "Excel.Application") ' Open your workbook... Excel.Workbooks.Open strPath ' Now go and click the Calculate button and see if it works. 'Attendez que cela soit chargé ... ' Launch Excel... CreateObject("WScript.Shell").Run "excel.exe" ' Wait for it to load... WScript.Sleep 5000 ' Get the running instance... Set Excel = GetObject(, "Excel.Application") ' Open your workbook... Excel.Workbooks.Open strPath ' Now go and click the Calculate button and see if it works. WScript.Sleep 5000 ' Launch Excel... CreateObject("WScript.Shell").Run "excel.exe" ' Wait for it to load... WScript.Sleep 5000 ' Get the running instance... Set Excel = GetObject(, "Excel.Application") ' Open your workbook... Excel.Workbooks.Open strPath ' Now go and click the Calculate button and see if it works. 'Obtenir l'instance en cours d'exécution ... ' Launch Excel... CreateObject("WScript.Shell").Run "excel.exe" ' Wait for it to load... WScript.Sleep 5000 ' Get the running instance... Set Excel = GetObject(, "Excel.Application") ' Open your workbook... Excel.Workbooks.Open strPath ' Now go and click the Calculate button and see if it works. Définir Excel = GetObject (, "Excel.Application") ' Launch Excel... CreateObject("WScript.Shell").Run "excel.exe" ' Wait for it to load... WScript.Sleep 5000 ' Get the running instance... Set Excel = GetObject(, "Excel.Application") ' Open your workbook... Excel.Workbooks.Open strPath ' Now go and click the Calculate button and see if it works. 'Ouvrez votre classur ... ' Launch Excel... CreateObject("WScript.Shell").Run "excel.exe" ' Wait for it to load... WScript.Sleep 5000 ' Get the running instance... Set Excel = GetObject(, "Excel.Application") ' Open your workbook... Excel.Workbooks.Open strPath ' Now go and click the Calculate button and see if it works. Excel.Workbooks.Open strPath ' Launch Excel... CreateObject("WScript.Shell").Run "excel.exe" ' Wait for it to load... WScript.Sleep 5000 ' Get the running instance... Set Excel = GetObject(, "Excel.Application") ' Open your workbook... Excel.Workbooks.Open strPath ' Now go and click the Calculate button and see if it works. 

J'ai trouvé le problème, ouvrir des pages excel avec VBScript (ou Powershell) n'inclut pas automatiquement les Addins qui sont inclus lors de l'ouverture manuelle, donc j'ai dû append manuellement ces deux addins.

 excel.AddIns.Add("C:\Program Files (x86)\PIPC\Excel\PITrendXL.xla").Installed = True excel.AddIns.Add("C:\Program Files (x86)\PIPC\Excel\pipc32.xll").Installed = True excel.AddIns.Add ("C: \ Program Files (x86) \ PIPC \ Excel \ PITrendXL.xla"). Installé = Vrai excel.AddIns.Add("C:\Program Files (x86)\PIPC\Excel\PITrendXL.xla").Installed = True excel.AddIns.Add("C:\Program Files (x86)\PIPC\Excel\pipc32.xll").Installed = True 

Essayez d'append un module avec celui-ci

  Sub Auto_Open() ActiveWorkbook.RefreshAll Calculate End Sub Sub Auto_Open ()  Sub Auto_Open() ActiveWorkbook.RefreshAll Calculate End Sub ActiveWorkbook.RefreshAll  Sub Auto_Open() ActiveWorkbook.RefreshAll Calculate End Sub