[[20200124134355]] 『PDFファイルを結合』(ともちゃん) ページの最後に飛ぶ

[ 初めての方へ | 一覧(最新更新順) | 全文検索 | 過去ログ ]

 

『PDFファイルを結合』(ともちゃん)

以下のVBAがあります。
「Data」のシートから「予算概要書」へ差込印刷となる設定になっております。
元々はPDFではなく
以下のコードとなっておりました。

印刷プレビュー

        Sheets("予算概要書").PrintOut

となっておりました。
これをPDF出力にということで、保存フォルダの中にPDFファイルが保存されるように以下のとおり変更しました。

一応VBAは動くことができました。

しかし、上司からPDFを単票ではなく全体を結合して出力できないかと
相談されましたが、どのようにしたらよいかさっぱり分からず投稿させていただきました。

Sub 事業概要書()

    Dim myrow As Integer
    Dim jigyocd As String

    Dim smkcd As String
    Dim smk As String
    Dim kaikeicd As String
    Dim kaikei As String
    Dim kancd As String
    Dim kan As String
    Dim koucd As String
    Dim kou As String
    Dim mokcd As String
    Dim mok As String

    Dim busyo As String
    Dim page As String
    Dim kubun As String

    Dim t_yosan As Long
    Dim z_yosan As Long
    Dim h_yosan As Long
    Dim t_kuniken As Long
    Dim z_kuniken As Long
    Dim h_kuniken As Long
    Dim t_kisai As Long
    Dim z_kisai As Long
    Dim h_kisai As Long
    Dim t_sonota As Long
    Dim z_sonota As Long
    Dim h_sonota As Long
    Dim t_ippan As Long
    Dim z_ippan As Long
    Dim h_ippan As Long

    Dim gaiyou As String

    Dim s_01 As Long
    Dim s_02 As Long
    Dim s_03 As Long
    Dim s_04 As Long
    Dim s_05 As Long
    Dim s_06 As Long
    Dim s_07 As Long
    Dim s_08 As Long
    Dim s_09 As Long
    Dim s_10 As Long
    Dim s_11 As Long
    Dim s_12 As Long
    Dim s_13 As Long
    Dim s_14 As Long
    Dim s_15 As Long
    Dim s_16 As Long
    Dim s_17 As Long
    Dim s_18 As Long
    Dim s_19 As Long
    Dim s_20 As Long
    Dim s_21 As Long
    Dim s_22 As Long
    Dim s_23 As Long
    Dim s_24 As Long
    Dim s_25 As Long
    Dim s_26 As Long
    Dim s_27 As Long
    Dim s_28 As Long
    Dim s_29 As Long

    Dim goukei As Long

    '変数の内容を設定
    Worksheets("Data").Select
    myrow = 2
    jigyocd = Cells(myrow, 2).Value
    smkcd = Cells(myrow, 11).Value
    smk = Cells(myrow, 12).Value
    kaikeicd = Cells(myrow, 3).Value
    kaikei = Cells(myrow, 4).Value
    kancd = Cells(myrow, 5).Value
    kan = Cells(myrow, 6).Value
    koucd = Cells(myrow, 7).Value
    kou = Cells(myrow, 8).Value
    mokcd = Cells(myrow, 9).Value
    mok = Cells(myrow, 10).Value

    busyo = Cells(myrow, 16).Value
    page = Cells(myrow, 13).Value
    kubun = Cells(myrow, 14).Value

    t_yosan = Cells(myrow, 17).Value
    z_yosan = Cells(myrow, 18).Value
    h_yosan = Cells(myrow, 19).Value
    t_kuniken = Cells(myrow, 20).Value
    z_kuniken = Cells(myrow, 21).Value
    h_kuniken = Cells(myrow, 22).Value
    t_kisai = Cells(myrow, 23).Value
    z_kisai = Cells(myrow, 24).Value
    h_kisai = Cells(myrow, 25).Value
    t_sonota = Cells(myrow, 26).Value
    z_sonota = Cells(myrow, 27).Value
    h_sonota = Cells(myrow, 28).Value
    t_ippan = Cells(myrow, 29).Value
    z_ippan = Cells(myrow, 30).Value
    h_ippan = Cells(myrow, 31).Value

    gaiyou = Cells(myrow, 32).Value

    s_01 = Cells(myrow, 33).Value
    s_02 = Cells(myrow, 34).Value
    s_03 = Cells(myrow, 35).Value
    s_04 = Cells(myrow, 36).Value
    s_05 = Cells(myrow, 37).Value
    s_06 = Cells(myrow, 38).Value
    s_07 = Cells(myrow, 39).Value
    s_08 = Cells(myrow, 40).Value
    s_09 = Cells(myrow, 41).Value
    s_10 = Cells(myrow, 42).Value
    s_11 = Cells(myrow, 43).Value
    s_12 = Cells(myrow, 44).Value
    s_13 = Cells(myrow, 45).Value
    s_14 = Cells(myrow, 46).Value
    s_15 = Cells(myrow, 47).Value
    s_16 = Cells(myrow, 48).Value
    s_17 = Cells(myrow, 49).Value
    s_18 = Cells(myrow, 50).Value
    s_19 = Cells(myrow, 51).Value
    s_20 = Cells(myrow, 52).Value
    s_21 = Cells(myrow, 53).Value
    s_22 = Cells(myrow, 54).Value
    s_23 = Cells(myrow, 55).Value
    s_24 = Cells(myrow, 56).Value
    s_25 = Cells(myrow, 57).Value
    s_26 = Cells(myrow, 58).Value
    s_27 = Cells(myrow, 59).Value
    s_28 = Cells(myrow, 60).Value
    s_29 = Cells(myrow, 61).Value

    goukei = Cells(myrow, 62).Value

    'データがなくなるまで処理を繰り返す

    Do Until jigyocd = ""

    'データ内容を差し込む

        'smkcdを差し込む
        Worksheets("予算概要書").Range("c1").Value = smkcd

   -------------省略----------------------------    

        'goukeiを差し込む
        Worksheets("予算概要書").Range("x40").Value = goukei

        '変数の内容を更新
        Worksheets("Data").Select
        myrow = myrow + 1
    jigyocd = Cells(myrow, 2).Value
    smkcd = Cells(myrow, 11).Value
    smk = Cells(myrow, 12).Value
    kaikeicd = Cells(myrow, 3).Value
    kaikei = Cells(myrow, 4).Value
    kancd = Cells(myrow, 5).Value
    kan = Cells(myrow, 6).Value
    koucd = Cells(myrow, 7).Value
    kou = Cells(myrow, 8).Value
    mokcd = Cells(myrow, 9).Value
    mok = Cells(myrow, 10).Value

    busyo = Cells(myrow, 16).Value
    page = Cells(myrow, 13).Value
    kubun = Cells(myrow, 14).Value

    t_yosan = Cells(myrow, 17).Value
    z_yosan = Cells(myrow, 18).Value
    h_yosan = Cells(myrow, 19).Value
    t_kuniken = Cells(myrow, 20).Value
    z_kuniken = Cells(myrow, 21).Value
    h_kuniken = Cells(myrow, 22).Value
    t_kisai = Cells(myrow, 23).Value
    z_kisai = Cells(myrow, 24).Value
    h_kisai = Cells(myrow, 25).Value
    t_sonota = Cells(myrow, 26).Value
    z_sonota = Cells(myrow, 27).Value
    h_sonota = Cells(myrow, 28).Value
    t_ippan = Cells(myrow, 29).Value
    z_ippan = Cells(myrow, 30).Value
    h_ippan = Cells(myrow, 31).Value

    gaiyou = Cells(myrow, 32).Value

    s_01 = Cells(myrow, 33).Value
    s_02 = Cells(myrow, 34).Value
    s_03 = Cells(myrow, 35).Value
    s_04 = Cells(myrow, 36).Value
    s_05 = Cells(myrow, 37).Value
    s_06 = Cells(myrow, 38).Value
    s_07 = Cells(myrow, 39).Value
    s_08 = Cells(myrow, 40).Value
    s_09 = Cells(myrow, 41).Value
    s_10 = Cells(myrow, 42).Value
    s_11 = Cells(myrow, 43).Value
    s_12 = Cells(myrow, 44).Value
    s_13 = Cells(myrow, 45).Value
    s_14 = Cells(myrow, 46).Value
    s_15 = Cells(myrow, 47).Value
    s_16 = Cells(myrow, 48).Value
    s_17 = Cells(myrow, 49).Value
    s_18 = Cells(myrow, 50).Value
    s_19 = Cells(myrow, 51).Value
    s_20 = Cells(myrow, 52).Value
    s_21 = Cells(myrow, 53).Value
    s_22 = Cells(myrow, 54).Value
    s_23 = Cells(myrow, 55).Value
    s_24 = Cells(myrow, 56).Value
    s_25 = Cells(myrow, 57).Value
    s_26 = Cells(myrow, 58).Value
    s_27 = Cells(myrow, 59).Value
    s_28 = Cells(myrow, 60).Value
    s_29 = Cells(myrow, 61).Value

    goukei = Cells(myrow, 62).Value

