Nathan_Barratt
New Member
- Joined
- Nov 28, 2017
- Messages
- 14
Good Morning all
I have the following code which I use at work to get folk to send me pdf files of certain reports from a excel workbook and its working fine.
I am now TRYING to adopt this code in another workbook for use in sending a single worksheet (not as pdf) to another department by all my users. This is so the other department can extract the info from the sheet.
What I need assistance with is as follows
1. Can this code be adopted to save as a single worksheet with the same naming protocols as within (obviously not as PDF)
2. If yes to above what do I need to change within my code to do so
Thanks in anticipation
Nath
I have the following code which I use at work to get folk to send me pdf files of certain reports from a excel workbook and its working fine.
I am now TRYING to adopt this code in another workbook for use in sending a single worksheet (not as pdf) to another department by all my users. This is so the other department can extract the info from the sheet.
What I need assistance with is as follows
1. Can this code be adopted to save as a single worksheet with the same naming protocols as within (obviously not as PDF)
2. If yes to above what do I need to change within my code to do so
Code:
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 + "\" + Format(Date, "yyyymmdd") + "-" + Worksheets("Dashboard").Range("A3") + "-" + xSht.Name + ".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 = "Recipients mail address"
.CC = ""
.Subject = Format(Date, "yyyymmdd") + "-" + Worksheets("Dashboard").Range("A3") + "-" + 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
Thanks in anticipation
Nath