Vba 拆分文档的Word宏正在创建一个额外页面

Vba 拆分文档的Word宏正在创建一个额外页面,vba,ms-word,Vba,Ms Word,我创建了一个宏,该宏基于用户输入,将Word文档拆分为较小的文档,然后以具有唯一名称的.pdf格式输出。但是,每个单独的文档都会在后面输出一个额外的空白页,而该页在任何时候都不在原始文档中。在保存到.pdf之前,有没有办法阻止这种情况发生/删除背面?我尝试过通过分节删除最后一页,但也没有成功 Sub SplitToPDF() Dim docMultiple As Document Dim docSingle As Document Dim rngPage As Range Dim iCurre

我创建了一个宏,该宏基于用户输入,将Word文档拆分为较小的文档,然后以具有唯一名称的.pdf格式输出。但是,每个单独的文档都会在后面输出一个额外的空白页,而该页在任何时候都不在原始文档中。在保存到.pdf之前,有没有办法阻止这种情况发生/删除背面?我尝试过通过分节删除最后一页,但也没有成功

Sub SplitToPDF()

Dim docMultiple As Document
Dim docSingle As Document
Dim rngPage As Range
Dim iCurrentPage As Integer
Dim iPageCount As Integer
Dim strNewFileName As String
Dim fDialog As FileDialog
Dim x As Integer
Dim Response As VbMsgBoxResult
Dim userInput As Integer
Dim fso
Dim currentDate As String
Dim customerName As String
Dim currentMonth As String
Dim currentYear As Integer

Response = MsgBox("Insturctions for use:" & vbNewLine & "Please ensure the first blank page has been deleted." & vbNewLine & "Please ensure you have saved (and re-named) this document to the fund operation name." & vbNewLine & vbNewLine & "This will also overwrite any other split you have done in the same folder. Continue?", vbExclamation + vbYesNo, "Warning!")
If Response = vbNo Then Exit Sub

inputData = InputBox("Please enter the length of each letter below.", "Notice length:")
If inputData = "" Then Exit Sub

'   1 Create dialog for saving and get directory details
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
With fDialog
    .Title = "Select folder to save split files"
    .AllowMultiSelect = False
    .InitialView = msoFileDialogViewList
    If .Show <> -1 Then
        MsgBox "Cancelled By User", vbInformation
        Exit Sub
    End If
    DocDir = fDialog.SelectedItems.Item(1)
End With

Application.ScreenUpdating = False

Set docMultiple = ActiveDocument
Set rngPage = docMultiple.Range
iCurrentPage = 1
iPageCount = docMultiple.BuiltInDocumentProperties(wdPropertyPages)

'   2 Loop through each page set and copy/paste data
Do Until iCurrentPage > iPageCount
    If iCurrentPage = iPageCount Then
        rngPage.End = ActiveDocument.Range.End
    Else
        Selection.GoTo wdGoToPage, wdGoToAbsolute, iCurrentPage + inputData
        rngPage.End = Selection.Start
    End If

    rngPage.Copy
    Set docSingle = Documents.Add
    docSingle.Range.Paste

    For i = 0 To docSingle.Sections.Count
    Next

    Set delSec = docSingle.Sections(i)
    delSec.Range.Delete

'   3 Variable for document name
    Application.Selection.Find.Execute "customer: "
    Application.Selection.Expand wdLine
    customerName = Replace(Application.Selection.Text, "customer: ", "")
    x = Len(customerName) - 1
    customerName = Left(customerName, x)

    Set fso = CreateObject("Scripting.FileSystemObject")

    currentDate = Replace(Date, "/", "-")
    currentMonth = Format(currentDate, "MMM")
    currentYear = Format(currentDate, "YY")
    currentDate = currentMonth & "_" & currentYear

    strNewFileName = fso.GetBaseName(docMultiple) & "_" & currentDate & "_" & customerName & ".pdf"
    docSingle.SaveAs FileName:=DocDir & "\" & strNewFileName, FileFormat:=wdFormatPDF

    iCurrentPage = iCurrentPage + inputData

    docSingle.Close SaveChanges:=wdDoNotSaveChanges
    rngPage.Collapse wdCollapseEnd
