|
简单写了一个
Sub test1()
Dim p As Paragraph
Dim i As Long
Dim row() As Long
Dim v_range As Range
i = 0
Application.ScreenUpdating = False
ReDim row(0) As Long
'获取段落
For i = 1 To ActiveDocument.Paragraphs.Count
s = ActiveDocument.Paragraphs(i).Range.Text '取段落文本
'判断方法,段落中存在“表”字,并且为黑体
pos = InStr(1, s, "表")
blodflag = ActiveDocument.Paragraphs(i).Range.Font.Bold
If pos > 0 And blodflag Then
row(UBound(row)) = i
ReDim Preserve row(UBound(row) + 1) As Long
End If
Next i
'按段落生成新文档
For i = 0 To UBound(row) - 1
'选择区域,从标题开始,至下一个标题为止。如果是最后一个段落,则取到文档末尾,否则取下一个段落之前
If i = UBound(row) - 1 Then
Set v_range = ActiveDocument.Range(ActiveDocument.Paragraphs(row(i)).Range.Start, ActiveDocument.Paragraphs(ActiveDocument.Paragraphs.Count).Range.End)
Else
Set v_range = ActiveDocument.Range(ActiveDocument.Paragraphs(row(i)).Range.Start, ActiveDocument.Paragraphs(row(i + 1)).Range.Start - 1)
End If
v_name = ActiveDocument.Paragraphs(row(i)).Range.Text '获取标题
v_name = Mid(v_name, 1, Len(v_name) - 1) '去掉段落标记
v_name = ActiveDocument.Path + "\" + v_name + ".doc" '以标题作为文件名
v_range.Copy '复制
'Set file = Documents.Add(Template:=ActiveDocument.Path + "\表11.dotx", Visible:=False) '按模板新建文档
Set file = Documents.Add(Visible:=False) '按模板新建文档
file.Paragraphs(1).Range.Paste '粘贴内容
file.SaveAs2 FileName:=v_name, fileformat:=wdFormatDocument '保存文件
file.Close '关闭文件
Next i
Application.ScreenUpdating = True
End Sub
|
|