Comment renommer simultanément un fichier dans MS Office et supprimer l’ancienne version ?
La manière la « plus simple » de répondre à cette question semble être de s’appuyer considérablement sur cette réponse.
-
Insérez le code suivant dans le modèle normal.dotm (situé dans
C:\Documents and Settings\user name\Application Data\Microsoft\Templatespour Windows 7 pour Word) -
Enregistrez normal.dotm
-
Ajoutez ceci à la barre d’outils d’accès rapide dans Word.
-
Optionnel - réaffectez un raccourci clavier à ceci
-
Optionnel - signez numériquement votre modèle (recommandé)
Notez que ceci déplace en fait l’ancien fichier vers la Corbeille plutôt que de le supprimer complètement, et définit également le nouveau nom de fichier de manière très pratique.
Option Explicit
'To send a file to the recycle bin, we'll need to use the Win32 API
'We'll be using the SHFileOperation function which uses a 'struct'
'as an argument. That struct is defined here:
Private Type SHFILEOPSTRUCT
hwnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAnyOperationsAborted As Long
hNameMappings As Long
lpszProgressTitle As Long
End Type
' function declaration:
Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
'there are some constants to declare too
Private Const FO_DELETE = &H3
Private Const FOF_ALLOWUNDO = &H40
Private Const FOF_NOCONFIRMATION = &H10
Private Const FOF_SILENT = &H4
Function RecycleFile(FileName As String, Optional UserConfirm As Boolean = True, Optional HideErrors As Boolean = False) As Long
'This function takes one mandatory argument (the file to be recycled) and two
'optional arguments: UserConfirm is used to determine if the "Are you sure..." dialog
'should be displayed before deleting the file and HideErrors is used to determine
'if any errors should be shown to the user
Dim ptFileOp As SHFILEOPSTRUCT
'We have declared FileOp as a SHFILEOPSTRUCT above, now to fill it:
With ptFileOp
.wFunc = FO_DELETE
.pFrom = FileName
.fFlags
*(Réponse tronquée)*