I have a word document of 15 pages, I want to export every 3 pages of this document to a separate pdf on my desktop and name each pdf according to a list of names.
I used the following code but it doesn't work correctly, and I think it's because the c
variable which is not good.
Any idea can help me thanks!
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
Step
is used in For
clause to specify the amount counter is changed each time.
The statement c = 0
should be placed outside the for loop; otherwise, the value of c
will always be zero
.
Microsoft documentation:
Please try.
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