190 - Suppression protection VBA

This commit is contained in:
2025-09-11 10:09:46 +02:00
parent 21560f9e7b
commit 49b85a51c6
7 changed files with 149 additions and 145 deletions

View File

@@ -1,31 +1,66 @@
' set_cell_silent.vbs
' Usage: cscript //nologo set_cell_silent.vbs "C:\x\file.xlsm" "Feuille" "A1" "Valeur"
' Usage: cscript //nologo set_cell_silent.vbs "C:\chemin\fichier.xlsm" "NomFeuille" "A1" "Valeur"
Option Explicit
Dim f, sheetName, addr, val
Dim xl, wb, ws
If WScript.Arguments.Count < 4 Then
WScript.Echo "[ERR] Args: set_cell_silent.vbs <fichier.xlsm> <feuille> <cellule> <valeur>"
WScript.Quit 1
End If
f = WScript.Arguments(0)
sheetName = WScript.Arguments(1)
addr = WScript.Arguments(2)
val = WScript.Arguments(3)
On Error Resume Next
If WScript.Arguments.Count < 4 Then WScript.Quit 1
Dim fpath, sname, addr, val
fpath = WScript.Arguments(0)
sname = WScript.Arguments(1)
addr = WScript.Arguments(2)
val = WScript.Arguments(3)
Dim xl, wb, ws
Set xl = CreateObject("Excel.Application")
xl.DisplayAlerts = False
If Err.Number <> 0 Then
WScript.Echo "[ERR] Excel non disponible (" & Err.Description & ")"
WScript.Quit 1
End If
On Error GoTo 0
xl.Visible = False
xl.AutomationSecurity = 3 ' macros OFF
xl.EnableEvents = False ' événements OFF
xl.DisplayAlerts = False
Set wb = xl.Workbooks.Open(fpath, 0, False)
If Err.Number <> 0 Then xl.Quit: WScript.Quit 2
' Désactiver macros et événements AVANT douvrir
On Error Resume Next
xl.AutomationSecurity = 3 ' msoAutomationSecurityForceDisable
xl.EnableEvents = False
xl.ScreenUpdating = False
On Error GoTo 0
Set ws = wb.Worksheets(sname)
If Err.Number <> 0 Then wb.Close False: xl.Quit: WScript.Quit 3
On Error Resume Next
Set wb = xl.Workbooks.Open(f, False, False)
If Err.Number <> 0 Then
xl.Quit
WScript.Echo "[ERR] Ouverture classeur: " & Err.Description
WScript.Quit 1
End If
On Error GoTo 0
ws.Range(addr).Value = val
If Err.Number <> 0 Then wb.Close False: xl.Quit: WScript.Quit 4
On Error Resume Next
Set ws = wb.Worksheets.Item(sheetName)
If Err.Number <> 0 Then
wb.Close False
xl.Quit
WScript.Echo "[ERR] Feuille introuvable: " & sheetName
WScript.Quit 1
End If
On Error GoTo 0
On Error Resume Next
ws.Range(addr).Value2 = val
If Err.Number <> 0 Then
wb.Close False
xl.Quit
WScript.Echo "[ERR] Ecriture cellule " & addr & " : " & Err.Description
WScript.Quit 1
End If
On Error GoTo 0
wb.Save
wb.Close False