Macro to autoload first recent doc

When I start LO, I want it to autoload the first document in the Recent
Documents list. I have found no option to do this in LO itself, so I
tried to cobble together an LO Basic macro attached to application start
that does that. I googled and found a couple of posts that deal with
similar problems, so I tried to adapt those (mostly this post from 2008:
https://forum.openoffice.org/en/forum/viewtopic.php?t=7128 .)

The resulting macro attaches and is executed but it doesn't work. Now I
know almost nothing about LO (I'm a pretty new user), let alone its
macro language, but I'm a longstanding user of MS Office products and I
know their macro language fairly well.

This is my code:

Option Explicit
Sub Load1st()
Dim oCP, oCUA, oList, oItem As Object
Dim aProps(0) As New com.sun.star.beans.PropertyValue
  oCP = GetProcessServiceManager().createInstanceWithContext( _
    "com.sun.star.configuration.ConfigurationProvider", GetDefaultContext() )
  aProps(0).Name = "nodepath"
  aProps(0).Value = "/org.openoffice.Office.Common/History"
  oCUA = oCP.createInstanceWithArguments( _
    "com.sun.star.configuration.ConfigurationUpdateAccess", aProps )
  oList = oCUA.getPropertyValue( "PickList" )
  If oList.hasByName( "p0" ) Then
    oItem = oList.getByName( "p0" )
    If FileExists( oItem.URL ) Then
      starDeskTop.loadComponentFromUrl(oItem.URL, "_blank", 0, Array())
    End If
  End If
End Sub

The macro borks at the access "oList.hasByName" which is not valid:
oList is an object so *something* does work but there's no hasByName
item.

Anyone got an idea?

Jon

Monologuing 1/2...

I can't believe I am the only guy on this weird planet who wants this
feature. Come on, somebody must have done this before!?

But even if not, there surely must be a way to do that via a LO Basic
macro. In MS Word and Excel (if I'm allowed to mention these here) it's
so easy to hack this together, even for a relative novice, that I'm
amazed LO is making it so darned difficult.

(And please, no comments along the lines that I should just go back to
MS... I am honestly trying to get away from MS Office.)

Jon

Subj: [libreoffice-users] Macro to autoload first recent doc

Hi Jon

I believe that the recent file list handling was reworked some years ago and the list is no longer located where your code tries to find it. What version of LibreOffice are you using?

I also recomend that you install XrayTool, that let’s you inspect objects and would have made it easier to see that the PickList you tried to access was empty.

http://bernard.marcelly.perso.sfr.fr/index2.html

I hope that the code below will do what you want. I used a few functions from the Tools library that is bundled with LibreOffice. I recomend that you have a look at these functions especially GetRegistryKeyContent.

Option Explicit

Sub Load1st()
Dim oCUA, oList, oItem As Object
BasicLibraries.LoadLibrary("Tools")
REM use GetRegistryKeyContent function from the module Tools.Misc
oCUA = GetRegistryKeyContent(sKeyName:="/org.openoffice.Office.Histories/Histories", bforUpdate:=true)
oList = oCUA.getByName("URLHistory").getByName("OrderList")
If oList.hasByName("0") Then
  oItem = oList.getByName("0")
  If FileExists(oItem.HistoryItemRef) Then
   REM use OpenDocument function from the module Tools.Misc
   OpenDocument(oItem.HistoryItemRef, Array())
  End If
End If
End Sub

Anyway I hope this helps you, and don’t hesitate to ask follow-up questions, and of course let me know if the code isn’t working for you.

Regards,

Niklas

Hi Jon

I believe that the recent file list handling was reworked some years ago and the list is no longer located where your code tries to find it. What version of LibreOffice are you using?

I also recomend that you install XrayTool, that let’s you inspect objects and would have made it easier to see that the PickList you tried to access was empty.

http://bernard.marcelly.perso.sfr.fr/index2.html

I hope that the code below will do what you want. I used a few functions from the Tools library that is bundled with LibreOffice. I recomend that you have a look at these functions especially GetRegistryKeyContent.

Option Explicit

Sub Load1st()
Dim oCUA, oList, oItem As Object
BasicLibraries.LoadLibrary("Tools")
REM use GetRegistryKeyContent function from the module Tools.Misc
oCUA = GetRegistryKeyContent(sKeyName:="/org.openoffice.Office.Histories/Histories", bforUpdate:=true)
oList = oCUA.getByName("URLHistory").getByName("OrderList")
If oList.hasByName("0") Then
  oItem = oList.getByName("0")
  If FileExists(oItem.HistoryItemRef) Then
   REM use OpenDocument function from the module Tools.Misc
   OpenDocument(oItem.HistoryItemRef, Array())
  End If
End If
End Sub

Anyway I hope this helps you, and don’t hesitate to ask follow-up questions, and of course let me know if the code isn’t working for you.

Regards,

Niklas

Let's improve the code a bit...

When we open the registry key we do not need it to be editable so let's open it as read only. By default GetRegistryKeyContent opens it as such. For some reason LibreOffice complains about named arguments not supported for the object, as Jon pointed out to me.

I've changed the key as well to correspond exactly with the key in registry. You can search for the key in the registrymodifications.xcu file (with the path %appdata%\LibreOffice\4\user\registrymodifications.xcu on windows).

I've added a new sub which opens the file and sets some open arguments according to what is saved in the registry and if the file is a template it should be opened in edit mode. I do not think that the password part works at the moment, so that part of the code probably needs some love. :wink:

Sub Load1st()
Dim oCUA, oList, oItem As Object
  If Not BasicLibraries.isLibraryLoaded("Tools") Then BasicLibraries.LoadLibrary("Tools")
  REM use GetRegistryKeyContent function from the module Tools.Misc
  oList = GetRegistryKeyContent("/org.openoffice.Office.Histories/Histories/org.openoffice.Office.Histories:HistoryInfo['PickList']/OrderList")
  If oList.hasByName("0") Then
   oItem = oList.getByName("0")
   OpenRecentFile(oItem.HistoryItemRef)
  End If
End Sub

Sub OpenRecentFile(sFileURL as String)
Dim oPickList As Object, oPickListArgs As Object
Dim loadArgs(2) As New com.sun.star.beans.PropertyValue
  If Not BasicLibraries.isLibraryLoaded("Tools") Then BasicLibraries.LoadLibrary("Tools")
  oPickList = GetRegistryKeyContent("/org.openoffice.Office.Histories/Histories/org.openoffice.Office.Histories:HistoryInfo['PickList']/ItemList")
  If oPickList.hasByName(sFileURL) Then
   oPickListArgs = oPickList.getByName(sFileURL)
   If FileExists(sFileURL) Then
    loadArgs(0).Name = "AsTemplate" 'We never want to open a recent file as template
    loadArgs(0).Value = false
    loadArgs(1).Name = "Filter"
    loadArgs(1).Value = oPickListArgs.getByName("Filter")
    loadArgs(2).Name = "Password"
    loadArgs(2).Value = oPickListArgs.getByName("Password")
    REM use OpenDocument function from the module Tools.Misc
    OpenDocument(sFileURL, loadArgs())
   End If
  End If
End Sub

'I loosely took inspiration from the LibreOffice code http://opengrok.libreoffice.org/xref/core/unotools/source/config/historyoptions.cxx

Regards,
Niklas Johansson

Hi Niklas,

thanks a lot for that. I can confirm that these macros do work... I will
analyse them and should be able to adapt them to my needs.

Cheers Jon

Subj: Re: [libreoffice-users] Macro to autoload first recent doc