Private Function SaveActiveFile As String

Dim document   as object
Dim dispatcher as Object
Dim crntPath As String
Dim fileName As String
Dim fileNameNoExt As String
Dim oDoc As Object
Dim docURL As string
Dim args1(3) as new com.sun.star.beans.PropertyValue
Dim chkPdfObj As Object
Dim fullPdfFilePathName As String
Dim indexTblObjects As Object
Dim indexNum As Integer

	On Local Error GoTo ErrorHandler
	
	'Disable screen update
	lockScreen(True)
	
	'Load the built-in library that has helper functions
	If (Not GlobalScope.BasicLibraries.isLibraryLoaded("Tools")) Then
		GlobalScope.BasicLibraries.LoadLibrary("Tools")
	End If
	
	'Make sure it has already been saved before
	oDoc = ThisComponent
	If Not (oDoc.hasLocation()) Then
		SaveActiveFile = ""
		lockScreen(False)
		Exit Function
	End If

	docURL = oDoc.getURL() 'Get the current path
	document = oDoc.CurrentController.Frame	
	dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
	
	'Update any index page e.g ToC
	indexTblObjects = oDoc.getDocumentIndexes()
	For indexNum = 0 to indexTblObjects.getCount() - 1
		indexTblObjects(indexNum).update
	Next indexNum
	
	dispatcher.executeDispatch(document, ".uno:Save", "", 0, Array()) 'Save the file
	
	chkPdfObj = dlgForm.GetControl("chkGeneratePDF")
	
	If chkPdfObj.State = 1 then
		crntPath = DirectoryNameoutofPath(docURL, "/") 'Convert it to a regular path string
		fileName = FileNameoutofPath(docURL, "/") 'Extract the filename
		fileNameNoExt = GetFileNameWithoutExtension(fileName) 'Extract the filename with no extension
		fullPdfFilePathName = crntPath + "/" + fileNameNoExt + ".pdf" 'Create a new filename

		args1(0).Name = "URL"
		args1(0).Value = fullPdfFilePathName
		args1(1).Name = "FilterName"
		args1(1).Value = "writer_pdf_Export"
		args1(2).Name = "ExportBookmarks"
		args1(2).Value = True
		args1(3).Name = "UseTaggedPDF"	'Use document headings
		args1(3).Value = True
		dispatcher.executeDispatch(document, ".uno:ExportDirectToPDF", "", 0, args1())
		
		'PDFDoc(fullPdfFilePathName)
			
		SaveActiveFile = fullPdfFilePathName
		
	End If
	
	'Re-enable screen update
	lockScreen(False)
	Exit Function
	
	ErrorHandler:
		dispError("SaveActiveFile", Err, Error, Erl)

End Function