Help me understand my own code

I have a macro that I created a couple years ago. Time (and a brain surgery) has prevented my remembering what is supposed to be happening, here. (It’s a good object lesson on why comments in code are a good idea.) I’d say there’s a good chance I copied most of this from somewhere else. (My most common resource when I created this was probably Andrew P’s references.

The line that begins oRegistryKeyContent = oConfigProvider keeps throwing an error “cannot find /org.openoffice.Office.Common/Path/Info.” I did manage to find this bug thread, where Comment 52 suggests that the solution is to make the path something on the local machine. That’s not really an option for our workflow.

Function SelectFile(FilterNames() as string) As String
	dim oFileDialog as object
	Dim iAccept as integer
	Dim sPath
	dim sInitPath as string
	Dim sRefControlName
	dim oUCB as object 'Simple File Access Uno Service
	With globalScope.basicLibraries
		If not .isLibraryLoaded("Tools") Then
			.loadLibrary("Tools")
		End If
	End with
	dim aNodePath(0) as new com.sun.star.beans.PropertyValue
	dim oConfigProvider as object


	With globalScope.basicLibraries
		If not .isLibraryLoaded("Tools") Then
			.loadLibrary("Tools")	
		End If
'		If .isLibraryLoaded("Tools") Then
'			msgbox "Tools Loaded"
'		End If
		
	End with
	oUCB = createUnoService("com.sun.star.ucb.SimpleFileAccess")
	oFileDialog = createUnoService("com.sun.star.ui.dialogs.FilePicker")
'	sInitPath = convertToURL("O:\ISO-IEC-17025\Server\QMS Documents\Working")
'	sinitPath = oFileDialog.getDisplayDirectory
'	msgbox sinitPath
	sInitPath = convertToURL("L:\QMS Documents\Working")
	oConfigProvider = createUnoService("com.sun.star.configuration.ConfigurationProvider")
	
	AddFiltersToDialog(FilterNames(), oFileDialog)
	
	If oUCB.Exists(sInitPath) Then
		oFileDialog.setDisplayDirectory(sInitPath)
		With aNodePath(0)
			.Name = "nodepath"
			.Value = "/org.openoffice.Office.Common/Path/Info"
		End With
		oRegistryKeyContent = oConfigProvider.createInstanceWithArguments("com.sun.star.configuration.ConfigurationUpdateAccess", aNodePath())
		With oRegistryKeyContent
			.WorkPathChanged = true
			.commitChanges
		End With
	Else
		msgBox "Error! Built-in path for documents is incorrect"
		Exit Function
	End If
	iAccept = oFileDialog.execute()
	If iAccept = 1 Then
		sPath = oFileDialog.Files(0)
		SelectFile = sPath
	End If
	oFileDialog.Dispose()	
End Function

The above macro is not working. What is WorkPathChanged? There is no such property / method in LO.

Interesting. It’s mentioned in Comment 49 of the bug report I mentioned above.

Perhaps this refers to OpenOffice, not LibreOffice.

Perhaps you’re right, but for some reason, that macro worked great back in LO5.3.7.2.

I got to know LO later than this version. :slightly_smiling_face:

Either way, if it worked before, and then stopped working, isn’t that a regression? I posted it over a month ago to https://bugs.documentfoundation.org/show_bug.cgi?id=144625, and its status is still “UNCONFIRMED”.

That line did not cause the error. The line two lines above it is what triggers the runtime error.

In theory, you can add the /org.openoffice.Office.Common/Path/Info key (don’t do this!). But only the LO developer can add the WorkPathChanged property / method.

I’ve discovered that the error message about not finding “/org.openoffice.Office.Common/Path/Info” only crops up when I launch the macro from an event in a Base file, and not when it’s launched from the LO Basic IDE. Is it possible that key is missing from Base, and not from the IDE?

Well, this is confusing. For some reason, the macro is working as desired regardless of how I launch it, today. It definitely wasn’t before. I have no idea what changed.

:flushed: Well, this is embarrassing. I just realized that at the end of the day, I had commented out the line that was triggering my error message, as well as the one that contained the WorkPathChanged property/method. So that’s why it’s working today. As far as I can tell, those lines were there because of an old bug where oFileDialog.setDisplayDirectory() didn’t work correctly without those lines. I think it was this bug.

By the way, when working with FilePicker, we must be very careful when choosing a constant TemplateDescription.

Can you expound on what you mean by this. It’s obvious to me you’re pointing out a pitfall of using the .FilePicker, which can be a conflict.

OK. Macro that asks for a name for the output xml file:

Sub Test
  Dim oFilePicker, mode As Long, file As String
  mode=com.sun.star.ui.dialogs.TemplateDescription.FILESAVE_SIMPLE
  'mode=com.sun.star.ui.dialogs.TemplateDescription.FILESAVE_AUTOEXTENSION
  oFilePicker=com.sun.star.ui.dialogs.FilePicker.createWithMode(mode)
  With oFilePicker
    .appendFilter "XML files (*.xml)", "*.xml"
    .setDisplayDirectory ConvertToUrl("C:\temp")
    .setDefaultName "test.xml"
    If .execute<>1 Then Exit Sub
    file=.SelectedFiles(0)
  End With 
  
  Msgbox "Selected file: " & file
End Sub

If you run this macro, you will see that the method setDefaultName did not work. The reason is an incorrectly chosen mode value. If you uncomment the second assignment statement for mode, then the macro is executed as expected.