Remove conditional highlighting VBA

Rogerisit

Board Regular
Joined
Oct 20, 2016
Messages
70
Office Version
  1. 2019
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.

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
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Right now you have:
VBA Code:
    ' remove highlighting
    With xForm.Select
        xForm.Cells.FormatConditions.Delete
    End With


Try this instead
VBA Code:
    ' remove highlighting
        xForm.FormatConditions.Delete
 
Upvote 0
Solution

Forum statistics

Threads
1,214,667
Messages
6,120,810
Members
448,990
Latest member
rohitsomani

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top