Private Function SaveFinalFile As string
Dim filePickerDialog 'FilePicker service instance
Dim fileAccess 'SimpleFileAccess service instance
Dim retVal as Integer 'Response to the FilePicker
Dim crntPath as String 'Hold the initial path
Dim newFileNameAndPath As String
Dim fileNameNoExt As String
Dim fileName As string
Dim filePickerArgs() As String
Dim getName() As String
Dim args1(1) as new com.sun.star.beans.PropertyValue
Dim args2(3) as new com.sun.star.beans.PropertyValue
dim document as object
dim dispatcher as Object
Dim fullPdfFilePathName As String
Dim fullOdtFilePathName As string
Dim indexTblObjects As Object
Dim indexNum As Integer

	On Local Error GoTo ErrorHandler
		
	'Disable screen update
	lockScreen(True)	
	
	document = ThisComponent.CurrentController.Frame
	dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

	'Load the built-in library that has helper functions
	If (Not GlobalScope.BasicLibraries.isLibraryLoaded("Tools")) Then
		GlobalScope.BasicLibraries.LoadLibrary("Tools")
	End If
	
	'Note: The following services MUST be called in the following order or Basic will not remove the FileDialog Service
	filePickerDialog = CreateUnoService("com.sun.star.ui.dialogs.FilePicker")
	fileAccess = CreateUnoService("com.sun.star.ucb.SimpleFileAccess")
	
	'https://api.libreoffice.org/docs/idl/ref/namespacecom_1_1sun_1_1star_1_1ui_1_1dialogs_1_1TemplateDescription.html#abd22bd8de4497ac68b5950779a5b6788
	filePickerArgs = Array(com.sun.star.ui.dialogs.TemplateDescription.FILESAVE_SIMPLE )
	
	'Set the initial path here!
	If ThisComponent.hasLocation() Then
		crntPath = DirectoryNameoutofPath(ThisComponent.getURL(),"/") 'existing document
	Else
		crntPath = environ("USERPROFILE") + "\Desktop" 'a new untitled document
	End If
	
	With filePickerDialog
		.Initialize( filePickerArgs() )
		.setDisplayDirectory(crntPath)
		.setMultiSelectionMode(False)
		.appendFilter("LO Writer Files (.odt)", "*.odt" )
		.setTitle( "Save File As..." )
	End With
	
	retVal = filePickerDialog.Execute() 'Run the file chooser dialog
	If retVal = 0 Then 'cancel pressed
		filePickerDialog.Dispose() 'Unload the dialogue box
		lockScreen(False)
		SaveFinalFile = ""
		Exit Function
	End If
	
	getName = filePickerDialog.getFiles()
	newFileNameAndPath = getName(0)
	filePickerDialog.Dispose() 'Unload the dialogue box

	fileName = FileNameoutofPath(newFileNameAndPath)
	fileNameNoExt = GetFileNameWithoutExtension(fileName)
	fullPdfFilePathName = crntPath + "/" + fileNameNoExt + ".pdf"
	fullOdtFilePathName = crntPath + "/" + fileNameNoExt + ".odt"
		
	'Update any index page e.g ToC
	indexTblObjects = ThisComponent.getDocumentIndexes()
	For indexNum = 0 to indexTblObjects.getCount() - 1
		indexTblObjects(indexNum).update
	Next indexNum
	
	args1(0).Name = "URL"
	args1(0).Value = fullOdtFilePathName
	args1(1).Name = "FilterName"
	args1(1).Value = "writer8"
	dispatcher.executeDispatch(document, ".uno:SaveAs", "", 0, args1())
	
	Call SetDocumentProperties(INTERNAL_CALL)
	dispatcher.executeDispatch(document, ".uno:Save", "", 0, Array()) 'Save the file
	
	args2(0).Name = "URL"
	args2(0).Value = fullPdfFilePathName
	args2(1).Name = "FilterName"
	args2(1).Value = "writer_pdf_Export"
	args2(2).Name = "ExportBookmarks"
	args2(2).Value = True
	args2(3).Name = "UseTaggedPDF"	'Use document headings
	args2(3).Value = True
	'dispatcher.executeDispatch(document, ".uno:ExportDirectToPDF", "", 0, args2())
	
	'PDFDoc(fullPdfFilePathName)
		
	Msgbox "The file has been saved in the current folder in PDF and ODT formats with the filename: """ + fileNameNoExt + """.", _
		MSGBOX_OK_INFO, "Saved Final Version, well done!!!"
			
	'Re-enable screen update
	lockScreen(False)
	SaveFinalFile = fullPdfFilePathName
	Exit Function

	ErrorHandler:
		dispError("SaveNewFile", Err, Error, Erl)
		
End Function