'PDF保存

 Application.ScreenUpdating = False
 Worksheets("予算概要書").ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\Users\●●●●\Desktop\保存\" & Cells(myrow - 1, 1).Value & ".pdf"
 Application.ScreenUpdating = True

    Loop

End Sub

< 使用 Excel:Excel2013、使用 OS:Windows10 >


複数シートを1つのPDFにするのであれば、1シートずつPDF出力するのを止めて、新規ブックにそれぞれ1シートとなるように出力。全部出力後に、プリンタを「Microsoft Print to PDF」に変えてから、全シートまとめて印刷、という感じにしてはどうでしょうか。
(???) 2020/01/24(金) 15:46

■1
質問が漠然としててあんまり理解できてませんが、「AdobePDFとして保存」をするときに、ブック単位や、(複数の)特定シートを指定することもできますから、出力用のブックに、それぞれの単票データをシートに分けてコピーして、出力用のブック全体を対象に「PDFとして保存」すれば、目的は達成できたりしませんか?

■2
肝心なところが--省略---されているのでわかりませんが、いちいち変数で受け取らなくても普通にコピーして値貼付すればよかったりしませんか?
列の並び順が違う場合でも、貼り付けたあとに列単位で並び替えるとか、何回かにわけてコピペするとか手があるとおもいますので、現コードの整理にも取り組まれたほうがよいとおもいます。

