excel 将Word文档拆分成多个PDF文档,每页2页或更多

j2cgzkjk  于 2023-05-19  发布在  其他
关注(0)|答案(1)|浏览(217)

我真的不擅长写vba代码,我更喜欢R和统计学,但我一直在尝试自动化一些我必须手动编写的邮件。所有的信息都来自我使用的Excel和通信,我可以制作几个文档,然后将大文档拆分为几个PDF文档将是理想的。
请帮帮我
我尝试了几个代码来使它工作,最后一个是这样的:

Sub Dividirdocumentos()
Dim iSplit As Long,
 iCount As Long,
 iLast As Long
Dim RngSplit As Range,
 StrDocName As String,
 StrDocExt As String
With ActiveDocument
iSplit = InputBox("El documento contiene " & .ComputeStatistics(wdStatisticPages) & " páginas." _
& vbCr & "¿Cuál es el número de páginas por el que quiere dividir?",
 "DividirDocumentos")
StrDocName = .FullName
StrDocExt = "." & Split(StrDocName, ".")(UBound(Split(StrDocName, ".")))
StrDocName = Left(StrDocName, Len(StrDocName) - Len(StrDocExt)) & "_"
For iCount = 0 To Int(.ComputeStatistics(wdStatisticPages) / iSplit)
If .ComputeStatistics(wdStatisticPages) > iSplit Then
iLast = iSplit
Else
iLast = .ComputeStatistics(wdStatisticPages)
End If
Set RngSplit = .GoTo(What:=wdGoToPage, Name:=iLast)
Set RngSplit = RngSplit.GoTo(What:=wdGoToBookmark, Name:="\page")
RngSplit.Start = .Range.Start
RngSplit.Cut
Documents.Add
Selection.Paste
ActiveDocument.SaveAs FileName:=StrDocName & iCount + 1 & StrDocExt, AddToRecentFiles:=False
ActiveWindow.Close
Next iCount
Set RngSplit = Nothing
'.Close Savechanges:=False
End With
End Sub

这一个,允许我把它们拆成偶数页,但它们不是PDF格式的,我也不能选择每个文档拆成多少页。我有一个文档,其中有一节我必须手动编辑,每个文档的长度都是可变的。

szqfcxe2

szqfcxe21#

我一直在思考如何开发必要的代码,我想我已经找到了一个有用的答案。如果你发现自己处于和我一样的境地,我会和你分享:
我在VBA中创建了一个代码,允许在每个循环的开始处选择要拆分的文档,然后选择要拆分的文档的页数以及从哪个页面开始。之后,创建一个包含该内容的PDF文档,并保存不包含该部分的单词。然后,它根据需要重复循环多次,直到您输入页数为0,从而结束循环。
代码如下:

Sub ExtractPagesToPDF()
    'Declare variables
    Dim startPage As Integer
    Dim endPage As Integer
    Dim numPages As Integer
    Dim selectedDoc As Document
    Dim fileName As String
    Dim savePath As String
    
    'Prompt user to select Word document
    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        .Title = "Select the Word document to extract pages from"
        .Filters.Clear
        .Filters.Add "Word Documents", "*.docx;*.docm;*.doc;*.dotx;*.dotm;*.dot"
        .FilterIndex = 1
        
        If .Show = -1 Then 'if user selects a file
            fileName = .SelectedItems(1)
            'Get the path of the selected file
            savePath = Left(fileName, InStrRev(fileName, "\"))
        Else 'if user cancels
            MsgBox "No document selected.", vbCritical
            Exit Sub
        End If
    End With
    
    'Open the selected document
    Set selectedDoc = Documents.Open(fileName)
    
    'Start loop
    Do
        'Ask user to input number of pages to extract
        numPages = InputBox("Enter the number of pages to extract to PDF (or enter 0 to stop):")
        
        'Check if user wants to stop loop
        If numPages = 0 Then
            MsgBox "El proceso ha terminado con éxito", vbInformation
            Exit Do
        End If
        
        'Ask user to input start page number
        startPage = InputBox("Enter the starting page number:")
        
        'Calculate end page number
        endPage = startPage + numPages - 1
        
        'Extract pages to PDF
        With selectedDoc
            .ExportAsFixedFormat OutputFileName:=savePath & "ExtractedPages_" & startPage & "-" & endPage & ".pdf", ExportFormat:=wdExportFormatPDF, Range:=wdExportFromTo, From:=startPage, To:=endPage, OpenAfterExport:=False
        End With
        
    Loop
    
    'Close the document
    selectedDoc.Close savechanges:=False
End Sub

相关问题