Loop

Application.ScreenUpdating = True

MsgBox "Complete", vbInformation

Set docMultiple = Nothing
Set docSingle = Nothing
Set rngPage = Nothing
Sub-splittopf()
将多个文档作为文档
将文档作为文档
Dim rngPage As范围
Dim iCurrentPage为整数
Dim iPageCount为整数
Dim strNewFileName作为字符串
Dim fDialog As FileDialog
作为整数的Dim x
作为VbMsgBoxResult的Dim响应
将用户输入设置为整数
模糊fso
将currentDate设置为字符串
将客户名称设置为字符串
将当前月份设置为字符串
将当前年份设置为整数
Response=MsgBox(“使用说明:”&vbNewLine&“请确保已删除第一个空白页。”&vbNewLine&“请确保已将此文档保存(并重新命名)到基金操作名称中。”&vbNewLine&vbNewLine&“这也将覆盖您在同一文件夹中进行的任何其他拆分。是否继续?”,vb感叹号+vbYesNo,“警告!”)
如果响应=vbNo,则退出Sub
inputData=InputBox(“请在下面输入每个字母的长度。”,“通知长度:”)
如果inputData=“”,则退出Sub
'1创建用于保存和获取目录详细信息的对话框
设置fDialog=Application.FileDialog(msoFileDialogFolderPicker)
用fDialog
.Title=“选择要保存拆分文件的文件夹”
.AllowMultiSelect=False
.InitialView=msoFileDialogViewList
如果.Show-1那么
MsgBox“已被用户取消”,vbInformation
出口接头
如果结束
DocDir=fDialog.SelectedItems.Item(1)
以
Application.ScreenUpdating=False
Set docMultiple=ActiveDocument
设置rngPage=docMultiple.Range
iCurrentPage=1
iPageCount=docMultiple.BuiltInDocumentProperties(wdPropertyPages)
'2循环浏览每个页面集并复制/粘贴数据
直到iCurrentPage>iPageCount为止
如果iCurrentPage=iPageCount,则
rngPage.End=ActiveDocument.Range.End
其他的
Selection.GoTo WDGOTOAPAGE、wdGoToAbsolute、ICCurrentPage+inputData
rngPage.End=Selection.Start
如果结束
rngPage,收到
Set docSingle=Documents.Add
docSingle.Range.Paste
对于i=0到docSingle.Sections.Count
下一个
设置delSec=docSingle.Sections(i)
delSec.Range.Delete
'3文档名称的变量
Application.Selection.Find.Execute“客户:
Application.Selection.Expand wdLine
customerName=Replace(Application.Selection.Text,“customer:”,“”)
x=Len(客户名称)-1
客户名称=左(客户名称,x)
设置fso=CreateObject(“Scripting.FileSystemObject”)
currentDate=替换(日期,“/”,“-”)
currentMonth=格式(currentDate,“MMM”)
当前年份=格式(当前日期,“YY”)
当前日期=当前月份和当前年份
strNewFileName=fso.GetBaseName(docMultiple)&“quo¤tDate&“quo&customerName&”.pdf”
docSingle.SaveAs文件名:=DocDir&“\”&strNewFileName,文件格式:=wdFormatPDF
iCurrentPage=iCurrentPage+inputData
docSingle.Close SaveChanges:=wdDoNotSaveChanges
rngPage.collapseend
环
Application.ScreenUpdating=True
MsgBox“完成”,VBS信息
Set docMultiple=Nothing
Set docSingle=Nothing
设置rngPage=Nothing
End Sub

在步骤2中(在页面集之间循环),在粘贴(第57行)之后添加以下内容:

    ' There is now an empty page at the end of the document.
    ' This is caused by a section break. Get rid of it.
    Selection.MoveLeft
    Selection.Delete

删除在各节中循环的额外代码。

您能展示您的全部代码吗?我现在已经更新了问题。谢谢。这肯定不是有效的代码,有一些明显的错误,您应该先改进。您还应该解释什么实际上是“用户输入”,以及它是如何指“拆分”过程的。