|
楼主,我是在2003中测试的,不敢保证在2007或以上正确。请先备份原文件后再测试,测试完毕与原文件对照一下以保正确。
- Sub SaveAsPage_New()
- Dim PageCount As Integer, StartRange As Long, EndRange As Long, MyRange As Range, Fn As String, MyDoc As Document, i As Long
- On Error Resume Next
- If Dir("D:\通知书", vbDirectory) <> "" Then '判断文件夹是否存在
- MsgBox "文件夹存在", , "提示" '提示
- Else
- MsgBox "文件夹不存在!在D盘创建一个名为“通知书”的文件夹", , "提示" '文件夹不存在的提示
- MkDir "D:\通知书"
- End If
- ChangeFileOpenDirectory "D:\通知书"
- PageCount = Selection.Information(wdNumberOfPagesInDocument)
- ' Range(0, 0).Select '将光标移至文档起点
- Selection.HomeKey unit:=wdStory
- For i = 1 To PageCount '设置循环次数
- StartRange = Selection.Start '取得该页的第一个字符位置
- Selection.EndKey unit:=wdLine '将光标移动到该页首行的最后位置
- ' Fn = "FILE" & i & ".DOC"
- If i = PageCount Then '如果循环到达最后一页
- EndRange = ActiveDocument.Content.End '将文档最后位置赋值于EndRange
- Else
- Selection.GoToNext (wdGoToPage) '否则,将下一页的起始位置赋值于EndRange(等同于本页的最后位置)
- EndRange = Selection.Start
- End If
- Set MyRange = ActiveDocument.Range(StartRange, EndRange) '将本页中的内容进行复制
- MyRange.Copy
- Set MyDoc = Documents.Add '新建一空白文档
- MyDoc.Range(0, 0).Paste '在文档开始处粘贴
-
- '取得学生名字
- Selection.HomeKey unit:=wdStory
- Selection.ClearFormatting
- Selection.Find.Execute findtext:="学生", Forward:=True, Wrap:=wdFindStop
- If Selection.Find.Found = True Then
- Do
- Selection.MoveEnd unit:=wdCharacter, Count:=1
- Loop Until Selection Like "*已于"
- Selection.MoveStart unit:=wdCharacter, Count:=2
- Selection.MoveEnd unit:=wdCharacter, Count:=-2
- End If
-
- Fn = Selection.Text
- Fn = Fn & ".doc"
- '
- ActiveDocument.Paragraphs.Last.Previous.Range.Delete
- ActiveDocument.Paragraphs.Last.Range.Select
- '最后一段,设置为1磅
- With Selection.Font
- .Size = 1
- .Kerning = 0
- .DisableCharacterSpaceGrid = True
- End With
- With Selection.ParagraphFormat
- .LineSpacing = LinesToPoints(0.25)
- .AutoAdjustRightIndent = False
- .DisableLineHeightGrid = True
- End With
- '''
- MyDoc.SaveAs FileName:=Fn '保存文档名
- MyDoc.Close '关闭文档
- Next
- End Sub
复制代码 |
|