Sub xxx() Dim Para As Paragraph Dim sTitre As String Dim oRange As Range Dim oDocument As Document With ActiveDocument sTitre = "Sans titre" Set oRange = .Range(0, 0) For Each Para In .Paragraphs If Para.Style = "Titre 1" Then If Para.Range.Start > 0 Then oRange.End = Para.Range.Start - 1 ' Ici le code pour enregistrer le contenu de oRange ' avec sTitre comme nom de fichier ' genre (à approfondir) : Set oDocument = New Document With oDocument .Range.Start = 0 .Range.End = oRange.End - oRange.Start .Range.FormattedText = oRange.FormattedText ChangeFileOpenDirectory "C:\mon répertoire qui va bien" .SaveAs FileName:=sTitre, _ FileFormat:=wdFormatDocument, _ LockComments:=False, _ Password:="", _ AddToRecentFiles:=True, _ WritePassword:="", _ ReadOnlyRecommended:=False, _ EmbedTrueTypeFonts:=False, _ SaveNativePictureFormat:=False, _ SaveFormsData:=False, _ SaveAsAOCELetter:=False .Close End With Set oDocument = Nothing End If ' Mémo des nouvelles infos sTitre = Para.Range.Text If Right$(sTitre, 1) = vbCr Then sTitre = Left$(sTitre, Len(sTitre) - 1) End If oRange.Start = Para.Range.Start oRange.End = oRange.Start End If Next Para If oRange.End <> .Range.End Then ' Enregistre dernière partie oRange.End = .Range.End Set oDocument = New Document With oDocument .Range.Start = 0 .Range.End = oRange.End - oRange.Start .Range.FormattedText = oRange.FormattedText ' Ici le code pour enregistrer le contenu de oRange ' avec sTitre comme nom de fichier ' genre (à approfondir) : ChangeFileOpenDirectory "C:\mon répertoire qui va bien" .SaveAs FileName:=sTitre, _ FileFormat:=wdFormatDocument, _ LockComments:=False, _ Password:="", _ AddToRecentFiles:=True, _ WritePassword:="", _ ReadOnlyRecommended:=False, _ EmbedTrueTypeFonts:=False, _ SaveNativePictureFormat:=False, _ SaveFormsData:=False, _ SaveAsAOCELetter:=False .Close End With Set oDocument = Nothing End If End With End Sub
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionFunction formatnom(st As String) As String Dim l As Integer Dim i As Integer l = Len(st) 'Debug.Print l While (l > 0) And (Mid(st, l, 1) <> ".") ' Debug.Print l 'If l = 0 Then ' st = "Sans titre" 'Else l = l - 1 Wend l = l - 1 st = Left(st, l) st1 = "" For i = 1 To l If (Mid(st, i, 1) ".") Or (Mid(st, i, 2) ". ") Then st1 = st1 & "_" Else st1 = st1 & Mid(st, i, 1) End If Next i formatnom = st1 End Function '.SaveAs FileName:=formatnom(sTitre)