0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

テスト108

Last updated at Posted at 2024-06-24

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

'カッコのつけ方と範囲が根本的におかしいかもしれないので、一通り見直してみる
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?