I thought this code was working properly, however, yesterday noticed that the excel file was showing as locked and excel wasn't open (from the perspective of the taskbar not having an underline below the icon).  When I went into Task Manager, Excel was there under Background Processes.  I think the reason I hadn't encountered previously was that I usually had another excel file open and the code is instructed not to close excel if other files are open.  I found when I stepped through the code, Excel closed properly. Leading me to think it was a timing issue.  I tried do events and made a visit to Mr. GPT and we had a dialogue with suggestions to use timer and loop which were unsuccessful.  The only method I got to work other than stepping through the code was via MsgBox.  Once the message box became visible, I could see the lock file disappear as well as Excel no longer showing up in the Background Process.  
Are there other suggestions to sort this without the need to click a message box or other form?
	
	
	
		
 Are there other suggestions to sort this without the need to click a message box or other form?
		Code:
	
	
	Function ExportPDF(xlFileName As String, SavePDF As String, Optional DisplayPDF As Boolean)
'Code to save xlsx as pdf
'Note: If file exists, it is automatically replaced
'https://access-programmers.co.uk/forums/showthread.php?t=228641
'2018114
    Dim objXl As Object
    
    Const xlTypePDF = 0
    Const xlQualityStandard = 0
    
    ' Establish an EXCEL application object
    On Error Resume Next
    Set objXl = GetObject(, "Excel.Application")
    If Err.Number <> 0 Then
        Set objXl = CreateObject("Excel.Application")
    End If
    Err.Clear
    On Error GoTo 0
    
    With objXl
        .Workbooks.Open (xlFileName)
          
        .ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
            "" & SavePDF & "", quality:=xlQualityStandard, _
            includedocproperties:=True, ignoreprintareas:=False, openafterpublish:= _
            DisplayPDF
        '.Quit
    End With
    'https://www.mrexcel.com/board/threads/quit-current-workbook-but-not-other-active-workbooks.1088914/
    '20240531
    DoEvents
    
    If objXl.Workbooks.Count = 1 Then
        objXl.Quit
        Set objXl = Nothing
        ' Give Excel more time to fully release the resources
        ' Couldn't figure out how to fully release excel using
        ' doevents or loop so ended up with a msgbox
        ' 20250116
        MsgBox "Since there are no other workbooks open, Closing Excel", vbInformation + vbOKOnly, "Timing Issue on Close"
        
    Else
        objXl.ActiveWorkbook.Close
        DoEvents
    End If
End Function 
	 
 
		 
 
		 
 
		 
 
		 
 
		