質問者
Mac版Excel VBAによるファイルの取得とpdf化実行の件

質問
-
お忙しいところ申し訳ありません。
現在、Mac版のExcel VBAの個人用マクロブックにて複数の同型ファイルを一気に開き、pdf化するマクロを作成しています。
しかし、なかなか上手くいきません。
作成手順としてはマクロの記録で作成したものを編集する形をとっています。
マクロの記録手順は下記のようになります。
指定フォルダからファイルを開く→ファイルにあるページ設定機能で「次のページ数に合わせる1ページ(横)×1ページ(縦)」に設定する→
指定フォルダにPDFファイルとして保存する→ファイルを閉じる
コードとしては下記になります。
Sub PDF化()
'
' PDF化 Macro
'
' Keyboard Shortcut: Ctrl+Shift+N
'
Workbooks.Open FileName:="/Users/ユーザー名/Desktop/見本データMac.xls"
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
Application.PrintCommunication = True
ActiveSheet.PageSetup.PrintArea = "$B$2:$BA$42"
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0.7)
.RightMargin = Application.InchesToPoints(0.7)
.TopMargin = Application.InchesToPoints(0.75)
.BottomMargin = Application.InchesToPoints(0.75)
.HeaderMargin = Application.InchesToPoints(0.3)
.FooterMargin = Application.InchesToPoints(0.3)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintSheetEnd
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlLandscape
.Draft = False
.PaperSize = xlPaperA4
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
.PrintErrors = xlPrintErrorsDisplayed
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.ScaleWithDocHeaderFooter = True
.AlignMarginsHeaderFooter = True
.EvenPage.LeftHeader.Text = ""
.EvenPage.CenterHeader.Text = ""
.EvenPage.RightHeader.Text = ""
.EvenPage.LeftFooter.Text = ""
.EvenPage.CenterFooter.Text = ""
.EvenPage.RightFooter.Text = ""
.FirstPage.LeftHeader.Text = ""
.FirstPage.CenterHeader.Text = ""
.FirstPage.RightHeader.Text = ""
.FirstPage.LeftFooter.Text = ""
.FirstPage.CenterFooter.Text = ""
.FirstPage.RightFooter.Text = ""
End With
Application.PrintCommunication = True
ChDir "/Users/ユーザー名/Desktop/"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
"/Users/ユーザー名/Desktop/" & ThisWorkbook.Name & ".pdf", Quality:=xlQualityMinimum, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
FalseThisWorkbook.close
End Sub
太字の部分が上手く書けません。
全体の部分もなにか指摘があれば教えていただきたいです。
開きたいファイルはExcel ファイル (*.xls; *.xlsx),*.xls; *.xlsx"になります。
すみませんが教えていただければ幸いです。
すべての返信
-
Windows 版では Dir() や FileSystemObject が使えるのですけれどね。
Mac 版には詳しくないのですが、どうやら MacScript 関数あるいはその後継の AppleScriptTask を使って処理するようです。
ただしサンドボックスの制限により、Excel 2016 以降では MacScript 関数からの finder の実行はできない という情報もありました。上記がうまくいかない場合は、AppleScriptTask での呼び出しに切り替える必要があるかもしれません。
- 編集済み 魔界の仮面弁士MVP 2020年1月16日 5:15 AppleScriptTask に関する情報を追加