title: macro executed when document is loaded
body: <p>After losing files from a HD, data rescue gave me back most of them, but with abstract names (such as file000123.xlsx). I need to rename them using cell values (client name, invoice ref).</p>
  <p>I could make a Basic macro that works for this, if I open the files and start the macro myself for each one of those files.
  As I have thousands of files to rename, I need that macro to execute on its own, either when documents are loaded, otherwise on a selected folder.</p>
  <p>I assigned my macro to the &quot;document loaded&quot; event via the &quot;tools-Customize-Events&quot; menu. Then, I get a &quot;wrong property value&quot; error on the 1st line, the one defining the function.</p>
  <p>Is my way of doing wrong ? Do I have to modify the macro for it to work there ?</p>
  <p>Context :</p>
  <ul>
  <li>This macro is in &quot;My macros&quot;, not within the documents.</li>
  <li>using libreOffice 7 on Linux</li>
  <li>working on .xlsx files</li>
  </ul>
  <p>Thanks for any help, my libreOffice Basic is even poorer than my English.. B-)</p>
  <p>My code :</p>
  <pre><code>function getFullRep(sPath As String) As String
  Dim cpt As Integer
  Dim buf As String
  Const SLASH = &quot;/&quot;
     buf = &quot;&quot;
     for cpt = Len(sPath) to 1 step -1
        if Mid(sPath, cpt, 1) = SLASH then
            buf = Left(sPath, cpt)
           exit for
         end if
     next
     getFullRep = buf
  end function
   
  Sub Main
   
  Dim oDoc as Object
  Dim sG2 as String
  Dim sO2 as String
  Dim sO as String
  Dim sPathBackupFolder as String
   
  Dim filespec As string
  Dim laDate As String
  Dim myfilename As String
  Dim oFeuille As Object
  Dim sNomFeuille As String
   
  oDoc = ThisComponent
   
  sPathBackupFolder = getFullRep (oDoc.location)
   
  oFeuille = oDoc.getCurrentController().getActiveSheet()
   
  sNomFeuille = oFeuille.getName()
  laDate = Format(Now(),&quot;YYMMDDhhmmss&quot;)
   
  If oFeuille.GetCellRangeByName(&quot;G14&quot;).String &lt;&gt;&quot;&quot; Then
  sG2 = oFeuille.GetCellRangeByName(&quot;G14&quot;).String
  sO2 = oFeuille.GetCellRangeByName(&quot;A15&quot;).String
      else
  sG2 = oFeuille.GetCellRangeByName(&quot;G15&quot;).String
  sO2 = oFeuille.GetCellRangeByName(&quot;A16&quot;).String
      End If
  sO = Replace(sO2, &quot;Facture N°&quot;, &quot;&quot;)
   
  'Chemin et nom de fichier composé
  myfilename = sPathBackupFolder+sG2+&quot; - &quot;+sO+&quot; - &quot;+laDate+&quot;.ods&quot;
   
  'Enregistrer sous
  dim document   as object
  dim dispatcher as object
   
  ' ----------------------------------------------------------------------
  document   = ThisComponent.CurrentController.Frame
  dispatcher = createUnoService(&quot;com.sun.star.frame.DispatchHelper&quot;)
   
  ' ----------------------------------------------------------------------
  dim args1(1) as new com.sun.star.beans.PropertyValue
  args1(0).Name = &quot;URL&quot;
  args1(0).Value = ConvertToUrl(myfilename) 'On converti le chemin
  args1(1).Name = &quot;FilterName&quot;
  args1(1).Value = &quot;calc8&quot;
   
  dispatcher.executeDispatch(document, &quot;.uno:SaveAs&quot;, &quot;&quot;, 0, args1())
  oDoc.store
  oDoc.close(True)
  End Sub
  </code></pre>