I have the following code to save a form as a PDF and email it. Part of the code includes removing the conditional format highlighting (the sheet cannot be saved so highlighting needs to remain as fields to fill in), however sometimes it works and sometimes it doesn't. Mostly error 400. This is not dependant on whether it has just been opened or not. The rest of the code works perfectly, any help appreciated.
Oh, another thing, a colleague has a different version oof outlook and this doesn't work for her, any thoughts? Thanks again.
Oh, another thing, a colleague has a different version oof outlook and this doesn't work for her, any thoughts? Thanks again.
VBA 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
Dim xMember As Range
Dim xMemberName As Range
Dim xForm As Range
Set xSht = ActiveSheet
Set xFileDlg = Application.FileDialog(msoFileDialogFolderPicker)
Set xMember = Range("c7")
Set xMemberName = Range("c6")
Set xForm = Range("b1:d49")
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 + "\" + xMemberName.Value + " " + xMember.Text + ".pdf"
' remove highlighting
With xForm.Select
xForm.Cells.FormatConditions.Delete
End With
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
If Application.WorksheetFunction.CountA(xForm.Cells) <> 0 Then
'Save as PDF file
xForm.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 = ""
.Subject = xMemberName.Text + " Holiday Request"
.Attachments.Add xFolder
.HTMLBody = "<FONT SIZE = 3.5 font face =Tahoma> Hi," & "<br/>" & "<br/>" & "Please find form attached, thank you." & .HTMLBody
If DisplayEmail = False Then
'.Send
End If
End With
Else
MsgBox "The active worksheet cannot be blank"
Exit Sub
End If
End Sub