Public Sub 月報_Print()
Dim wb As Workbook
Dim ws As Worksheet
Dim sheet_name As Variant
Dim pPath As String
Dim pFile As String
Dim wsh As Object
Dim p_Name As String
Dim p_FilePath As String
Dim strShCmnd As String
Const parent_path = "C:\教えてgoo" 'ご説明されている「リンゴフォルダ」の親フォルダを指定
Set wsh = CreateObject("Wscript.Shell")
Set wb = ThisWorkbook
MsgBox "シート選択ダイアログが表示されます。" & vbCrLf & "印刷する月報を選択してください "
Do
wb.Application.CommandBars("Workbook tabs").ShowPopup
DoEvents
sheet_name = Application.InputBox("このシートでいいですか?", , ActiveSheet.Name)
Loop Until sheet_name <> False
Worksheets(sheet_name).PrintOut
pPath = parent_path & "\リンゴ\青森"
p_FilePath = GetLatestPDF(pPath)
p_Name = Application.ActivePrinter
p_Name = Left(p_Name, InStr(p_Name, " on ") - 1)
strShCmnd = "AcroRd32.exe /t " & p_FilePath & " " & p_Name
wsh.Run (strShCmnd)
Application.Wait Now + TimeValue("0:00:10") '10秒待つ
wsh.Exec ("taskkill.exe /F /IM AcroRd32.exe")
Set wsh = Nothing
End Sub
Public Function GetLatestPDF(pPath As String) As String
Dim chkTime As Date
Dim maxTime As Date
Dim chkName As String
Dim maxName As String
pPath = pPath & "\"
chkName = Dir(pPath & "*.pdf")
maxTime = FileDateTime(pPath & chkName)
maxName = chkName
Do While chkName <> ""
chkTime = FileDateTime(pPath & chkName)
If chkTime > maxTime Then
maxTime = chkTime
maxName = chkName
End If
chkName = Dir()
Loop
GetLatestPDF = pPath & maxName
End Function