<html>
    <head>
      <base href="https://bugs.documentfoundation.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_UNCONFIRMED "
   title="UNCONFIRMED - ThisComponent undefined/outdated when a spreadsheet loads"
   href="https://bugs.documentfoundation.org/show_bug.cgi?id=125042">125042</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>ThisComponent undefined/outdated when a spreadsheet loads
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>LibreOffice
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>x86-64 (AMD64)
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Windows (All)
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>UNCONFIRMED
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>medium
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Calc
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>libreoffice-bugs@lists.freedesktop.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>arminlinder@arminlinder.de
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Description:
If you use a formula pointing to a user defined function/macro (tested with:
Basic Macro), which uses the ThisComponent object, the call fails, or gives
unexpected values during the initial recalculation of the spreadsheet cells.

Steps to Reproduce:
1. Sample macro code:

Function TestThisComponent()

        Dim Doc As Object
        Dim Sheet As Object
        Dim Result as String
        Dim S as String

        on Error Goto ExitFunction
rem             Result = "Unable to access XRay"                
rem             xRay ThisComponent
        Result = "Unable to access ThisComomnent"       
        Doc = ThisComponent
        Result = "Unable to access ThisComponent.Sheets(0)"
        Sheet = Doc.Sheets (0)
        Result = "Unable to access Sheet Name"
        S = Sheet.Name
        Result = S
ExitFunction:
        TestThisComponent = Result
End Function

2. In a cell, enter formula: =TestThisComponent()

3. Save and reload the sheet.

Actual Results:
After entering the forumla, everything looks fine, the worksheet name appears
in the cell. When reloading the file, the call/formula fails.

If the reloaded sheet is the only open document, you get  "Unable to access
ThisComponent.Sheets(0)" or "Unable to access ThisComponent", depending on
whether you have the macro editor open or not. If there is another calc
spreadsheet open and activated, you get the other spreadsheet name inserted in
your current document. If any other LibreOffice document (not a spreadsheet) is
active, ThisComponent points to that document, and most likely fails because it
won't have a "Sheets" property.

Expected Results:
The name of the current worksheet stays in the cell if I save and re-load the
file.


Reproducible: Always


User Profile Reset: No



Additional Info:
If I recalculate the spreadsheet cell manually after load [F9], the macro
works.

Conclusions: ThisComponent points to the document object last activated.
Spreadsheet recalculation on document load does obviously take place before the
document gets activated, so this ThisComponent is either uninitialized or
pointing to the wrong document. From testing I'd guess that the initial
recalculation indeed takes place before any document event fires, so there
isn't a workaround.

As far as I have read this behaviour is undocumented, contrary, the
documentation does always state that ThisComponent points to the current
document, which is not correct while the document recalculates upon load.

The only workaround I found is to ignore/suppress any errors, and trigger
another recalculation in the "View Created" event handler which fires after the
document has finished loading. This will, however, have the unwanted
side-effect that the document recalculates twice on load.

Version: 6.1.5.2 (x64)
Build-ID: 90f8dcf33c87b3705e78202e3df5142b201bd805
CPU-Threads: 4; BS: Windows 10.0; UI-Render: Standard; 
Gebietsschema: de-DE (de_DE); Calc: group threaded</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are the assignee for the bug.</li>
      </ul>
    </body>
</html>