Public Type Type_Page
Page_Name As String
Page_Cnt As String
End Type
Public TypePage() As Type_Page
Sub PDF_Proc()
Dim fol_Path As String
Dim sh As Worksheet
Dim i As Long
Dim j As Long
Dim File_Path As String
Dim FSO As Scripting.FileSystemObject
Dim str_Array As Variant
Dim From_Page As Variant
Dim To_Page As Variant
ReDim Preserve TypePage(0)
Set FSO = New Scripting.FileSystemObject
j = 1
Set sh = ThisWorkbook.Sheets("PDFページ")
fol_Path = "C:\Users\User\Desktop\VBA関連\VBA練習\PDF_テスト"
With sh
TypePage(UBound(TypePage)).Page_Name = Trim(.Cells(1, "B").Value)
TypePage(UBound(TypePage)).Page_Cnt = 1 & ","
For i = 1 To sh.PageSetup.Pages.Count - 1
If TypePage(UBound(TypePage)).Page_Name = Trim(.Cells(.HPageBreaks(i).Location.Row, "B").Value) Then
TypePage(UBound(TypePage)).Page_Cnt = TypePage(UBound(TypePage)).Page_Cnt & i + 1 & ","
Else
If i <> 1 _
Or TypePage(UBound(TypePage)).Page_Name <> Trim(.Cells(.HPageBreaks(i).Location.Row, "B").Value) Then
TypePage(UBound(TypePage)).Page_Cnt = Left(TypePage(UBound(TypePage)).Page_Cnt, Len(TypePage(UBound(TypePage)).Page_Cnt) - 1)
ReDim Preserve TypePage(UBound(TypePage) + 1)
End If
TypePage(UBound(TypePage)).Page_Name = Trim(.Cells(.HPageBreaks(i).Location.Row, "B").Value)
TypePage(UBound(TypePage)).Page_Cnt = i + 1 & ","
End If
Next i
TypePage(UBound(TypePage)).Page_Cnt = Left(TypePage(UBound(TypePage)).Page_Cnt, Len(TypePage(UBound(TypePage)).Page_Cnt) - 1)
End With
For i = LBound(TypePage) To UBound(TypePage)
File_Path = FSO.BuildPath(fol_Path, "test_" & i + 1 & ".pdf")
str_Array = Split(TypePage(i).Page_Cnt, ",")
From_Page = str_Array(LBound(str_Array))
To_Page = str_Array(UBound(str_Array))
sh.ExportAsFixedFormat Type:=xlTypePDF, FileName:=File_Path, _
From:=From_Page, _
To:=To_Page
Next i
End Sub
'カッコのつけ方と範囲が根本的におかしいかもしれないので、一通り見直してみる
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme