Commits

Anonymous committed 9709bb0

Fix for issue: 113357

Comments (0)

Files changed (2)

sc/source/ui/vba/vbaeventshelper.cxx

     mbDocDisposed = true;
 }
 
-// Compares the old selection with the new selection to check if the selection is changed.
-// Returns sal_True if selection is not changed, otherwise return sal_False.
-sal_Bool ScVbaEventsHelper::compareSelection( uno::Any oldSelection, uno::Any newSelection )
+// Compares old selection with new selection to check if should fire selection change event.
+// Returns sal_True if should fire the selection change event, otherwise return sal_False.
+sal_Bool ScVbaEventsHelper::shouldFireSelectionEvent( uno::Any oldSelection, uno::Any newSelection, const SCTAB nNewSelTab )
 {
-    ScCellRangesBase* pNewCellRanges = ScCellRangesBase::getImplementation( uno::Reference< uno::XInterface >( oldSelection, uno::UNO_QUERY ) );
-    ScCellRangesBase* pOldCellRanges = ScCellRangesBase::getImplementation( uno::Reference< uno::XInterface >( newSelection, uno::UNO_QUERY ) );
-    if ( pNewCellRanges && pOldCellRanges && pNewCellRanges->GetRangeList() == pOldCellRanges->GetRangeList() )
+    sal_Bool bShouldFireEvent = sal_True;
+    try
     {
-        return sal_True;
+        ScCellRangesBase* pOldCellRanges = ScCellRangesBase::getImplementation( uno::Reference< uno::XInterface >( oldSelection, uno::UNO_QUERY ) );
+        ScCellRangesBase* pNewCellRanges = ScCellRangesBase::getImplementation( uno::Reference< uno::XInterface >( newSelection, uno::UNO_QUERY ) );
+        // When open the spreadsheet file, a selection change event is fired, but in Excel this event should not be fired.
+        if ( !pOldCellRanges || !pNewCellRanges || pOldCellRanges->GetRangeList() == pNewCellRanges->GetRangeList() )
+        {
+            bShouldFireEvent = sal_False;
+        }
+        else
+        {
+            // Selection change event is fired when activating a sheet. But in Excel, activating sheet should not fire sheet change event.
+            if ( getTabFromArgs( uno::Sequence< uno::Any >( &oldSelection, 1 ) ) != nNewSelTab )
+            {
+                bShouldFireEvent = sal_False;
+            }
+        }
     }
-    return sal_False;
+    catch ( uno::Exception& /*ex*/ )
+    {
+        bShouldFireEvent = sal_False;
+    }
+    return bShouldFireEvent;
 }
 
 sal_Bool SAL_CALL 
         {
             nTab = getTabFromArgs( aArgs );
             // Check whether the selection is changed. If selection is not changed, then do not fire the SelectionChange event.
-            if( nTab != INVALID_TAB && !compareSelection( m_curSelection, aArgs[0] ) )
+            if( nTab != INVALID_TAB && shouldFireSelectionEvent( m_curSelection, aArgs[0], nTab ) )
             {
-                m_curSelection = aArgs[0];
                 processVbaEvent( nEventId, aArgs, nTab );
                 ProcessCompatibleVbaEvent( VBAEVENT_WORKBOOK_SHEET_SELECTIONCHANGE, aArgs );
             }
+            m_curSelection = aArgs[0];
             break;
         }
         // Workbook_sheet

sc/source/ui/vba/vbaeventshelper.hxx

     sal_Bool processVbaEvent( const sal_Int32 nEventId, const css::uno::Sequence< css::uno::Any >& rArgs, const SCTAB nTab = INVALID_TAB );
     void Initilize();
     void stopListening();
-    sal_Bool compareSelection( css::uno::Any oldSelection, css::uno::Any newSelection );
+    sal_Bool shouldFireSelectionEvent( css::uno::Any oldSelection, css::uno::Any newSelection, const SCTAB nNewSelTab );
 
 public:
 	//ScVbaEventsHelper( ScDocument* pDocument ):pDoc( pDocument ), mbOpened( sal_False ){};