|
不偷工减料应该这样:
Sub 每两页一文档()
On Error Resume Next
Dim s%, mydoc
For s = 1 To ActiveDocument.Range.Information(wdNumberOfPagesInDocument) Step 2
With ActiveDocument
.Range(.GoTo(wdGoToPage, wdGoToNext, , s).Start, VBA.IIf(s + 2 < ActiveDocument.Range.Information(wdNumberOfPagesInDocument), .GoTo(wdGoToPage, wdGoToNext, , s + 2).Start, .Content.End)).Copy
End With
Set mydoc = Documents.Add(Visible:=False)
With mydoc
.Content.Paste
.SaveAs getfirstvisibletextcontent(mydoc) & ".doc" '此处偷工减料没有采用word默认文件名,直接数字了
.Close
End With
Set mydoc = Nothing
Next
End Sub
'获取指定行第一行可见文字
Function getfirstvisibletextcontent(odoc)
Dim oparagraph
Dim strcontent
For Each oparagraph In odoc.Paragraphs
strcontent = getsafefilename(oparagraph.Range.Text)
If Len(strcontent) <> 1 Then
getfirstvisibletextcontent = strcontent
Exit Function
End If
Next
getfirstvisibletextcontent = ""
End Function
'过滤文件名中的无效字符
Function getsafefilename(strfilename)
Dim arrunsafecharacters, strunsafechar
Dim nlndex
arrunsafecharacters = Array("\", "/", ":", "*", "?", """", "<", ">", "|")
For nlndex = 0 To &H2F
strfilename = Replace(strfilename, Chr(nlndex), "")
Next
For Each strunsafechar In arrunsafecharacters
strfilename = Replace(strfilename, strunsafechar, "")
Next
getsafefilename = Left(Trim(strfilename), 20)
'getsafefilename = Left(Trim(strfilename), g_ntitlemaxlen)
End Function |
|