Сделайте так:
1. Откройте VBAредактор
2. Щелкните правым кликом на проекте Normal
3. В открывшемся меню выберете Insert-->Module
4. Запомните номер Module
5. Скопируйте в него код
Public Fold1 As String
Public FoldsPref As String
Public FoldName As String
Public FileName As String
Private Function SelFiles() As Collection
Dim col As Collection, pr As Collection, col1 As Collection
Dim Prg As Paragraph, Rng As Range, WD As Range
Dim NameFold As String, i As Byte, Name As String
Selection.Find.ClearFormatting
Selection.Find.Font.Bold = False
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "([!^0013])([^0013])([!^0013])"
.Replacement.Text = "\1 \3"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Set SelFiles = New Collection
For Each Prg In Selection.Paragraphs
Set pr = New Collection
Set Rng = Prg.Range
If Rng.Words(1).Font.Bold = True Then
i = i + 1
Set col = New Collection
col.Add Rng.Words(1).Characters.First.Text, "Pref"
NameFold = ""
For Each WD In Rng.Words
If WD.Font.Bold = True Or WD.Text = Chr(12) Then
NameFold = NameFold & WD
Else
Exit For
End If
Next WD
NameFold = Trim(NameFold)
For j = Len(NameFold) To 1
If Mid(NameFold, j, 1) = "," Then
NameFold = Mid(NameFold, j - 1, 1)
NameFold = Trim(NameFold)
End If
Next j
col.Add NameFold, "NameFold"
col.Add Prg.Range, "Range"
col.Add Trim(Rng.Words(1)) & ".doc", "Name"
SelFiles.Add col
End If
Next Prg
End Function
Public Sub SaveFiles()
Dim mFiles As Collection, Doc1 As Document
Dim appWD As Application, Doc2 As Document, i As Long
Dim col As Collection, Rng As Range
Set mFiles = SelFiles
Set Doc1 = ActiveDocument
Set appWD = New Application
appWD.Visible = False
On Error Resume Next
Fold1 = InputBox("Введите каталог сохранения файлов!")
If Fold1 = "" Then Exit Sub
If Mid(Fold1, Len(Fold1), 1) = "\" Then
Fold1 = Trim(Mid(Fold1, 1, Len(Fold1) - 1))
End If
On Error Resume Next
MkDir Fold1
For i = 1 To mFiles.Count
Set col = mFiles(i)
On Error Resume Next
MkDir Fold1 & "\" & col("Pref")
FoldsPref = Fold1 & "\" & col("Pref")
MkDir FoldsPref & "\" & col("NameFold")
FoldName = FoldsPref & "\" & col("NameFold")
FileName = FoldName & "\" & col("Name")
Set Rng = col("Range")
With appWD
Set Doc2 = .Documents.Add
Rng.Copy
Rng.HighlightColorIndex = wdRed
Doc2.Range.PasteAndFormat (wdFormatOriginalFormatting)
Doc2.SaveAs2 FileName:=FileName, FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles:= _
True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False, CompatibilityMode:=0
Doc2.Close
End With
Next i
appWD.Quit
MsgBox "Создано " & mFiles.Count & " файлов", vbInformation
End Sub
Если 1-я строка в модуле Option Explicit Удалите ее
5. Нажмите Ctrl+S
6. Закройте VBA
7. Откройте тестируемый файл
8. Выделите любое количество фамилий
9. Откройте меню макросов и выберете нужный макрос например Normal.Module6.SaveFiles
10. Запустите макрос
11. В открывшемся окне наберите путь каталога например C:\myTest
Если этого пути не существует, макрос автоматом создаст каталог
12. Нажмите OK и дождитесь окончания макроса
13. Откройте каталог C:\myTest и проверьте сохраненные файлы