(もこな2) 2020/01/24(金) 15:55


PDFの結合処理が簡単にVBAでできるはず、と質問者さんは(というか上司が)思っていることが懸念される。
それは思うほど簡単な話じゃないんですよ。

直接その方法を採りたいというなら、PDF操作のための別途のアプリケーションを利用したほうがよいです。
フリーのものもあります。

と、ここまで来て、VBAだけでもし対応するとしたら、こんな方法もありうる、という提案を
いただいているものと思います。
たぶん質問者さんは、それだと別の負荷が発生するじゃないか、ぐらいに思っている可能性があります。
(γ) 2020/01/25(土) 01:45


よく見たら■1は、???さんが既におっしゃってましたね。

難易度は上がりますけど、Adobe Acrobat(readerでなく、有償のほう)をお持ちであれば、ExcelVBAから操作して、複数のPDFを結合すること自体は可能です。
https://www.fastclassinfo.com/entry/vba_pdf_combine
https://www.ka-net.org/blog/?p=5866

結合したいPDFの元データがExcelだけじゃない場合には検討してみるとよいとおもいます。

(もこな2 ) 2020/01/25(土) 11:44


 コメント拝見しました。ありがとうございます。
 以下、余談的に。

 Acrobatを使えばVBAから直接操作できることは承知しています。
 VBAだけでという意味でした。
 私も前世では?、Acrobatを使っておりました。
 VBAから操作するコードもいくつか書きました。

 また、Acrobatでは、Scriptで一括処理もできましたね。
 しかし、残念ながら高価で、それを持っている人は少ないかもしれません。
 まあ、企業なら最低でも組織にいくつかは保有しているでしょうけど。
 なお、Office系で簡単にPDFを作れるようになった昨今、
 純正のAcrobat利用は少なくなってきているのかもしれません。

 PDFの結合などには、
 CubePDF Utility と言ったアプリケーションがあります。
 私も時々使っています。
(γ) 2020/01/25(土) 12:16

 Adobe Acrobatはかなり低価格になったんですね。
 たしか、昔は10万円近い価格だった記憶があります。
 営業妨害になりかねないので、上記を訂正させていただきます。
(γ) 2020/01/25(土) 12:48

コメント返信:

[ 一覧(最新更新順) ]


YukiWiki 1.6.7 Copyright (C) 2000,2001 by Hiroshi Yuki. Modified by kazu.