将Word文档拆分为单独的PDF

问题描述 投票:0回答:1

我有一个15页的word文档,我想将该文档的每3页导出到桌面上的一个单独的pdf中,并根据名称列表为每个pdf命名。

我使用了以下代码,但它无法正常工作,我认为这是因为

c
变量不好。

任何想法都可以帮助我,谢谢!

Public Sub split_3_pages()


Dim MyArray As Variant
Dim fileName, myPath As String
myPath = "path\Desktop\" 


MyArray = Array( "Name 1",  "Name 2",  "Name 3",  "Name 4",  "Name 5",  "Name 6",
"Name 7",  "Name 8",  "Name 9",  "Name 10",  "Name 11",  "Name 12",  "Name 13",  "Name
14",  "Name 15")


For a = 1 To 15
b = a + 2
c = 0

    fileName = myPath & MyArray(c)

ActiveDocument.ExportAsFixedFormat OutputFileName:= _
    fileName _
    , ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
    wdExportOptimizeForPrint, Range:=wdExportFromTo, From:=a, To:=b, Item:= _
    wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
    CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
    BitmapMissingFonts:=True, UseISO19005_1:=False
a = a + 2
c = c + 1 
Next
End Sub
vba ms-word office365
1个回答
0
投票
  • Step
    用于
    For
    子句中,指定每次更改的金额计数器。

  • 语句

    c = 0
    应放在for循环之外;否则,
    c
    的值将始终是
    zero

微软文档:

对于...下一个声明

请尝试一下。

Option Explicit
Public Sub split_3_pages()
    Dim MyArray As Variant
    Dim fileName, myPath As String
    myPath = "path\Desktop\"
    MyArray = Array("Name 1", "Name 2", "Name 3", "Name 4", "Name 5", _
        "Name 6", "Name 7", "Name 8", "Name 9", "Name 10", "Name 11", _
        "Name 12", "Name 13", "Name14", "Name 15")
    Dim sPg As Long, ePg As Long, c As Long
    Const MAX_PG = 15
    c = 0
    For sPg = 1 To MAX_PG Step 3
        ePg = sPg + 2
        If ePg > MAX_PG Then ePg = MAX_PG
'        Debug.Print sPg, ePg, MyArray(c)
        fileName = myPath & MyArray(c)
        ActiveDocument.ExportAsFixedFormat OutputFileName:=fileName, _
        ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, _
        OptimizeFor:=wdExportOptimizeForPrint, Range:=wdExportFromTo, _
        From:=sPg, To:=ePg, Item:=wdExportDocumentContent, _
        IncludeDocProps:=True, KeepIRM:=True, _
        CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
        BitmapMissingFonts:=True, UseISO19005_1:=False
        c = c + 1
    Next
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.