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?

Wordで1ページずつpdfにして保存する

Last updated at Posted at 2025-04-02

こんなくどいことする必要ある?
↑kintoneのプラグインを使ってたら必要な場面がでてきたので自分用に一応残しておきます。。

Sub SplitWordToPDF()
    Dim doc As Document
    Dim totalPages As Integer
    Dim i As Integer
    Dim tempRange As Range
    Dim pdfPath As String
    Dim startPos As Long, endPos As Long

    Set doc = ActiveDocument
    totalPages = doc.ComputeStatistics(wdStatisticPages)

    For i = 1 To totalPages
        ' ページの開始位置
        startPos = doc.GoTo(What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=i).Start
        
        ' ページの終了位置
        If i < totalPages Then
            endPos = doc.GoTo(What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=i + 1).Start - 1
        Else
            endPos = doc.Range.End ' 最終ページの場合は文書の終端を取得
        End If

        ' ページ範囲を設定
        Set tempRange = doc.Range(Start:=startPos, End:=endPos)

        ' 新しいWord文書を作成し、ページ内容をコピー
        Dim tempDoc As Document
        Set tempDoc = Documents.Add
        tempDoc.Range.FormattedText = tempRange.FormattedText
        
        ' 余白と用紙サイズをWordと揃える
        With tempDoc.PageSetup
            .TopMargin = doc.PageSetup.TopMargin
            .BottomMargin = doc.PageSetup.BottomMargin
            .LeftMargin = doc.PageSetup.LeftMargin
            .RightMargin = doc.PageSetup.RightMargin
            .PageWidth = doc.PageSetup.PageWidth
            .PageHeight = doc.PageSetup.PageHeight
        End With

        ' PDFとして保存
        pdfPath = doc.Path & "\Page_" & i & ".pdf"
        tempDoc.ExportAsFixedFormat OutputFileName:=pdfPath, ExportFormat:=wdExportFormatPDF

        ' 一時的なドキュメントを閉じる
        tempDoc.Close False
    Next i

    MsgBox "PDF出力しました", vbInformation
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?