Il m'a fallu un certain temps pour comprendre comment faire, même avec l'article KB. Tout d'abord, vous devez mettre la macro dans Normal.dotm ... Ouvrez C: \ Users \ Yourname \ AppData \ Roaming \ Microsoft \ Templates \ Normal.dotm dans Word, appuyez sur Alt-F11 et collez le suivant dans le module 1:
Sub BreakOnSection()
Application.ScreenUpdating = False 'Makes the code run faster and reduces screen flicker a bit.
' Used to set criteria for moving through the document by section.
Application.Browser.Target = wdBrowseSection
strBaseFilename = ActiveDocument.Name
On Error GoTo CopyFailed
'A mail merge document ends with a section break next page.
'Note: Document may or may not end with a section break,
For I = 1 To ActiveDocument.Sections.Count
'Select and copy the section text to the clipboard.
ActiveDocument.Bookmarks("\Section").Range.Copy
'Create a new document to paste text from clipboard.
Documents.Add
Selection.Paste
DocNum = DocNum + 1
strNewFileName = Replace(strBaseFilename, ".do", "_" & Format(DocNum, "000") & ".do")
ActiveDocument.SaveAs "C:\Destination\" & strNewFileName
ActiveDocument.Close
' Move the selection to the next section in the document.
Application.Browser.Next
Next I
Application.Quit SaveChanges:=wdSaveChanges
End
CopyFailed:
'MsgBox ("No final Section Break in " & strBaseFilename)
Application.Quit SaveChanges:=wdSaveChanges
End
End Sub
Enregistrez le fichier Normal.dotm.
L'exécution de ce code divisera un document composé de plusieurs sections en plusieurs documents dans le répertoire C: \ Destination, puis fermera Word.
Vous pouvez exécuter ce billet depuis la ligne de commande via:
"c:\Program Files\Microsoft Office\Office12\WINWORD.EXE" /mBreakOnSection "C:\Path to Source\Document with multiple sections.doc"
Pour traiter tous les fichiers .doc dans un répertoire, créez un fichier batch comme suit, et l'exécuter:
@ECHO off
set "dir1=C:\Path to Source"
echo running
FOR %%X in ("%dir1%\*.doc") DO "c:\Program Files\Microsoft Office\Office12\WINWORD.EXE" /mBreakOnSection "%%~X"
echo Done
pause
Votre réponse serait meilleure si vous expliquiez un peu plus ce que fait ce code. Et si vous copiez le code sur Internet, assurez-vous de [l'attribut] (http://www.vbaexpress.com/kb/getarticle.php?kb_id=462) [it] (http://stackoverflow.com/a/27908010/578411). – rene