Vba 按字符串拆分文档并输出为.pdf-拆分函数不起作用

Vba 按字符串拆分文档并输出为.pdf-拆分函数不起作用,vba,ms-word,Vba,Ms Word,我有一个大的word文档(100页),我正在尝试将文档拆分为几个较小的文档,然后将每个文档保存为.pdf格式,保存到用户定义的位置。到目前为止,我已将其按分隔符拆分并保存为.pdf格式,一点问题也没有。 另一方面,split函数似乎删除了页面中的所有格式,只是以纯文本的形式输出。因为格式化是必要的,有什么办法可以解决这个问题吗?巧合的是,拆分是指每3页。。。是否有办法将范围设置为3页块 Sub SplitNotes(delim As String, strFileName As String)

我有一个大的word文档(100页),我正在尝试将文档拆分为几个较小的文档,然后将每个文档保存为.pdf格式,保存到用户定义的位置。到目前为止,我已将其按分隔符拆分并保存为.pdf格式,一点问题也没有。
另一方面,split函数似乎删除了页面中的所有格式,只是以纯文本的形式输出。因为格式化是必要的,有什么办法可以解决这个问题吗?巧合的是,拆分是指每3页。。。是否有办法将范围设置为3页块

Sub SplitNotes(delim As String, strFileName As String)
    Dim fDialog As FileDialog
    Dim X
    Dim Doc As Document

    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"
            Exit Sub
        End If
    DocDir = fDialog.SelectedItems.Item(1)

        arrNotes = Split(ActiveDocument.Range, delim)

        Response = MsgBox("This will split the document into " & UBound(arrNotes) + 1 & " sections. Do you wish to preceed?", 4)
        If Response = 7 Then Exit Sub
        For I = LBound(arrNotes) To UBound(arrNotes)
            If Trim(arrNotes(I)) <> "" Then
            X = X + 1
            ActiveDocument.Range = arrNotes(I)
            ActiveDocument.Range.Copy
            Set Doc = Documents.Add
            ActiveDocument.Range.Paste
            ActiveDocument.SaveAs FileName:=DocDir & "\" & X & ".PDF", FileFormat:=wdFormatPDF
            ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
            End If
        Next     
    End With
End Sub

Sub test()
    'delimiter & filename
SplitNotes "MyText", "Notes "
End Sub
子拆分注释(delim作为字符串,strFileName作为字符串)
Dim fDialog As FileDialog
暗X
将文档变为文档
设置fDialog=Application.FileDialog(msoFileDialogFolderPicker)
用fDialog
.Title=“选择要保存拆分文件的文件夹”
.AllowMultiSelect=False
.InitialView=msoFileDialogViewList
如果.Show-1那么
MsgBox“已被用户取消”
出口接头
如果结束
DocDir=fDialog.SelectedItems.Item(1)
arrNotes=Split(ActiveDocument.Range,delim)
Response=MsgBox(“这会将文档拆分为“&UBound(arrNotes)+1&”部分。是否要继续?”,4)
如果响应=7,则退出子系统
对于I=LBound(arrNotes)到UBound(arrNotes)
如果修剪(注释(I))”“那么
X=X+1
ActiveDocument.Range=arrNotes(I)
ActiveDocument.Range.Copy
设置文档=文档。添加
ActiveDocument.Range.Paste
ActiveDocument.SaveAs文件名:=DocDir&“\”&X&“.PDF”,文件格式:=wdFormatPDF
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
如果结束
下一个
以
端接头
子测试()
'分隔符和文件名
拆分注释“MyText”、“注释”
端接头

以下代码将选择三个页面并将其复制到一个新页面,该页面将保存为PDF,然后选择并复制接下来的三个页面,依此类推

空的附加页面仍然存在一些问题,我现在无法解决

Sub SplitDocument()

Dim rgePages As Range

iCurrentPage = 1
Set docMultiple = ActiveDocument
iPageCount = docMultiple.Content.ComputeStatistics(wdStatisticPages)

counter = 0
Do Until (counter * 3) + 1 > iPageCount

    Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=(counter * 3) + 1
    Set rgePages = Selection.Range
    counter = counter + 1
    If counter * 3 > iPageCount Then
        Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=iPageCount
    Else
        Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=(counter * 3)
    End If
    rgePages.End = Selection.Bookmarks("\Page").Range.End
    rgePages.Select

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

    strNewFileName = "C:\Temp\DocumentPart" & "_" & counter & ".PDF"
    docSingle.SaveAs strNewFileName, FileFormat:=wdFormatPDF

    docSingle.Close SaveChanges:=False
    rgePages.Collapse wdCollapseEnd
Loop

End Sub