|
我也遇到了同样的问题,下面代码把文档按两页拆分另存为新文档没问题,按一页拆分就会提示“ .LeftMargin = sinLeft”有问题,超出范围还是什么。
Sub SplitEveryFivePagesAsDocuments()
Dim oSrcDoc As Document, oNewDoc As Document
Dim strSrcName As String, strNewName As String, strNewDot As String
Dim oRange As Range
Dim nIndex As Integer, nSubIndex As Integer, nTotalPages As Integer, nBound As Integer
Dim fso As Object
Dim sinLeft As Single, sinRight As Single, sinTop As Single, sinBottom As Single
Dim i As Integer, N As Integer
Dim pgOrientation As WdOrientation
Application.ScreenUpdating = False '关闭屏幕更新
Const nSteps = 2 ' 修改这里控制每隔几页分割一次
Set fso = CreateObject("Scripting.FileSystemObject")
Set oSrcDoc = ActiveDocument
Set oRange = oSrcDoc.Content
nTotalPages = ActiveDocument.Content.Information(wdNumberOfPagesInDocument)
oRange.Collapse wdCollapseStart
oRange.Select
For nIndex = 1 To nTotalPages Step nSteps
Set oNewDoc = Documents.Add
If nIndex + nSteps > nTotalPages Then
nBound = nTotalPages
Else
nBound = nIndex + nSteps - 1
End If
For nSubIndex = nIndex To nBound
oSrcDoc.Activate
oSrcDoc.Bookmarks("\page").Range.Copy
'读取页面设置
With oSrcDoc.Bookmarks("\page").Range.Sections(1).PageSetup
sinLeft = .LeftMargin '左页边距
sinRight = .RightMargin '右页边距
sinTop = .TopMargin '上边距
sinBottom = .BottomMargin '下边距
pgOrientation = .Orientation '纸张方向
End With
oSrcDoc.Windows(1).Activate
Application.Browser.Target = wdBrowsePage
Application.Browser.Next
oNewDoc.Activate
oNewDoc.Windows(1).Selection.Paste
oNewDoc.Background.Fill.Transparency = oSrcDoc.Background.Fill.Transparency
oNewDoc.Background.Fill.PresetTextured msoTexturePinkTissuePaper
With oNewDoc.PageSetup
.Orientation = pgOrientation
.LeftMargin = sinLeft
.RightMargin = sinRight
.TopMargin = sinTop
.BottomMargin = sinBottom
End With
Next nSubIndex
'删除空白段落
N = 0
temp = ActiveDocument.Paragraphs.Count
For i = temp To 1 Step -1
If Len(Trim(ActiveDocument.Paragraphs(i).Range)) = 1 Then
ActiveDocument.Paragraphs(i).Range.Delete
N = N + 1
Else
Exit For
End If
Next
strSrcName = oSrcDoc.FullName
strNewName = fso.BuildPath(fso.GetParentFolderName(strSrcName), _
fso.GetBaseName(strSrcName) & "_" & (nIndex \ nSteps) & "." & fso.GetExtensionName(strSrcName))
oNewDoc.SaveAs strNewName
oNewDoc.Close False
Next nIndex
Set oNewDoc = Nothing
Set oRange = Nothing
Set oSrcDoc = Nothing
Set fso = Nothing
Application.ScreenUpdating = True '恢复屏幕更新
MsgBox "结束!"
End Sub
下面这段代码按一页拆分后面就会有一页空白页而且删不掉。
Sub SplitEveryFivePagesAsDocuments()
Dim oSrcDoc As Document, oNewDoc As Document
Dim strSrcName As String, strNewName As String
Dim oRange As Range
Dim nIndex As Integer, nSubIndex As Integer, nTotalPages As Integer, nBound As Integer
Dim fso As Object
Const nSteps = 1 ' 修改这里控制每隔几页分割一次
Set fso = CreateObject("Scripting.FileSystemObject")
Set oSrcDoc = ActiveDocument
Set oRange = oSrcDoc.Content
nTotalPages = ActiveDocument.Content.Information(wdNumberOfPagesInDocument)
oRange.Collapse wdCollapseStart
oRange.Select
For nIndex = 1 To nTotalPages Step nSteps
Set oNewDoc = Documents.Add
If nIndex + nSteps > nTotalPages Then
nBound = nTotalPages
Else
nBound = nIndex + nSteps - 1
End If
For nSubIndex = nIndex To nBound
oSrcDoc.Activate
oSrcDoc.Bookmarks("\page").Range.Copy
oSrcDoc.Windows(1).Activate
Application.Browser.Target = wdBrowsePage
Application.Browser.Next
oNewDoc.Activate
oNewDoc.Background.Fill.Transparency = 0#
oNewDoc.Background.Fill.PresetTextured msoTexturePinkTissuePaper
oNewDoc.Windows(1).Selection.Paste
Next nSubIndex
strSrcName = oSrcDoc.FullName
strNewName = fso.BuildPath(fso.GetParentFolderName(strSrcName), _
fso.GetBaseName(strSrcName) & "_" & (nIndex \ nSteps + 1) & "." & fso.GetExtensionName(strSrcName))
oNewDoc.SaveAs strNewName
oNewDoc.Close False
Next nIndex
Set oNewDoc = Nothing
Set oRange = Nothing
Set oSrcDoc = Nothing
Set fso = Nothing
MsgBox "结束!"
End Sub
|
|