
確認するのではなく特定のフォルダーに保存するように次のスクリプトを変更するにはどうすればよいですか?
Sub Saveaspdfandsend()
Dim xSht As Worksheet
Dim xFileDlg As FileDialog
Dim xFolder As String
Dim xYesorNo As Integer
Dim xOutlookObj As Object
Dim xEmailObj As Object
Dim xUsedRng As Range
Set xSht = ActiveSheet
Set xFileDlg = Application.FileDialog(msoFileDialogFolderPicker)
If xFileDlg.Show = True Then
xFolder = xFileDlg.SelectedItems(1)
Else
MsgBox "You must specify a folder to save the PDF into." & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Must Specify Destination Folder"
Exit Sub
End If
xFolder = xFolder + "\" + xSht.Name + Format(Now, " yyyy-mm-dd hmmAM/PM") + ".pdf"
'Check if file already exist
If Len(Dir(xFolder)) > 0 Then
xYesorNo = MsgBox(xFolder & " already exists." & vbCrLf & vbCrLf & "Do you want to overwrite it?", _
vbYesNo + vbQuestion, "File Exists")
On Error Resume Next
If xYesorNo = vbYes Then
Kill xFolder
Else
MsgBox "if you don't overwrite the existing PDF, I can't continue." _
& vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Exiting Macro"
Exit Sub
End If
If Err.Number <> 0 Then
MsgBox "Unable to delete existing file. Please make sure the file is not open or write protected." _
& vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Unable to Delete File"
Exit Sub
End If
End If
Set xUsedRng = xSht.UsedRange
If Application.WorksheetFunction.CountA(xUsedRng.Cells) <> 0 Then
'Save as PDF file
xSht.ExportAsFixedFormat Type:=xlTypePDF, Filename:=xFolder, Quality:=xlQualityStandard
'Create Outlook email
Set xOutlookObj = CreateObject("Outlook.Application")
Set xEmailObj = xOutlookObj.CreateItem(0)
With xEmailObj
.Display
.To = ""
.CC = "[email protected]"
.Subject = xSht.Name + ".pdf"
.Attachments.Add xFolder
If DisplayEmail = False Then
'.Send
End If
End With
Else
MsgBox "The active worksheet cannot be blank"
Exit Sub
End If
End Sub
感謝を込めて、マイケル
答え1
選択された項目が変数に格納されていることがわかります。xフォルダxFileDlg に関するセクションから xFolder が追加されるところまでをコメント アウトします。
それを好きなように変更します:
xFolder = xFolder + "\" + xSht.Name + Format(Now, " yyyy-mm-dd hmmAM/PM") + ".pdf"
答え2
フォルダーを選択する必要がある唯一の理由はこの部分です。
xFileDlg = Application.FileDialog(msoFileDialogFolderPicker) を設定します。
If xFileDlg.Show = True Then xFolder = xFileDlg.SelectedItems(1) Else MsgBox "PDF を保存するフォルダを指定する必要があります。" & vbCrLf & vbCrLf & "このマクロを終了するには [OK] を押してください。", vbCritical, "保存先フォルダを指定する必要があります" Exit Sub End If
これは、毎回フォルダーを選択する場合や、新しいフォルダーを作成する必要がある場合に最適です。ただし、フォルダーが存在し、常に同じ場所に保存する場合は、上記の部分を削除して、フォルダーの場所を手動で追加するだけです。
例:
xFolder = "C:\MyPDFs" + "\" + xSht.Name + Format(Now, " yyyy-mm-dd hmmAM/PM") + ".pdf"