sc/qa/unit/copy_paste_test.cxx                                    |    4 
 sc/source/ui/Accessibility/AccessibleText.cxx                     |    2 
 sc/source/ui/StatisticsDialogs/AnalysisOfVarianceDialog.cxx       |    4 
 sc/source/ui/StatisticsDialogs/ChiSquareTestDialog.cxx            |    4 
 sc/source/ui/StatisticsDialogs/DescriptiveStatisticsDialog.cxx    |    4 
 sc/source/ui/StatisticsDialogs/ExponentialSmoothingDialog.cxx     |    4 
 sc/source/ui/StatisticsDialogs/FTestDialog.cxx                    |    4 
 sc/source/ui/StatisticsDialogs/FourierAnalysisDialog.cxx          |    4 
 sc/source/ui/StatisticsDialogs/MatrixComparisonGenerator.cxx      |    4 
 sc/source/ui/StatisticsDialogs/MovingAverageDialog.cxx            |    4 
 sc/source/ui/StatisticsDialogs/RandomNumberGeneratorDialog.cxx    |    8 
 sc/source/ui/StatisticsDialogs/RegressionDialog.cxx               |    4 
 sc/source/ui/StatisticsDialogs/SamplingDialog.cxx                 |   24 
 sc/source/ui/StatisticsDialogs/StatisticsInputOutputDialog.cxx    |    8 
 sc/source/ui/StatisticsDialogs/StatisticsTwoVariableDialog.cxx    |    8 
 sc/source/ui/StatisticsDialogs/TTestDialog.cxx                    |    4 
 sc/source/ui/StatisticsDialogs/TableFillingAndNavigationTools.cxx |   18 
 sc/source/ui/StatisticsDialogs/ZTestDialog.cxx                    |    4 
 sc/source/ui/app/client.cxx                                       |    4 
 sc/source/ui/app/inputhdl.cxx                                     |   70 +-
 sc/source/ui/app/inputwin.cxx                                     |    8 
 sc/source/ui/app/scmod.cxx                                        |    4 
 sc/source/ui/app/seltrans.cxx                                     |   18 
 sc/source/ui/app/transobj.cxx                                     |    3 
 sc/source/ui/condformat/condformatdlg.cxx                         |    4 
 sc/source/ui/condformat/condformateasydlg.cxx                     |    2 
 sc/source/ui/dbgui/PivotLayoutDialog.cxx                          |    2 
 sc/source/ui/dbgui/dbnamdlg.cxx                                   |    2 
 sc/source/ui/dbgui/filtdlg.cxx                                    |    2 
 sc/source/ui/dbgui/sfiltdlg.cxx                                   |    2 
 sc/source/ui/dialogs/SparklineDataRangeDialog.cxx                 |    2 
 sc/source/ui/dialogs/SparklineDialog.cxx                          |    2 
 sc/source/ui/docshell/docfunc.cxx                                 |    2 
 sc/source/ui/docshell/docsh.cxx                                   |    2 
 sc/source/ui/docshell/docsh3.cxx                                  |    2 
 sc/source/ui/docshell/docsh4.cxx                                  |    2 
 sc/source/ui/docshell/docsh6.cxx                                  |    2 
 sc/source/ui/docshell/externalrefmgr.cxx                          |    4 
 sc/source/ui/drawfunc/drawsh2.cxx                                 |    2 
 sc/source/ui/drawfunc/drawsh5.cxx                                 |   20 
 sc/source/ui/drawfunc/drtxtob.cxx                                 |    4 
 sc/source/ui/drawfunc/drtxtob1.cxx                                |    2 
 sc/source/ui/drawfunc/fudraw.cxx                                  |    2 
 sc/source/ui/drawfunc/fuins2.cxx                                  |   18 
 sc/source/ui/drawfunc/fusel.cxx                                   |    4 
 sc/source/ui/drawfunc/fusel2.cxx                                  |    4 
 sc/source/ui/drawfunc/futext3.cxx                                 |    8 
 sc/source/ui/formdlg/formula.cxx                                  |    2 
 sc/source/ui/inc/AnalysisOfVarianceDialog.hxx                     |    2 
 sc/source/ui/inc/ChiSquareTestDialog.hxx                          |    2 
 sc/source/ui/inc/DescriptiveStatisticsDialog.hxx                  |    2 
 sc/source/ui/inc/ExponentialSmoothingDialog.hxx                   |    2 
 sc/source/ui/inc/FTestDialog.hxx                                  |    2 
 sc/source/ui/inc/FourierAnalysisDialog.hxx                        |    2 
 sc/source/ui/inc/MatrixComparisonGenerator.hxx                    |    2 
 sc/source/ui/inc/MovingAverageDialog.hxx                          |    2 
 sc/source/ui/inc/RegressionDialog.hxx                             |    2 
 sc/source/ui/inc/SamplingDialog.hxx                               |    6 
 sc/source/ui/inc/StatisticsInputOutputDialog.hxx                  |    2 
 sc/source/ui/inc/StatisticsTwoVariableDialog.hxx                  |    2 
 sc/source/ui/inc/TTestDialog.hxx                                  |    2 
 sc/source/ui/inc/TableFillingAndNavigationTools.hxx               |    4 
 sc/source/ui/inc/ZTestDialog.hxx                                  |    2 
 sc/source/ui/inc/datastreamdlg.hxx                                |    4 
 sc/source/ui/inc/inputhdl.hxx                                     |    4 
 sc/source/ui/inc/namedefdlg.hxx                                   |    2 
 sc/source/ui/inc/optsolver.hxx                                    |    4 
 sc/source/ui/inc/sharedocdlg.hxx                                  |    2 
 sc/source/ui/inc/viewdata.hxx                                     |    6 
 sc/source/ui/miscdlgs/acredlin.cxx                                |   38 -
 sc/source/ui/miscdlgs/crnrdlg.cxx                                 |    6 
 sc/source/ui/miscdlgs/datafdlg.cxx                                |   14 
 sc/source/ui/miscdlgs/datastreamdlg.cxx                           |   10 
 sc/source/ui/miscdlgs/highred.cxx                                 |    2 
 sc/source/ui/miscdlgs/instbdlg.cxx                                |    2 
 sc/source/ui/miscdlgs/optsolver.cxx                               |   24 
 sc/source/ui/miscdlgs/sharedocdlg.cxx                             |   17 
 sc/source/ui/namedlg/namedefdlg.cxx                               |    8 
 sc/source/ui/undo/undocell.cxx                                    |    2 
 sc/source/ui/unoobj/dispuno.cxx                                   |    2 
 sc/source/ui/unoobj/docuno.cxx                                    |    4 
 sc/source/ui/unoobj/viewuno.cxx                                   |   74 +-
 sc/source/ui/view/SparklineShell.cxx                              |    2 
 sc/source/ui/view/auditsh.cxx                                     |    2 
 sc/source/ui/view/cellsh.cxx                                      |   24 
 sc/source/ui/view/cellsh1.cxx                                     |   38 -
 sc/source/ui/view/cellsh2.cxx                                     |   23 
 sc/source/ui/view/cellsh3.cxx                                     |    4 
 sc/source/ui/view/dbfunc.cxx                                      |   64 +-
 sc/source/ui/view/dbfunc3.cxx                                     |   96 +--
 sc/source/ui/view/drawvie3.cxx                                    |    4 
 sc/source/ui/view/drawvie4.cxx                                    |   36 -
 sc/source/ui/view/drawview.cxx                                    |   23 
 sc/source/ui/view/editsh.cxx                                      |   12 
 sc/source/ui/view/formatsh.cxx                                    |    8 
 sc/source/ui/view/gridwin.cxx                                     |  140 ++--
 sc/source/ui/view/gridwin2.cxx                                    |   22 
 sc/source/ui/view/gridwin4.cxx                                    |   16 
 sc/source/ui/view/pgbrksh.cxx                                     |    2 
 sc/source/ui/view/pivotsh.cxx                                     |   10 
 sc/source/ui/view/printfun.cxx                                    |    3 
 sc/source/ui/view/select.cxx                                      |   10 
 sc/source/ui/view/spelldialog.cxx                                 |    2 
 sc/source/ui/view/spelleng.cxx                                    |    4 
 sc/source/ui/view/tabcont.cxx                                     |   12 
 sc/source/ui/view/tabview.cxx                                     |   15 
 sc/source/ui/view/tabview2.cxx                                    |   22 
 sc/source/ui/view/tabview3.cxx                                    |   17 
 sc/source/ui/view/tabview4.cxx                                    |    6 
 sc/source/ui/view/tabview5.cxx                                    |    8 
 sc/source/ui/view/tabvwsh3.cxx                                    |   28 
 sc/source/ui/view/tabvwsh4.cxx                                    |  110 +--
 sc/source/ui/view/tabvwsh5.cxx                                    |    6 
 sc/source/ui/view/tabvwsh9.cxx                                    |    2 
 sc/source/ui/view/tabvwsha.cxx                                    |   97 +--
 sc/source/ui/view/tabvwshb.cxx                                    |   36 -
 sc/source/ui/view/tabvwshd.cxx                                    |    6 
 sc/source/ui/view/tabvwshf.cxx                                    |   24 
 sc/source/ui/view/tabvwshh.cxx                                    |    2 
 sc/source/ui/view/viewdata.cxx                                    |   19 
 sc/source/ui/view/viewfun2.cxx                                    |  290 
+++++-----
 sc/source/ui/view/viewfun3.cxx                                    |  126 ++--
 sc/source/ui/view/viewfun4.cxx                                    |   52 -
 sc/source/ui/view/viewfun5.cxx                                    |    8 
 sc/source/ui/view/viewfun6.cxx                                    |   62 +-
 sc/source/ui/view/viewfun7.cxx                                    |    8 
 sc/source/ui/view/viewfunc.cxx                                    |  271 
++++-----
 127 files changed, 1166 insertions(+), 1188 deletions(-)

New commits:
commit d8ab8992bd35aedfc1d2b0debebab0d623aa8d09
Author:     Xisco Fauli <xiscofa...@libreoffice.org>
AuthorDate: Mon May 26 11:06:06 2025 +0200
Commit:     Xisco Fauli <xiscofa...@libreoffice.org>
CommitDate: Mon May 26 16:15:35 2025 +0200

    ScViewData: pass ScDocShell by ref
    
    After this change, testTdf159174 started to fail.
    Move the fix introduced in
    
    commit 0b93cc83993b3cd78aa05556342839a21c96dc5a
    Author: Mike Kaganski <mike.kagan...@collabora.com>
    Date:   Sun Jan 14 12:16:48 2024 +0600
    
        tdf#159174: there may be no tabs in the clipboard document
    
    to the caller in ScTransferObj::PaintToDev
    
    Change-Id: I770305dd0e1ac31f897ddcf4209f7aba1387f8fb
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/185786
    Reviewed-by: Xisco Fauli <xiscofa...@libreoffice.org>
    Tested-by: Jenkins

diff --git a/sc/qa/unit/copy_paste_test.cxx b/sc/qa/unit/copy_paste_test.cxx
index b1691fc23d5d..50e2693ec3e7 100644
--- a/sc/qa/unit/copy_paste_test.cxx
+++ b/sc/qa/unit/copy_paste_test.cxx
@@ -458,8 +458,8 @@ void 
ScCopyPasteTest::testTdf88782_autofillLinearNumbersInMergedCells()
     // merge the yellow cells
     ScCellMergeOption aMergeOptions(9, 11, 10, 13);     //J12:K14
     aMergeOptions.maTabs.insert(0);
-    ScDocShell* pDocSh = pViewShell->GetViewData().GetDocShell();
-    pDocSh->GetDocFunc().MergeCells(aMergeOptions, false, true, true, false);
+    ScDocShell& rDocSh = pViewShell->GetViewData().GetDocShell();
+    rDocSh.GetDocFunc().MergeCells(aMergeOptions, false, true, true, false);
 
     // fillauto numbers, these areas contain mostly merged cells
     pViewShell->FillAuto(FILL_TO_BOTTOM, 1, 8, 3, 14, 7);    // B9:D15 ->  
B9:D22
diff --git a/sc/source/ui/Accessibility/AccessibleText.cxx 
b/sc/source/ui/Accessibility/AccessibleText.cxx
index 3df9a2d0bcbb..db1fb175a3ec 100644
--- a/sc/source/ui/Accessibility/AccessibleText.cxx
+++ b/sc/source/ui/Accessibility/AccessibleText.cxx
@@ -653,7 +653,7 @@ ScDocShell* 
ScAccessibleCellTextData::GetDocShell(ScTabViewShell* pViewShell)
 {
     ScDocShell* pDocSh = nullptr;
     if (pViewShell)
-        pDocSh = pViewShell->GetViewData().GetDocShell();
+        pDocSh = &pViewShell->GetViewData().GetDocShell();
     return pDocSh;
 }
 
diff --git a/sc/source/ui/StatisticsDialogs/AnalysisOfVarianceDialog.cxx 
b/sc/source/ui/StatisticsDialogs/AnalysisOfVarianceDialog.cxx
index acfab698b601..ee8e93a054f0 100644
--- a/sc/source/ui/StatisticsDialogs/AnalysisOfVarianceDialog.cxx
+++ b/sc/source/ui/StatisticsDialogs/AnalysisOfVarianceDialog.cxx
@@ -538,9 +538,9 @@ void 
ScAnalysisOfVarianceDialog::AnovaTwoFactor(AddressWalkerWriter& output, For
     }
 }
 
-ScRange ScAnalysisOfVarianceDialog::ApplyOutput(ScDocShell* pDocShell)
+ScRange ScAnalysisOfVarianceDialog::ApplyOutput(ScDocShell& rDocShell)
 {
-    AddressWalkerWriter output(mOutputAddress, pDocShell, mDocument,
+    AddressWalkerWriter output(mOutputAddress, rDocShell, mDocument,
         
formula::FormulaGrammar::mergeToGrammar(formula::FormulaGrammar::GRAM_ENGLISH, 
mAddressDetails.eConv));
     FormulaTemplate aTemplate(&mDocument);
 
diff --git a/sc/source/ui/StatisticsDialogs/ChiSquareTestDialog.cxx 
b/sc/source/ui/StatisticsDialogs/ChiSquareTestDialog.cxx
index 5c1ada025aa8..32505d6c2269 100644
--- a/sc/source/ui/StatisticsDialogs/ChiSquareTestDialog.cxx
+++ b/sc/source/ui/StatisticsDialogs/ChiSquareTestDialog.cxx
@@ -37,9 +37,9 @@ TranslateId ScChiSquareTestDialog::GetUndoNameId()
     return STR_CHI_SQUARE_TEST;
 }
 
-ScRange ScChiSquareTestDialog::ApplyOutput(ScDocShell* pDocShell)
+ScRange ScChiSquareTestDialog::ApplyOutput(ScDocShell& rDocShell)
 {
-    AddressWalkerWriter aOutput(mOutputAddress, pDocShell, mDocument,
+    AddressWalkerWriter aOutput(mOutputAddress, rDocShell, mDocument,
             formula::FormulaGrammar::mergeToGrammar( 
formula::FormulaGrammar::GRAM_ENGLISH, mAddressDetails.eConv));
     FormulaTemplate aTemplate(&mDocument);
 
diff --git a/sc/source/ui/StatisticsDialogs/DescriptiveStatisticsDialog.cxx 
b/sc/source/ui/StatisticsDialogs/DescriptiveStatisticsDialog.cxx
index d82d7fa0e957..c1e298ef53f7 100644
--- a/sc/source/ui/StatisticsDialogs/DescriptiveStatisticsDialog.cxx
+++ b/sc/source/ui/StatisticsDialogs/DescriptiveStatisticsDialog.cxx
@@ -68,9 +68,9 @@ TranslateId ScDescriptiveStatisticsDialog::GetUndoNameId()
     return STR_DESCRIPTIVE_STATISTICS_UNDO_NAME;
 }
 
-ScRange ScDescriptiveStatisticsDialog::ApplyOutput(ScDocShell* pDocShell)
+ScRange ScDescriptiveStatisticsDialog::ApplyOutput(ScDocShell& rDocShell)
 {
-    AddressWalkerWriter aOutput(mOutputAddress, pDocShell, mDocument,
+    AddressWalkerWriter aOutput(mOutputAddress, rDocShell, mDocument,
             formula::FormulaGrammar::mergeToGrammar( 
formula::FormulaGrammar::GRAM_ENGLISH, mAddressDetails.eConv));
     FormulaTemplate aTemplate(&mDocument);
 
diff --git a/sc/source/ui/StatisticsDialogs/ExponentialSmoothingDialog.cxx 
b/sc/source/ui/StatisticsDialogs/ExponentialSmoothingDialog.cxx
index d7fe7c872167..b610ab580cdd 100644
--- a/sc/source/ui/StatisticsDialogs/ExponentialSmoothingDialog.cxx
+++ b/sc/source/ui/StatisticsDialogs/ExponentialSmoothingDialog.cxx
@@ -41,9 +41,9 @@ TranslateId ScExponentialSmoothingDialog::GetUndoNameId()
     return STR_EXPONENTIAL_SMOOTHING_UNDO_NAME;
 }
 
-ScRange ScExponentialSmoothingDialog::ApplyOutput(ScDocShell* pDocShell)
+ScRange ScExponentialSmoothingDialog::ApplyOutput(ScDocShell& rDocShell)
 {
-    AddressWalkerWriter output(mOutputAddress, pDocShell, mDocument,
+    AddressWalkerWriter output(mOutputAddress, rDocShell, mDocument,
             formula::FormulaGrammar::mergeToGrammar( 
formula::FormulaGrammar::GRAM_ENGLISH, mAddressDetails.eConv));
     FormulaTemplate aTemplate(&mDocument);
 
diff --git a/sc/source/ui/StatisticsDialogs/FTestDialog.cxx 
b/sc/source/ui/StatisticsDialogs/FTestDialog.cxx
index 68834bea55db..a4e527dab9b3 100644
--- a/sc/source/ui/StatisticsDialogs/FTestDialog.cxx
+++ b/sc/source/ui/StatisticsDialogs/FTestDialog.cxx
@@ -39,9 +39,9 @@ TranslateId ScFTestDialog::GetUndoNameId()
     return STR_FTEST_UNDO_NAME;
 }
 
-ScRange ScFTestDialog::ApplyOutput(ScDocShell* pDocShell)
+ScRange ScFTestDialog::ApplyOutput(ScDocShell& rDocShell)
 {
-    AddressWalkerWriter aOutput(mOutputAddress, pDocShell, mDocument,
+    AddressWalkerWriter aOutput(mOutputAddress, rDocShell, mDocument,
             
formula::FormulaGrammar::mergeToGrammar(formula::FormulaGrammar::GRAM_ENGLISH, 
mAddressDetails.eConv));
     FormulaTemplate aTemplate(&mDocument);
 
diff --git a/sc/source/ui/StatisticsDialogs/FourierAnalysisDialog.cxx 
b/sc/source/ui/StatisticsDialogs/FourierAnalysisDialog.cxx
index 94564358b9f0..5092d0426b90 100644
--- a/sc/source/ui/StatisticsDialogs/FourierAnalysisDialog.cxx
+++ b/sc/source/ui/StatisticsDialogs/FourierAnalysisDialog.cxx
@@ -51,10 +51,10 @@ void ScFourierAnalysisDialog::Close()
 
 TranslateId ScFourierAnalysisDialog::GetUndoNameId() { return 
STR_FOURIER_ANALYSIS_UNDO_NAME; }
 
-ScRange ScFourierAnalysisDialog::ApplyOutput(ScDocShell* pDocShell)
+ScRange ScFourierAnalysisDialog::ApplyOutput(ScDocShell& rDocShell)
 {
     getOptions();
-    AddressWalkerWriter aOutput(mOutputAddress, pDocShell, mDocument,
+    AddressWalkerWriter aOutput(mOutputAddress, rDocShell, mDocument,
                                 formula::FormulaGrammar::mergeToGrammar(
                                     formula::FormulaGrammar::GRAM_ENGLISH, 
mAddressDetails.eConv));
     FormulaTemplate aTemplate(&mDocument);
diff --git a/sc/source/ui/StatisticsDialogs/MatrixComparisonGenerator.cxx 
b/sc/source/ui/StatisticsDialogs/MatrixComparisonGenerator.cxx
index f2059ff822a8..a90c95369e7e 100644
--- a/sc/source/ui/StatisticsDialogs/MatrixComparisonGenerator.cxx
+++ b/sc/source/ui/StatisticsDialogs/MatrixComparisonGenerator.cxx
@@ -55,9 +55,9 @@ TranslateId ScMatrixComparisonGenerator::GetUndoNameId()
     return STR_CORRELATION_UNDO_NAME;
 }
 
-ScRange ScMatrixComparisonGenerator::ApplyOutput(ScDocShell* pDocShell)
+ScRange ScMatrixComparisonGenerator::ApplyOutput(ScDocShell& rDocShell)
 {
-    AddressWalkerWriter output(mOutputAddress, pDocShell, mDocument,
+    AddressWalkerWriter output(mOutputAddress, rDocShell, mDocument,
             formula::FormulaGrammar::mergeToGrammar( 
formula::FormulaGrammar::GRAM_ENGLISH, mAddressDetails.eConv));
     FormulaTemplate aTemplate(&mDocument);
 
diff --git a/sc/source/ui/StatisticsDialogs/MovingAverageDialog.cxx 
b/sc/source/ui/StatisticsDialogs/MovingAverageDialog.cxx
index 4cbe9343e200..246103a7ad62 100644
--- a/sc/source/ui/StatisticsDialogs/MovingAverageDialog.cxx
+++ b/sc/source/ui/StatisticsDialogs/MovingAverageDialog.cxx
@@ -42,9 +42,9 @@ TranslateId ScMovingAverageDialog::GetUndoNameId()
     return STR_MOVING_AVERAGE_UNDO_NAME;
 }
 
-ScRange ScMovingAverageDialog::ApplyOutput(ScDocShell* pDocShell)
+ScRange ScMovingAverageDialog::ApplyOutput(ScDocShell& rDocShell)
 {
-    AddressWalkerWriter output(mOutputAddress, pDocShell, mDocument,
+    AddressWalkerWriter output(mOutputAddress, rDocShell, mDocument,
             formula::FormulaGrammar::mergeToGrammar( 
formula::FormulaGrammar::GRAM_ENGLISH, mAddressDetails.eConv));
     FormulaTemplate aTemplate(&mDocument);
 
diff --git a/sc/source/ui/StatisticsDialogs/RandomNumberGeneratorDialog.cxx 
b/sc/source/ui/StatisticsDialogs/RandomNumberGeneratorDialog.cxx
index 96a324c7fad8..60d88fb06d03 100644
--- a/sc/source/ui/StatisticsDialogs/RandomNumberGeneratorDialog.cxx
+++ b/sc/source/ui/StatisticsDialogs/RandomNumberGeneratorDialog.cxx
@@ -254,8 +254,8 @@ void ScRandomNumberGeneratorDialog::GenerateNumbers(DIST& 
distribution, std::mt1
     OUString aDistributionName = ScResId(pDistributionStringId);
     aUndo = aUndo.replaceAll("%1",  aDistributionName);
 
-    ScDocShell* pDocShell = mrViewData.GetDocShell();
-    SfxUndoManager* pUndoManager = pDocShell->GetUndoManager();
+    ScDocShell& rDocShell = mrViewData.GetDocShell();
+    SfxUndoManager* pUndoManager = rDocShell.GetUndoManager();
     pUndoManager->EnterListAction( aUndo, aUndo, 0, 
mrViewData.GetViewShell()->GetViewShellId() );
 
     SCROW nRowStart = maInputRange.aStart.Row();
@@ -284,13 +284,13 @@ void ScRandomNumberGeneratorDialog::GenerateNumbers(DIST& 
distribution, std::mt1
                     aVals.push_back(distribution(seed));
             }
 
-            pDocShell->GetDocFunc().SetValueCells(aPos, aVals, true);
+            rDocShell.GetDocFunc().SetValueCells(aPos, aVals, true);
         }
     }
 
     pUndoManager->LeaveListAction();
 
-    pDocShell->PostPaint( maInputRange, PaintPartFlags::Grid );
+    rDocShell.PostPaint( maInputRange, PaintPartFlags::Grid );
 }
 
 IMPL_LINK_NOARG( ScRandomNumberGeneratorDialog, OkClicked, weld::Button&, void 
)
diff --git a/sc/source/ui/StatisticsDialogs/RegressionDialog.cxx 
b/sc/source/ui/StatisticsDialogs/RegressionDialog.cxx
index b75412947d96..3613eda61d05 100644
--- a/sc/source/ui/StatisticsDialogs/RegressionDialog.cxx
+++ b/sc/source/ui/StatisticsDialogs/RegressionDialog.cxx
@@ -171,9 +171,9 @@ TranslateId ScRegressionDialog::GetUndoNameId()
     return STR_REGRESSION_UNDO_NAME;
 }
 
-ScRange ScRegressionDialog::ApplyOutput(ScDocShell* pDocShell)
+ScRange ScRegressionDialog::ApplyOutput(ScDocShell& rDocShell)
 {
-    AddressWalkerWriter aOutput(mOutputAddress, pDocShell, mDocument,
+    AddressWalkerWriter aOutput(mOutputAddress, rDocShell, mDocument,
             formula::FormulaGrammar::mergeToGrammar( 
formula::FormulaGrammar::GRAM_ENGLISH, mAddressDetails.eConv));
     FormulaTemplate aTemplate(&mDocument);
     aTemplate.autoReplaceUses3D(mbUse3DAddresses);
diff --git a/sc/source/ui/StatisticsDialogs/SamplingDialog.cxx 
b/sc/source/ui/StatisticsDialogs/SamplingDialog.cxx
index c6a807ddf3f8..cacbf772bf20 100644
--- a/sc/source/ui/StatisticsDialogs/SamplingDialog.cxx
+++ b/sc/source/ui/StatisticsDialogs/SamplingDialog.cxx
@@ -170,7 +170,7 @@ void ScSamplingDialog::SetReference( const ScRange& 
rReferenceRange, ScDocument&
     mxButtonOk->set_sensitive(mInputRange.IsValid() && 
mOutputAddress.IsValid());
 }
 
-ScRange ScSamplingDialog::PerformPeriodicSampling(ScDocShell* pDocShell)
+ScRange ScSamplingDialog::PerformPeriodicSampling(ScDocShell& rDocShell)
 {
     ScAddress aStart = mInputRange.aStart;
     ScAddress aEnd   = mInputRange.aEnd;
@@ -193,7 +193,7 @@ ScRange 
ScSamplingDialog::PerformPeriodicSampling(ScDocShell* pDocShell)
                 if (i % aPeriod == aPeriod - 1 ) // Sample the last of period
                 {
                     double aValue = mDocument.GetValue(ScAddress(inCol, inRow, 
inTab));
-                    pDocShell->GetDocFunc().SetValueCell(ScAddress(outCol, 
outRow, outTab), aValue, true);
+                    rDocShell.GetDocFunc().SetValueCell(ScAddress(outCol, 
outRow, outTab), aValue, true);
                     outRow++;
                 }
                 i++;
@@ -206,7 +206,7 @@ ScRange 
ScSamplingDialog::PerformPeriodicSampling(ScDocShell* pDocShell)
     return ScRange(mOutputAddress, ScAddress(outTab, outRow, outTab) );
 }
 
-ScRange ScSamplingDialog::PerformRandomSampling(ScDocShell* pDocShell)
+ScRange ScSamplingDialog::PerformRandomSampling(ScDocShell& rDocShell)
 {
     ScAddress aStart = mInputRange.aStart;
     ScAddress aEnd   = mInputRange.aEnd;
@@ -273,7 +273,7 @@ ScRange ScSamplingDialog::PerformRandomSampling(ScDocShell* 
pDocShell)
                 }
 
                 const double fValue = mDocument.GetValue( ScAddress(inCol, 
nRandom, inTab) );
-                pDocShell->GetDocFunc().SetValueCell(ScAddress(outCol, outRow, 
outTab), fValue, true);
+                rDocShell.GetDocFunc().SetValueCell(ScAddress(outCol, outRow, 
outTab), fValue, true);
                 outRow++;
             }
             outCol++;
@@ -284,7 +284,7 @@ ScRange ScSamplingDialog::PerformRandomSampling(ScDocShell* 
pDocShell)
     return ScRange(mOutputAddress, ScAddress(outTab, outRow, outTab) );
 }
 
-ScRange ScSamplingDialog::PerformRandomSamplingKeepOrder(ScDocShell* pDocShell)
+ScRange ScSamplingDialog::PerformRandomSamplingKeepOrder(ScDocShell& rDocShell)
 {
     ScAddress aStart = mInputRange.aStart;
     ScAddress aEnd   = mInputRange.aEnd;
@@ -317,7 +317,7 @@ ScRange 
ScSamplingDialog::PerformRandomSamplingKeepOrder(ScDocShell* pDocShell)
                 else
                 {
                     double aValue = mDocument.GetValue( ScAddress(inCol, 
inRow, inTab) );
-                    pDocShell->GetDocFunc().SetValueCell(ScAddress(outCol, 
outRow, outTab), aValue, true);
+                    rDocShell.GetDocFunc().SetValueCell(ScAddress(outCol, 
outRow, outTab), aValue, true);
                     inRow++;
                     outRow++;
                 }
@@ -333,8 +333,8 @@ ScRange 
ScSamplingDialog::PerformRandomSamplingKeepOrder(ScDocShell* pDocShell)
 void ScSamplingDialog::PerformSampling()
 {
     OUString aUndo(ScResId(STR_SAMPLING_UNDO_NAME));
-    ScDocShell* pDocShell = mViewData.GetDocShell();
-    SfxUndoManager* pUndoManager = pDocShell->GetUndoManager();
+    ScDocShell& rDocShell = mViewData.GetDocShell();
+    SfxUndoManager* pUndoManager = rDocShell.GetUndoManager();
 
     ScRange aModifiedRange;
 
@@ -343,17 +343,17 @@ void ScSamplingDialog::PerformSampling()
     if (mxRandomMethodRadio->get_active())
     {
         if (mxKeepOrder->get_sensitive() && mxKeepOrder->get_active())
-            aModifiedRange = PerformRandomSamplingKeepOrder(pDocShell);
+            aModifiedRange = PerformRandomSamplingKeepOrder(rDocShell);
         else
-            aModifiedRange = PerformRandomSampling(pDocShell);
+            aModifiedRange = PerformRandomSampling(rDocShell);
     }
     else if (mxPeriodicMethodRadio->get_active())
     {
-        aModifiedRange = PerformPeriodicSampling(pDocShell);
+        aModifiedRange = PerformPeriodicSampling(rDocShell);
     }
 
     pUndoManager->LeaveListAction();
-    pDocShell->PostPaint(aModifiedRange, PaintPartFlags::Grid);
+    rDocShell.PostPaint(aModifiedRange, PaintPartFlags::Grid);
 }
 
 sal_Int64 ScSamplingDialog::GetPopulationSize() const
diff --git a/sc/source/ui/StatisticsDialogs/StatisticsInputOutputDialog.cxx 
b/sc/source/ui/StatisticsDialogs/StatisticsInputOutputDialog.cxx
index cff235681f1e..badba7855b70 100644
--- a/sc/source/ui/StatisticsDialogs/StatisticsInputOutputDialog.cxx
+++ b/sc/source/ui/StatisticsDialogs/StatisticsInputOutputDialog.cxx
@@ -281,14 +281,14 @@ IMPL_LINK_NOARG( ScStatisticsInputOutputDialog, 
RefInputModifyHandler, formula::
 void ScStatisticsInputOutputDialog::CalculateInputAndWriteToOutput()
 {
     OUString aUndo(ScResId(GetUndoNameId()));
-    ScDocShell* pDocShell = mViewData.GetDocShell();
-    SfxUndoManager* pUndoManager = pDocShell->GetUndoManager();
+    ScDocShell& rDocShell = mViewData.GetDocShell();
+    SfxUndoManager* pUndoManager = rDocShell.GetUndoManager();
     pUndoManager->EnterListAction( aUndo, aUndo, 0, 
mViewData.GetViewShell()->GetViewShellId() );
 
-    ScRange aOutputRange = ApplyOutput(pDocShell);
+    ScRange aOutputRange = ApplyOutput(rDocShell);
 
     pUndoManager->LeaveListAction();
-    pDocShell->PostPaint( aOutputRange, PaintPartFlags::Grid );
+    rDocShell.PostPaint( aOutputRange, PaintPartFlags::Grid );
 }
 
 bool ScStatisticsInputOutputDialog::InputRangesValid()
diff --git a/sc/source/ui/StatisticsDialogs/StatisticsTwoVariableDialog.cxx 
b/sc/source/ui/StatisticsDialogs/StatisticsTwoVariableDialog.cxx
index c0fcbc751dbc..7b495a876b1f 100644
--- a/sc/source/ui/StatisticsDialogs/StatisticsTwoVariableDialog.cxx
+++ b/sc/source/ui/StatisticsDialogs/StatisticsTwoVariableDialog.cxx
@@ -323,14 +323,14 @@ IMPL_LINK_NOARG( ScStatisticsTwoVariableDialog, 
RefInputModifyHandler, formula::
 void ScStatisticsTwoVariableDialog::CalculateInputAndWriteToOutput()
 {
     OUString aUndo(ScResId(GetUndoNameId()));
-    ScDocShell* pDocShell = mViewData.GetDocShell();
-    SfxUndoManager* pUndoManager = pDocShell->GetUndoManager();
+    ScDocShell& rDocShell = mViewData.GetDocShell();
+    SfxUndoManager* pUndoManager = rDocShell.GetUndoManager();
     pUndoManager->EnterListAction( aUndo, aUndo, 0, 
mViewData.GetViewShell()->GetViewShellId() );
 
-    ScRange aOutputRange = ApplyOutput(pDocShell);
+    ScRange aOutputRange = ApplyOutput(rDocShell);
 
     pUndoManager->LeaveListAction();
-    pDocShell->PostPaint( aOutputRange, PaintPartFlags::Grid );
+    rDocShell.PostPaint( aOutputRange, PaintPartFlags::Grid );
 }
 
 bool ScStatisticsTwoVariableDialog::InputRangesValid()
diff --git a/sc/source/ui/StatisticsDialogs/TTestDialog.cxx 
b/sc/source/ui/StatisticsDialogs/TTestDialog.cxx
index 7f57ebebdb35..b354f4ee02a8 100644
--- a/sc/source/ui/StatisticsDialogs/TTestDialog.cxx
+++ b/sc/source/ui/StatisticsDialogs/TTestDialog.cxx
@@ -39,9 +39,9 @@ TranslateId ScTTestDialog::GetUndoNameId()
     return STR_TTEST_UNDO_NAME;
 }
 
-ScRange ScTTestDialog::ApplyOutput(ScDocShell* pDocShell)
+ScRange ScTTestDialog::ApplyOutput(ScDocShell& rDocShell)
 {
-    AddressWalkerWriter aOutput(mOutputAddress, pDocShell, mDocument,
+    AddressWalkerWriter aOutput(mOutputAddress, rDocShell, mDocument,
             formula::FormulaGrammar::mergeToGrammar( 
formula::FormulaGrammar::GRAM_ENGLISH, mAddressDetails.eConv));
     FormulaTemplate aTemplate(&mDocument);
 
diff --git a/sc/source/ui/StatisticsDialogs/TableFillingAndNavigationTools.cxx 
b/sc/source/ui/StatisticsDialogs/TableFillingAndNavigationTools.cxx
index d109fb537fbd..502d467cc5b8 100644
--- a/sc/source/ui/StatisticsDialogs/TableFillingAndNavigationTools.cxx
+++ b/sc/source/ui/StatisticsDialogs/TableFillingAndNavigationTools.cxx
@@ -154,17 +154,17 @@ void AddressWalker::push(SCCOL aRelativeCol, SCROW 
aRelativeRow, SCTAB aRelative
     mAddressStack.push_back(mCurrentAddress);
 }
 
-AddressWalkerWriter::AddressWalkerWriter(const ScAddress& aInitialAddress, 
ScDocShell* pDocShell, ScDocument& rDocument,
+AddressWalkerWriter::AddressWalkerWriter(const ScAddress& aInitialAddress, 
ScDocShell& rDocShell, ScDocument& rDocument,
         formula::FormulaGrammar::Grammar eGrammar ) :
     AddressWalker(aInitialAddress),
-    mpDocShell(pDocShell),
+    mrDocShell(rDocShell),
     mrDocument(rDocument),
     meGrammar(eGrammar)
 {}
 
 void AddressWalkerWriter::writeFormula(const OUString& aFormula)
 {
-    mpDocShell->GetDocFunc().SetFormulaCell(mCurrentAddress,
+    mrDocShell.GetDocFunc().SetFormulaCell(mCurrentAddress,
             new ScFormulaCell(mrDocument, mCurrentAddress, aFormula, 
meGrammar), true);
 }
 
@@ -174,7 +174,7 @@ void AddressWalkerWriter::writeFormulas(const 
std::vector<OUString>& rFormulas)
     if (!nLength)
         return;
 
-    const size_t nMaxLen = mpDocShell->GetDocument().MaxRow() - 
mCurrentAddress.Row() + 1;
+    const size_t nMaxLen = mrDocShell.GetDocument().MaxRow() - 
mCurrentAddress.Row() + 1;
     // If not done already, trim the length to fit.
     if (nLength > nMaxLen)
         nLength = nMaxLen;
@@ -187,7 +187,7 @@ void AddressWalkerWriter::writeFormulas(const 
std::vector<OUString>& rFormulas)
         aAddr.IncRow(1);
     }
 
-    mpDocShell->GetDocFunc().SetFormulaCells(mCurrentAddress, aFormulaCells, 
true);
+    mrDocShell.GetDocFunc().SetFormulaCells(mCurrentAddress, aFormulaCells, 
true);
 }
 
 void AddressWalkerWriter::writeMatrixFormula(const OUString& aFormula, SCCOL 
nCols, SCROW nRows)
@@ -199,12 +199,12 @@ void AddressWalkerWriter::writeMatrixFormula(const 
OUString& aFormula, SCCOL nCo
         aRange.aEnd.IncCol(nCols - 1);
     if (nRows > 1)
         aRange.aEnd.IncRow(nRows - 1);
-    mpDocShell->GetDocFunc().EnterMatrix(aRange, nullptr, nullptr, aFormula, 
false, false, OUString(), meGrammar );
+    mrDocShell.GetDocFunc().EnterMatrix(aRange, nullptr, nullptr, aFormula, 
false, false, OUString(), meGrammar );
 }
 
 void AddressWalkerWriter::writeString(const OUString& aString)
 {
-    mpDocShell->GetDocFunc().SetStringCell(mCurrentAddress, aString, true);
+    mrDocShell.GetDocFunc().SetStringCell(mCurrentAddress, aString, true);
 }
 
 void AddressWalkerWriter::writeString(const char* aCharArray)
@@ -223,12 +223,12 @@ void AddressWalkerWriter::writeBoldString(const OUString& 
aString)
     aItemSet.Put(aJustify);
     rEngine.QuickSetAttribs(aItemSet, ESelection(0, 0, 0, aString.getLength()) 
);
     std::unique_ptr<EditTextObject> pEditText(rEngine.CreateTextObject());
-    mpDocShell->GetDocFunc().SetEditCell(mCurrentAddress, *pEditText, true);
+    mrDocShell.GetDocFunc().SetEditCell(mCurrentAddress, *pEditText, true);
 }
 
 void AddressWalkerWriter::writeValue(double aValue)
 {
-    mpDocShell->GetDocFunc().SetValueCell(mCurrentAddress, aValue, true);
+    mrDocShell.GetDocFunc().SetValueCell(mCurrentAddress, aValue, true);
 }
 
 // Applies a column header format to the current cell and subsequent (nCols - 
1) columns
diff --git a/sc/source/ui/StatisticsDialogs/ZTestDialog.cxx 
b/sc/source/ui/StatisticsDialogs/ZTestDialog.cxx
index 627c1cf65006..0966af0aaf1b 100644
--- a/sc/source/ui/StatisticsDialogs/ZTestDialog.cxx
+++ b/sc/source/ui/StatisticsDialogs/ZTestDialog.cxx
@@ -39,9 +39,9 @@ TranslateId ScZTestDialog::GetUndoNameId()
     return STR_ZTEST_UNDO_NAME;
 }
 
-ScRange ScZTestDialog::ApplyOutput(ScDocShell* pDocShell)
+ScRange ScZTestDialog::ApplyOutput(ScDocShell& rDocShell)
 {
-    AddressWalkerWriter aOutput(mOutputAddress, pDocShell, mDocument,
+    AddressWalkerWriter aOutput(mOutputAddress, rDocShell, mDocument,
             formula::FormulaGrammar::mergeToGrammar( 
formula::FormulaGrammar::GRAM_ENGLISH, mAddressDetails.eConv));
     FormulaTemplate aTemplate(&mDocument);
 
diff --git a/sc/source/ui/app/client.cxx b/sc/source/ui/app/client.cxx
index 5c44bc077e83..a13372ca9bdf 100644
--- a/sc/source/ui/app/client.cxx
+++ b/sc/source/ui/app/client.cxx
@@ -168,7 +168,7 @@ void ScClient::ObjectAreaChanged()
     pDrawObj->setSuppressSetVisAreaSize(false);
 
     //  set document modified (SdrModel::SetChanged is not used)
-    pViewSh->GetViewData().GetDocShell()->SetDrawModified();
+    pViewSh->GetViewData().GetDocShell().SetDrawModified();
     pViewSh->ScrollToObject(pDrawObj);
 }
 
@@ -232,7 +232,7 @@ void ScClient::ViewChanged()
             pDrawObj->SetLogicRect( aLogicRect );
 
             // set document modified (SdrModel::SetChanged is not used)
-            pViewSh->GetViewData().GetDocShell()->SetDrawModified();
+            pViewSh->GetViewData().GetDocShell().SetDrawModified();
         }
     }
 }
diff --git a/sc/source/ui/app/inputhdl.cxx b/sc/source/ui/app/inputhdl.cxx
index ea59d4e44105..5c0a718a5ba6 100644
--- a/sc/source/ui/app/inputhdl.cxx
+++ b/sc/source/ui/app/inputhdl.cxx
@@ -362,8 +362,8 @@ void ScInputHandler::InitRangeFinder( const OUString& 
rFormula )
     DeleteRangeFinder();
     if (!pActiveViewSh || !ScModule::get()->GetInputOptions().GetRangeFinder())
         return;
-    ScDocShell* pDocSh = pActiveViewSh->GetViewData().GetDocShell();
-    ScDocument& rDoc = pDocSh->GetDocument();
+    ScDocShell& rDocSh = pActiveViewSh->GetViewData().GetDocShell();
+    ScDocument& rDoc = rDocSh.GetDocument();
     const sal_Unicode cSheetSep = rDoc.GetSheetSeparator();
 
     OUString aDelimiters = ScEditUtil::ModifyDelimiters(u" !~%\"       
"_ustr);
@@ -458,7 +458,7 @@ handle_r1c1:
                 if (!nCount)
                 {
                     mpEditEngine->SetUpdateLayout( false );
-                    pRangeFindList.reset(new ScRangeFindList( 
pDocSh->GetTitle() ));
+                    pRangeFindList.reset(new ScRangeFindList( 
rDocSh.GetTitle() ));
                 }
 
                 Color nColor = pRangeFindList->Insert( ScRangeFindData( 
aRange, nFlags, aSel));
@@ -479,11 +479,11 @@ handle_r1c1:
     {
         mpEditEngine->SetUpdateLayout( true );
 
-        pDocSh->Broadcast( SfxHint( SfxHintId::ScShowRangeFinder ) );
+        rDocSh.Broadcast( SfxHint( SfxHintId::ScShowRangeFinder ) );
     }
 }
 
-ReferenceMark ScInputHandler::GetReferenceMark( const ScViewData& rViewData, 
ScDocShell* pDocSh,
+ReferenceMark ScInputHandler::GetReferenceMark( const ScViewData& rViewData, 
ScDocShell& rDocSh,
                                     tools::Long nX1, tools::Long nX2, 
tools::Long nY1, tools::Long nY2,
                                     tools::Long nTab, const Color& rColor )
 {
@@ -495,7 +495,7 @@ ReferenceMark ScInputHandler::GetReferenceMark( const 
ScViewData& rViewData, ScD
     {
         SCCOL nCol1 = nX1, nCol2 = nX2;
         SCROW nRow1 = nY1, nRow2 = nY2;
-        ScDocument& rDoc = pDocSh->GetDocument();
+        ScDocument& rDoc = rDocSh.GetDocument();
 
         PutInOrder(nCol1, nCol2);
         PutInOrder(nRow1, nRow2);
@@ -524,11 +524,11 @@ ReferenceMark ScInputHandler::GetReferenceMark( const 
ScViewData& rViewData, ScD
     Fraction aZoomY = rViewData.GetZoomY();
 
     ScTableInfo aTabInfo(nY1, nY2, true);
-    pDocSh->GetDocument().FillInfo( aTabInfo, nX1, nY1, nX2, nY2,
+    rDocSh.GetDocument().FillInfo( aTabInfo, nX1, nY1, nX2, nY2,
                                     nTab, nPPTX, nPPTY, false, false );
 
     ScOutputData aOutputData( nullptr, OUTTYPE_WINDOW, aTabInfo,
-                              &( pDocSh->GetDocument() ), nTab,
+                              &( rDocSh.GetDocument() ), nTab,
                               nScrX, nScrY,
                               nX1, nY1, nX2, nY2,
                               nPPTX, nPPTY,
@@ -550,7 +550,7 @@ void ScInputHandler::UpdateLokReferenceMarks()
         return;
 
     ScViewData& rViewData = pShell->GetViewData();
-    ScDocShell* pDocSh = rViewData.GetDocShell();
+    ScDocShell& rDocSh = rViewData.GetDocShell();
     ScRangeFindList* pRangeFinder = GetRangeFindList();
 
     if ( !pRangeFinder && !rViewData.IsRefMode() )
@@ -577,7 +577,7 @@ void ScInputHandler::UpdateLokReferenceMarks()
         PutInOrder(nX1, nX2);
         PutInOrder(nY1, nY2);
 
-        aReferenceMarks[0] = ScInputHandler::GetReferenceMark( rViewData, 
pDocSh,
+        aReferenceMarks[0] = ScInputHandler::GetReferenceMark( rViewData, 
rDocSh,
                                                    nX1, nX2, nY1, nY2,
                                                    nTab, aRefColor );
     }
@@ -587,7 +587,7 @@ void ScInputHandler::UpdateLokReferenceMarks()
     aReferenceMarks.resize( nCount );
 
     if ( nCount && pRangeFinder && !pRangeFinder->IsHidden() &&
-         pRangeFinder->GetDocName() == pDocSh->GetTitle() )
+         pRangeFinder->GetDocName() == rDocSh.GetTitle() )
     {
         for (sal_uInt16 i = 0; i < nCount - nAdditionalMarks; i++)
         {
@@ -601,7 +601,7 @@ void ScInputHandler::UpdateLokReferenceMarks()
             tools::Long nY2 = aRef.aEnd.Row();
             tools::Long nTab = aRef.aStart.Tab();
 
-            aReferenceMarks[i + nAdditionalMarks] = 
ScInputHandler::GetReferenceMark( rViewData, pDocSh,
+            aReferenceMarks[i + nAdditionalMarks] = 
ScInputHandler::GetReferenceMark( rViewData, rDocSh,
                                                                           nX1, 
nX2, nY1, nY2,
                                                                           
nTab, rData.nColor );
 
@@ -706,9 +706,9 @@ void ScInputHandler::DeleteRangeFinder()
     ScTabViewShell* pPaintView = pRefViewSh ? pRefViewSh : pActiveViewSh;
     if ( pRangeFindList && pPaintView )
     {
-        ScDocShell* pDocSh = pActiveViewSh->GetViewData().GetDocShell();
+        ScDocShell& rDocSh = pActiveViewSh->GetViewData().GetDocShell();
         pRangeFindList->SetHidden(true);
-        pDocSh->Broadcast( SfxHint( SfxHintId::ScShowRangeFinder ) );  // Steal
+        rDocSh.Broadcast( SfxHint( SfxHintId::ScShowRangeFinder ) );  // Steal
         pRangeFindList.reset();
     }
 }
@@ -925,7 +925,7 @@ void ScInputHandler::ImplCreateEditEngine()
 
     // we cannot create a properly initialised EditEngine until we have a 
document
     assert( pActiveViewSh );
-    ScDocument& rDoc = 
pActiveViewSh->GetViewData().GetDocShell()->GetDocument();
+    ScDocument& rDoc = 
pActiveViewSh->GetViewData().GetDocShell().GetDocument();
     mpEditEngine = std::make_unique<ScFieldEditEngine>(&rDoc, 
rDoc.GetEnginePool(), rDoc.GetEditPool());
     mpEditEngine->SetWordDelimiters( ScEditUtil::ModifyDelimiters( 
mpEditEngine->GetWordDelimiters() ) );
     UpdateRefDevice();      // also sets MapMode
@@ -1014,7 +1014,7 @@ void ScInputHandler::GetFormulaData()
     if ( !pActiveViewSh )
         return;
 
-    ScDocument& rDoc = 
pActiveViewSh->GetViewData().GetDocShell()->GetDocument();
+    ScDocument& rDoc = 
pActiveViewSh->GetViewData().GetDocShell().GetDocument();
 
     if ( pFormulaData )
         pFormulaData->clear();
@@ -1130,9 +1130,9 @@ void ScInputHandler::ShowArgumentsTip( OUString& rSelText 
)
     if ( !pActiveViewSh )
         return;
 
-    ScDocShell* pDocSh = pActiveViewSh->GetViewData().GetDocShell();
+    ScDocShell& rDocSh = pActiveViewSh->GetViewData().GetDocShell();
     const sal_Unicode cSep = ScCompiler::GetNativeSymbolChar(ocSep);
-    const sal_Unicode cSheetSep = pDocSh->GetDocument().GetSheetSeparator();
+    const sal_Unicode cSheetSep = rDocSh.GetDocument().GetSheetSeparator();
     FormulaHelper aHelper(ScGlobal::GetStarCalcFunctionMgr());
     bool bFound = false;
     while( !bFound )
@@ -1861,7 +1861,7 @@ void ScTabViewShell::LOKSendFormulabarUpdate(const 
EditView* pActiveView,
     maSendFormulabarUpdate.m_nTimeStamp = now;
 
     ScViewData& rViewData = this->GetViewData();
-    const ScDocument& rDoc = rViewData.GetDocShell()->GetDocument();
+    const ScDocument& rDoc = rViewData.GetDocShell().GetDocument();
     const ScPatternAttr* pPattern = rDoc.GetPattern(rViewData.GetCurX(), 
rViewData.GetCurY(), rViewData.GetRefTabNo());
 
     if (pPattern)
@@ -1957,7 +1957,7 @@ void ScInputHandler::FormulaPreview()
         OUString aPart = pActiveView->GetSelected();
         if (aPart.isEmpty())
             aPart = mpEditEngine->GetText(0);
-        ScDocument& rDoc = 
pActiveViewSh->GetViewData().GetDocShell()->GetDocument();
+        ScDocument& rDoc = 
pActiveViewSh->GetViewData().GetDocShell().GetDocument();
         aValue = lcl_Calculate( aPart, rDoc, aCursorPos );
     }
 
@@ -2089,7 +2089,7 @@ void ScInputHandler::GetColData()
     if ( !pActiveViewSh )
         return;
 
-    ScDocument& rDoc = 
pActiveViewSh->GetViewData().GetDocShell()->GetDocument();
+    ScDocument& rDoc = 
pActiveViewSh->GetViewData().GetDocShell().GetDocument();
 
     if ( pColumnData )
         pColumnData->clear();
@@ -2432,7 +2432,7 @@ void ScInputHandler::UpdateAdjust( sal_Unicode cTyped )
                     bNumber = (cTyped>='0' && cTyped<='9');     // Only 
ciphers are numbers
                 else if ( pActiveViewSh )
                 {
-                    ScDocument& rDoc = 
pActiveViewSh->GetViewData().GetDocShell()->GetDocument();
+                    ScDocument& rDoc = 
pActiveViewSh->GetViewData().GetDocShell().GetDocument();
                     bNumber = ( rDoc.GetCellType( aCursorPos ) == 
CELLTYPE_VALUE );
                 }
                 eSvxAdjust = bNumber ? SvxAdjust::Right : SvxAdjust::Left;
@@ -2512,7 +2512,7 @@ bool ScInputHandler::StartTable( sal_Unicode cTyped, bool 
bFromCommand, bool bIn
 
     if (pActiveViewSh)
     {
-        ScDocument& rDoc = 
pActiveViewSh->GetViewData().GetDocShell()->GetDocument();
+        ScDocument& rDoc = 
pActiveViewSh->GetViewData().GetDocShell().GetDocument();
 
         if (!rDoc.ValidCol(aCursorPos.Col()))
             return false;
@@ -2540,7 +2540,7 @@ bool ScInputHandler::StartTable( sal_Unicode cTyped, bool 
bFromCommand, bool bIn
             // activation is requested (double-click or F2) or a click in input
             // line.
             bool bShowError = (!bInputActivated || !aTester.GetMessageId() || 
aTester.GetMessageId() != STR_PROTECTIONERR) &&
-                !pActiveViewSh->GetViewData().GetDocShell()->IsReadOnly();
+                !pActiveViewSh->GetViewData().GetDocShell().IsReadOnly();
             if (bShowError)
             {
                 eMode = SC_INPUT_NONE;
@@ -3050,7 +3050,7 @@ void ScInputHandler::SetMode( ScInputMode eNewMode, const 
OUString* pInitText, S
         {
             if (StartTable(0, false, eMode == SC_INPUT_TABLE, pTopEngine))
             {
-                
pActiveViewSh->GetViewData().GetDocShell()->PostEditView(*mpEditEngine, 
aCursorPos);
+                
pActiveViewSh->GetViewData().GetDocShell().PostEditView(*mpEditEngine, 
aCursorPos);
             }
         }
 
@@ -3577,12 +3577,12 @@ void ScInputHandler::CancelHandler()
     }
 }
 
-bool ScInputHandler::IsModalMode( const SfxObjectShell* pDocSh )
+bool ScInputHandler::IsModalMode( const SfxObjectShell& rDocSh )
 {
     // References to unnamed document; that doesn't work
     return bFormulaMode && pRefViewSh
-            && pRefViewSh->GetViewData().GetDocument().GetDocumentShell() != 
pDocSh
-            && !pDocSh->HasName();
+            && pRefViewSh->GetViewData().GetDocument().GetDocumentShell() != 
&rDocSh
+            && !rDocSh.HasName();
 }
 
 void ScInputHandler::AddRefEntry()
@@ -3966,7 +3966,7 @@ bool ScInputHandler::KeyInput( const KeyEvent& rKEvt, 
bool bStartEdit /* = false
             if (bNewView )                          // Create anew
             {
                 if (pActiveViewSh)
-                    
pActiveViewSh->GetViewData().GetDocShell()->PostEditView(*mpEditEngine, 
aCursorPos);
+                    
pActiveViewSh->GetViewData().GetDocShell().PostEditView(*mpEditEngine, 
aCursorPos);
                 UpdateActiveView();
                 if (eMode==SC_INPUT_NONE)
                     if (pTableView || pTopView)
@@ -4089,8 +4089,8 @@ bool ScInputHandler::KeyInput( const KeyEvent& rKEvt, 
bool bStartEdit /* = false
             // right away at the start of the edit, so that the content is
             // saved even when the user leaves the document before hitting
             // Enter
-            if (comphelper::LibreOfficeKit::isActive() && bSetModified && 
pActiveViewSh && !pActiveViewSh->GetViewData().GetDocShell()->IsModified())
-                pActiveViewSh->GetViewData().GetDocShell()->SetModified();
+            if (comphelper::LibreOfficeKit::isActive() && bSetModified && 
pActiveViewSh && !pActiveViewSh->GetViewData().GetDocShell().IsModified())
+                pActiveViewSh->GetViewData().GetDocShell().SetModified();
 
             InvalidateAttribs();        //! in DataChanged?
         }
@@ -4200,7 +4200,7 @@ void ScInputHandler::InputCommand( const CommandEvent& 
rCEvt )
         {
             if (bNewView)                           // create new edit view
             {
-                
pActiveViewSh->GetViewData().GetDocShell()->PostEditView(*mpEditEngine, 
aCursorPos);
+                
pActiveViewSh->GetViewData().GetDocShell().PostEditView(*mpEditEngine, 
aCursorPos);
                 UpdateActiveView();
                 if (eMode==SC_INPUT_NONE)
                     if (pTableView || pTopView)
@@ -4324,8 +4324,8 @@ void ScInputHandler::NotifyChange( const ScInputHdlState* 
pState,
                 const EditTextObject*   pData   = pState->GetEditData();
                 OUString aString = pState->GetString();
                 bool bTxtMod = false;
-                ScDocShell* pDocSh = 
pActiveViewSh->GetViewData().GetDocShell();
-                ScDocument& rDoc = pDocSh->GetDocument();
+                ScDocShell& rDocSh = 
pActiveViewSh->GetViewData().GetDocShell();
+                ScDocument& rDoc = rDocSh.GetDocument();
 
                 aCursorPos  = pState->GetPos();
 
@@ -4563,7 +4563,7 @@ void ScInputHandler::InputChanged( const EditView* pView, 
bool bFromNotify )
     {
         ScViewData& rViewData = pActiveViewSh->GetViewData();
         if ( bNewView )
-            rViewData.GetDocShell()->PostEditView(*mpEditEngine, aCursorPos);
+            rViewData.GetDocShell().PostEditView(*mpEditEngine, aCursorPos);
 
         rViewData.EditGrowY();
         rViewData.EditGrowX();
diff --git a/sc/source/ui/app/inputwin.cxx b/sc/source/ui/app/inputwin.cxx
index 79f5f62e202d..315f190c5666 100644
--- a/sc/source/ui/app/inputwin.cxx
+++ b/sc/source/ui/app/inputwin.cxx
@@ -1534,7 +1534,7 @@ void ScTextWnd::InitEditEngine()
     ScDocShell* pDocSh = nullptr;
     if ( mpViewShell )
     {
-        pDocSh = mpViewShell->GetViewData().GetDocShell();
+        pDocSh = &mpViewShell->GetViewData().GetDocShell();
         ScDocument& rDoc = mpViewShell->GetViewData().GetDocument();
         pNew = std::make_unique<ScFieldEditEngine>(&rDoc, 
rDoc.GetEnginePool(), rDoc.GetEditPool());
     }
@@ -2603,8 +2603,8 @@ void ScPosWnd::DoEnter()
             if ( pViewSh )
             {
                 ScViewData& rViewData = pViewSh->GetViewData();
-                ScDocShell* pDocShell = rViewData.GetDocShell();
-                ScDocument& rDoc = pDocShell->GetDocument();
+                ScDocShell& rDocShell = rViewData.GetDocShell();
+                ScDocument& rDoc = rDocShell.GetDocument();
 
                 ScNameInputType eType = lcl_GetInputType( aText );
                 if ( eType == SC_NAME_INPUT_BAD_NAME || eType == 
SC_NAME_INPUT_BAD_SELECTION )
@@ -2625,7 +2625,7 @@ void ScPosWnd::DoEnter()
                         ScRangeData* pNew = new ScRangeData( rDoc, aText, 
aContent, aCursor );
                         if ( aNewRanges.insert(pNew) )
                         {
-                            pDocShell->GetDocFunc().ModifyRangeNames( 
aNewRanges );
+                            rDocShell.GetDocFunc().ModifyRangeNames( 
aNewRanges );
                             pViewSh->UpdateInputHandler(true);
                         }
                     }
diff --git a/sc/source/ui/app/scmod.cxx b/sc/source/ui/app/scmod.cxx
index bfca56475ba6..a4b22c473b76 100644
--- a/sc/source/ui/app/scmod.cxx
+++ b/sc/source/ui/app/scmod.cxx
@@ -1662,14 +1662,14 @@ bool ScModule::IsModalMode(SfxObjectShell* pDocSh)
             // in LOK case when no ChildWindow for this view was detected -> 
fallback
             ScInputHandler* pHdl = GetInputHdl();
             if ( pHdl )
-                bIsModal = pHdl->IsModalMode(pDocSh);
+                bIsModal = pHdl->IsModalMode(*pDocSh);
         }
     }
     else if (pDocSh)
     {
         ScInputHandler* pHdl = GetInputHdl();
         if ( pHdl )
-            bIsModal = pHdl->IsModalMode(pDocSh);
+            bIsModal = pHdl->IsModalMode(*pDocSh);
     }
 
     return bIsModal;
diff --git a/sc/source/ui/app/seltrans.cxx b/sc/source/ui/app/seltrans.cxx
index 29563cc55d46..5ad42cdb90da 100644
--- a/sc/source/ui/app/seltrans.cxx
+++ b/sc/source/ui/app/seltrans.cxx
@@ -262,11 +262,11 @@ void ScSelectionTransferObj::CreateCellData()
         //  similar to ScViewFunctionSet::BeginDrag
         if ( aNewMark.IsMarked() && !aNewMark.IsMultiMarked() )
         {
-            ScDocShell* pDocSh = rViewData.GetDocShell();
+            ScDocShell& rDocSh = rViewData.GetDocShell();
 
             const ScRange& aSelRange = aNewMark.GetMarkArea();
             ScDocShellRef aDragShellRef;
-            if ( pDocSh->GetDocument().HasOLEObjectsInArea( aSelRange, 
&aNewMark ) )
+            if ( rDocSh.GetDocument().HasOLEObjectsInArea( aSelRange, 
&aNewMark ) )
             {
                 aDragShellRef = new ScDocShell;     // DocShell needs a Ref 
immediately
                 aDragShellRef->DoInitNew();
@@ -284,8 +284,8 @@ void ScSelectionTransferObj::CreateCellData()
             if ( bCopied )
             {
                 TransferableObjectDescriptor aObjDesc;
-                pDocSh->FillTransferableObjectDescriptor( aObjDesc );
-                aObjDesc.maDisplayName = 
pDocSh->GetMedium()->GetURLObject().GetURLNoPass();
+                rDocSh.FillTransferableObjectDescriptor( aObjDesc );
+                aObjDesc.maDisplayName = 
rDocSh.GetMedium()->GetURLObject().GetURLNoPass();
                 // maSize is set in ScTransferObj ctor
 
                 rtl::Reference<ScTransferObj> xTransferObj = new 
ScTransferObj( std::move(pClipDoc), std::move(aObjDesc) );
@@ -295,7 +295,7 @@ void ScSelectionTransferObj::CreateCellData()
 
                 xTransferObj->SetDrawPersist(aDragShellRef); // keep persist 
for ole objects alive
 
-                xTransferObj->SetDragSource( pDocSh, aNewMark );
+                xTransferObj->SetDragSource( &rDocSh, aNewMark );
 
                 mxCellData = std::move(xTransferObj);
             }
@@ -330,14 +330,14 @@ void ScSelectionTransferObj::CreateDrawData()
             ScDrawLayer::SetGlobalDrawPersist(nullptr);
 
             ScViewData& rViewData = pView->GetViewData();
-            ScDocShell* pDocSh = rViewData.GetDocShell();
+            ScDocShell& rDocSh = rViewData.GetDocShell();
 
             TransferableObjectDescriptor aObjDesc;
-            pDocSh->FillTransferableObjectDescriptor( aObjDesc );
-            aObjDesc.maDisplayName = 
pDocSh->GetMedium()->GetURLObject().GetURLNoPass();
+            rDocSh.FillTransferableObjectDescriptor( aObjDesc );
+            aObjDesc.maDisplayName = 
rDocSh.GetMedium()->GetURLObject().GetURLNoPass();
             // maSize is set in ScDrawTransferObj ctor
 
-            rtl::Reference<ScDrawTransferObj> pTransferObj = new 
ScDrawTransferObj( std::move(pModel), pDocSh, std::move(aObjDesc) );
+            rtl::Reference<ScDrawTransferObj> pTransferObj = new 
ScDrawTransferObj( std::move(pModel), &rDocSh, std::move(aObjDesc) );
 
             pTransferObj->SetDrawPersist(aDragShellRef); // keep persist for 
ole objects alive
             pTransferObj->SetDragSource( pDrawView );       // copies selection
diff --git a/sc/source/ui/app/transobj.cxx b/sc/source/ui/app/transobj.cxx
index 9ed376dd7f35..3be74258a202 100644
--- a/sc/source/ui/app/transobj.cxx
+++ b/sc/source/ui/app/transobj.cxx
@@ -100,6 +100,9 @@ void ScTransferObj::GetAreaSize( const ScDocument& rDoc, 
SCTAB nTab1, SCTAB nTab
 void ScTransferObj::PaintToDev( OutputDevice* pDev, ScDocument& rDoc, double 
nPrintFactor,
                                 const ScRange& rBlock )
 {
+    if (rDoc.GetMaxTableNumber() < 0)
+        return;
+
     tools::Rectangle aBound( Point(), pDev->GetOutputSize() );      //! use 
size from clip area?
 
     ScViewData aViewData(rDoc);
diff --git a/sc/source/ui/condformat/condformatdlg.cxx 
b/sc/source/ui/condformat/condformatdlg.cxx
index cc401c0f19cf..1b0a6195f950 100644
--- a/sc/source/ui/condformat/condformatdlg.cxx
+++ b/sc/source/ui/condformat/condformatdlg.cxx
@@ -620,11 +620,11 @@ void ScCondFormatDlg::OkPressed()
         if(pFormat)
         {
             auto& rRangeList = pFormat->GetRange();
-            
mrViewData.GetDocShell()->GetDocFunc().ReplaceConditionalFormat(mnKey,
+            
mrViewData.GetDocShell().GetDocFunc().ReplaceConditionalFormat(mnKey,
                     std::move(pFormat), maPos.Tab(), rRangeList);
         }
         else
-            
mrViewData.GetDocShell()->GetDocFunc().ReplaceConditionalFormat(mnKey,
+            
mrViewData.GetDocShell().GetDocFunc().ReplaceConditionalFormat(mnKey,
                     nullptr, maPos.Tab(), ScRangeList());
     }
     else
diff --git a/sc/source/ui/condformat/condformateasydlg.cxx 
b/sc/source/ui/condformat/condformateasydlg.cxx
index 208ae7f110c5..78bec5d75f58 100644
--- a/sc/source/ui/condformat/condformateasydlg.cxx
+++ b/sc/source/ui/condformat/condformateasydlg.cxx
@@ -270,7 +270,7 @@ IMPL_LINK(ConditionalFormatEasyDialog, ButtonPressed, 
weld::Button&, rButton, vo
             pFormat->AddEntry(pEntry.release());
             pFormat->SetRange(aRange);
             auto& rRangeList = pFormat->GetRange();
-            mrViewData.GetDocShell()->GetDocFunc().ReplaceConditionalFormat(
+            mrViewData.GetDocShell().GetDocFunc().ReplaceConditionalFormat(
                 0, std::move(pFormat), maPosition.Tab(), rRangeList);
         }
         m_xDialog->response(RET_OK);
diff --git a/sc/source/ui/dbgui/PivotLayoutDialog.cxx 
b/sc/source/ui/dbgui/PivotLayoutDialog.cxx
index d91343f925f9..e7ec2f9e5bb4 100644
--- a/sc/source/ui/dbgui/PivotLayoutDialog.cxx
+++ b/sc/source/ui/dbgui/PivotLayoutDialog.cxx
@@ -506,7 +506,7 @@ void ScPivotLayoutDialog::ApplyChanges()
             }
             if (pDPObj)
             {
-                ScDBDocFunc aFunc( *(mrViewData.GetDocShell() ));
+                ScDBDocFunc aFunc( mrViewData.GetDocShell() );
                 aFunc.RemovePivotTable( *pDPObj, true, false);
                 mrViewData.GetView()->CursorPosChanged();
             }
diff --git a/sc/source/ui/dbgui/dbnamdlg.cxx b/sc/source/ui/dbgui/dbnamdlg.cxx
index 3b05ca1b371e..c0d681382688 100644
--- a/sc/source/ui/dbgui/dbnamdlg.cxx
+++ b/sc/source/ui/dbgui/dbnamdlg.cxx
@@ -376,7 +376,7 @@ IMPL_LINK_NOARG(ScDbNameDlg, OkBtnHdl, weld::Button&, void)
     // be created at this point:
     if (!bInvalid)
     {
-        ScDBDocFunc aFunc(*m_rViewData.GetDocShell());
+        ScDBDocFunc aFunc(m_rViewData.GetDocShell());
         aFunc.ModifyAllDBData(aLocalDbCol, aRemoveList);
         response(RET_OK);
     }
diff --git a/sc/source/ui/dbgui/filtdlg.cxx b/sc/source/ui/dbgui/filtdlg.cxx
index 67d8850f8005..d5f6f3d090c6 100644
--- a/sc/source/ui/dbgui/filtdlg.cxx
+++ b/sc/source/ui/dbgui/filtdlg.cxx
@@ -399,7 +399,7 @@ void ScFilterDlg::Init()
 
 void ScFilterDlg::Close()
 {
-    rViewData.GetDocShell()->CancelAutoDBRange();
+    rViewData.GetDocShell().CancelAutoDBRange();
 
     DoClose( ScFilterDlgWrapper::GetChildWindowId() );
 }
diff --git a/sc/source/ui/dbgui/sfiltdlg.cxx b/sc/source/ui/dbgui/sfiltdlg.cxx
index f2b1225e38e8..9df10b9d9003 100644
--- a/sc/source/ui/dbgui/sfiltdlg.cxx
+++ b/sc/source/ui/dbgui/sfiltdlg.cxx
@@ -178,7 +178,7 @@ void ScSpecialFilterDlg::Init( const SfxItemSet& rArgSet )
 
 void ScSpecialFilterDlg::Close()
 {
-    rViewData.GetDocShell()->CancelAutoDBRange();
+    rViewData.GetDocShell().CancelAutoDBRange();
 
     DoClose( ScSpecialFilterDlgWrapper::GetChildWindowId() );
 }
diff --git a/sc/source/ui/dialogs/SparklineDataRangeDialog.cxx 
b/sc/source/ui/dialogs/SparklineDataRangeDialog.cxx
index 607f5624072b..9f59d8aced38 100644
--- a/sc/source/ui/dialogs/SparklineDataRangeDialog.cxx
+++ b/sc/source/ui/dialogs/SparklineDataRangeDialog.cxx
@@ -193,7 +193,7 @@ void SparklineDataRangeDialog::perform()
 {
     ScRangeList aList{ maDataRange };
 
-    auto& rDocFunc = mrViewData.GetDocShell()->GetDocFunc();
+    auto& rDocFunc = mrViewData.GetDocShell().GetDocFunc();
     rDocFunc.ChangeSparkline(mpSparkline, mrViewData.GetTabNo(), aList);
 }
 
diff --git a/sc/source/ui/dialogs/SparklineDialog.cxx 
b/sc/source/ui/dialogs/SparklineDialog.cxx
index ad092b6659e2..5ce8d2f6c3da 100644
--- a/sc/source/ui/dialogs/SparklineDialog.cxx
+++ b/sc/source/ui/dialogs/SparklineDialog.cxx
@@ -534,7 +534,7 @@ void SparklineDialog::perform()
     
maAttributes.setColorFirst(mxColorFirst->GetSelectedEntry().getComplexColor());
     
maAttributes.setColorLast(mxColorLast->GetSelectedEntry().getComplexColor());
 
-    auto& rDocFunc = mrViewData.GetDocShell()->GetDocFunc();
+    auto& rDocFunc = mrViewData.GetDocShell().GetDocFunc();
 
     if (mpSparklineGroup)
     {
diff --git a/sc/source/ui/docshell/docfunc.cxx 
b/sc/source/ui/docshell/docfunc.cxx
index 92b7bcc6c88f..4565fde4b192 100644
--- a/sc/source/ui/docshell/docfunc.cxx
+++ b/sc/source/ui/docshell/docfunc.cxx
@@ -1150,7 +1150,7 @@ bool ScDocFunc::SetFormulaCells( const ScAddress& rPos, 
std::vector<ScFormulaCel
 void ScDocFunc::NotifyInputHandler( const ScAddress& rPos )
 {
     ScTabViewShell* pViewSh = ScTabViewShell::GetActiveViewShell();
-    if ( !(pViewSh && pViewSh->GetViewData().GetDocShell() == &rDocShell) )
+    if ( !(pViewSh && &pViewSh->GetViewData().GetDocShell() == &rDocShell) )
         return;
 
     ScInputHandler* pInputHdl = ScModule::get()->GetInputHdl();
diff --git a/sc/source/ui/docshell/docsh.cxx b/sc/source/ui/docshell/docsh.cxx
index 41eb3364c898..9d6c8aa614c4 100644
--- a/sc/source/ui/docshell/docsh.cxx
+++ b/sc/source/ui/docshell/docsh.cxx
@@ -249,7 +249,7 @@ std::shared_ptr<model::ColorSet> 
ScDocShell::GetThemeColors()
 void ScDocShell::DoEnterHandler()
 {
     ScTabViewShell* pViewSh = ScTabViewShell::GetActiveViewShell();
-    if (pViewSh && pViewSh->GetViewData().GetDocShell() == this)
+    if (pViewSh && &pViewSh->GetViewData().GetDocShell() == this)
         ScModule::get()->InputEnterHandler();
 }
 
diff --git a/sc/source/ui/docshell/docsh3.cxx b/sc/source/ui/docshell/docsh3.cxx
index 701240a7b553..3583984ec4ea 100644
--- a/sc/source/ui/docshell/docsh3.cxx
+++ b/sc/source/ui/docshell/docsh3.cxx
@@ -83,7 +83,7 @@ void ScDocShell::PostEditView( ScEditEngineDefaulter& 
rEditEngine, const ScAddre
         //  Test: only active ViewShell
 
     ScTabViewShell* pViewSh = ScTabViewShell::GetActiveViewShell();
-    if (pViewSh && pViewSh->GetViewData().GetDocShell() == this)
+    if (pViewSh && &pViewSh->GetViewData().GetDocShell() == this)
     {
         ScEditViewHint aHint(rEditEngine, rCursorPos);
         pViewSh->Notify( *this, aHint );
diff --git a/sc/source/ui/docshell/docsh4.cxx b/sc/source/ui/docshell/docsh4.cxx
index 384740d55e66..172419f0421b 100644
--- a/sc/source/ui/docshell/docsh4.cxx
+++ b/sc/source/ui/docshell/docsh4.cxx
@@ -2824,7 +2824,7 @@ ScTabViewShell* ScDocShell::GetBestViewShell( bool 
bOnlyVisible )
 {
     ScTabViewShell* pViewSh = ScTabViewShell::GetActiveViewShell();
     // wrong Doc?
-    if( pViewSh && pViewSh->GetViewData().GetDocShell() != this )
+    if( pViewSh && &pViewSh->GetViewData().GetDocShell() != this )
         pViewSh = nullptr;
     if( !pViewSh )
     {
diff --git a/sc/source/ui/docshell/docsh6.cxx b/sc/source/ui/docshell/docsh6.cxx
index 23c86bcd089c..1a06ccdbf17c 100644
--- a/sc/source/ui/docshell/docsh6.cxx
+++ b/sc/source/ui/docshell/docsh6.cxx
@@ -124,7 +124,7 @@ void ScDocShell::SetVisAreaOrSize( const tools::Rectangle& 
rVisArea )
         ScTabViewShell* pViewSh = ScTabViewShell::GetActiveViewShell();
         if (pViewSh)
         {
-            if (pViewSh->GetViewData().GetDocShell() == this)
+            if (&pViewSh->GetViewData().GetDocShell() == this)
                 pViewSh->UpdateOleZoom();
         }
     }
diff --git a/sc/source/ui/docshell/externalrefmgr.cxx 
b/sc/source/ui/docshell/externalrefmgr.cxx
index 02dac4f20886..d931e01c5d61 100644
--- a/sc/source/ui/docshell/externalrefmgr.cxx
+++ b/sc/source/ui/docshell/externalrefmgr.cxx
@@ -1493,8 +1493,8 @@ void ScExternalRefLink::Closed()
         if (!pViewData)
             return ERROR_GENERAL;
 
-        ScDocShell* pDocShell = pViewData->GetDocShell();
-        ScDocShellModificator aMod(*pDocShell);
+        ScDocShell& rDocShell = pViewData->GetDocShell();
+        ScDocShellModificator aMod(rDocShell);
         pMgr->switchSrcFile(mnFileId, aFile, aFilter);
         aMod.SetDocumentModified();
     }
diff --git a/sc/source/ui/drawfunc/drawsh2.cxx 
b/sc/source/ui/drawfunc/drawsh2.cxx
index 579b55a63f23..4bf32a50815d 100644
--- a/sc/source/ui/drawfunc/drawsh2.cxx
+++ b/sc/source/ui/drawfunc/drawsh2.cxx
@@ -70,7 +70,7 @@ ScDrawShell::ScDrawShell( ScViewData& rData ) :
             pFrame->GetFrame().GetController(),
             vcl::EnumContext::Context::Cell);
     SetPool( &rViewData.GetScDrawView()->GetModel().GetItemPool() );
-    SfxUndoManager* pMgr = rViewData.GetSfxDocShell()->GetUndoManager();
+    SfxUndoManager* pMgr = rViewData.GetSfxDocShell().GetUndoManager();
     SetUndoManager( pMgr );
     if ( !rViewData.GetDocument().IsUndoEnabled() )
     {
diff --git a/sc/source/ui/drawfunc/drawsh5.cxx 
b/sc/source/ui/drawfunc/drawsh5.cxx
index f16dcb308c80..1cdb33ee05fe 100644
--- a/sc/source/ui/drawfunc/drawsh5.cxx
+++ b/sc/source/ui/drawfunc/drawsh5.cxx
@@ -183,7 +183,7 @@ void ScDrawShell::ExecuteHLink( const SfxRequest& rReq )
                                         xPropSet->setPropertyValue( 
sPropLabel, uno::Any(rName) );
                                     }
 
-                                    OUString aTmp = INetURLObject::GetAbsURL( 
rViewData.GetDocShell()->GetMedium()->GetBaseURL(), rURL );
+                                    OUString aTmp = INetURLObject::GetAbsURL( 
rViewData.GetDocShell().GetMedium()->GetBaseURL(), rURL );
                                     xPropSet->setPropertyValue( 
sPropTargetURL, uno::Any(aTmp) );
 
                                     if( !rTarget.isEmpty() && 
xInfo->hasPropertyByName( sPropTargetFrame ) )
@@ -197,7 +197,7 @@ void ScDrawShell::ExecuteHLink( const SfxRequest& rReq )
                                     }
 
                                     //! Undo ???
-                                    
rViewData.GetDocShell()->SetDocumentModified();
+                                    
rViewData.GetDocShell().SetDocumentModified();
                                     bDone = true;
                                 }
                             }
@@ -514,7 +514,7 @@ void ScDrawShell::ExecDrawFunc( SfxRequest& rReq )
                             {
                                 if (nResult == RET_OK)
                                 {
-                                    ScDocShell* pDocSh = 
rViewData.GetDocShell();
+                                    ScDocShell& rDocSh = 
rViewData.GetDocShell();
                                     OUString aNewName = pDlg->GetName();
 
                                     if (aNewName != pSelected->GetName())
@@ -543,8 +543,8 @@ void ScDrawShell::ExecDrawFunc( SfxRequest& rReq )
 
                                             if(!aPersistName.isEmpty())
                                             {
-                                                
pDocSh->GetUndoManager()->AddUndoAction(
-                                                    
std::make_unique<ScUndoRenameObject>(*pDocSh, aPersistName, 
pSelected->GetName(), aNewName));
+                                                
rDocSh.GetUndoManager()->AddUndoAction(
+                                                    
std::make_unique<ScUndoRenameObject>(rDocSh, aPersistName, 
pSelected->GetName(), aNewName));
                                             }
                                         }
 
@@ -553,8 +553,8 @@ void ScDrawShell::ExecDrawFunc( SfxRequest& rReq )
                                     }
 
                                     // ChartListenerCollectionNeedsUpdate is 
needed for Navigator update
-                                    
pDocSh->GetDocument().SetChartListenerCollectionNeedsUpdate( true );
-                                    pDocSh->SetDrawModified();
+                                    
rDocSh.GetDocument().SetChartListenerCollectionNeedsUpdate( true );
+                                    rDocSh.SetDrawModified();
                                 }
                                 pDlg->disposeOnce();
                             }
@@ -589,7 +589,7 @@ void ScDrawShell::ExecDrawFunc( SfxRequest& rReq )
                             {
                                 if (nResult == RET_OK)
                                 {
-                                    ScDocShell* pDocSh = 
rViewData.GetDocShell();
+                                    ScDocShell& rDocSh = 
rViewData.GetDocShell();
 
                                     // handle Title and Description
                                     pSelected->SetTitle(pDlg->GetTitle());
@@ -597,8 +597,8 @@ void ScDrawShell::ExecDrawFunc( SfxRequest& rReq )
                                     
pSelected->SetDecorative(pDlg->IsDecorative());
 
                                     // ChartListenerCollectionNeedsUpdate is 
needed for Navigator update
-                                    
pDocSh->GetDocument().SetChartListenerCollectionNeedsUpdate( true );
-                                    pDocSh->SetDrawModified();
+                                    
rDocSh.GetDocument().SetChartListenerCollectionNeedsUpdate( true );
+                                    rDocSh.SetDrawModified();
                                 }
                                 pDlg->disposeOnce();
                             }
diff --git a/sc/source/ui/drawfunc/drtxtob.cxx 
b/sc/source/ui/drawfunc/drtxtob.cxx
index 407a5caf9501..ba159f9c40f2 100644
--- a/sc/source/ui/drawfunc/drtxtob.cxx
+++ b/sc/source/ui/drawfunc/drtxtob.cxx
@@ -119,7 +119,7 @@ ScDrawTextObjectBar::ScDrawTextObjectBar(ScViewData& rData) 
:
     SetPool( mrViewData.GetScDrawView()->GetDefaultAttr().GetPool() );
 
     //  At the switching-over the UndoManager is changed to edit mode
-    SfxUndoManager* pMgr = mrViewData.GetSfxDocShell()->GetUndoManager();
+    SfxUndoManager* pMgr = mrViewData.GetSfxDocShell().GetUndoManager();
     SetUndoManager( pMgr );
     if ( !mrViewData.GetDocument().IsUndoEnabled() )
     {
@@ -924,7 +924,7 @@ void ScDrawTextObjectBar::ExecuteAttr( SfxRequest &rReq )
             {
                 const sal_uInt16 nEEWhich = 
GetPool().GetWhichIDFromSlotID(nSlot);
                 const std::optional<NamedColor> oColor
-                    = mrViewData.GetDocShell()->GetRecentColor(nSlot);
+                    = mrViewData.GetDocShell().GetRecentColor(nSlot);
                 if (oColor.has_value())
                 {
                     const model::ComplexColor aCol = 
(*oColor).getComplexColor();
diff --git a/sc/source/ui/drawfunc/drtxtob1.cxx 
b/sc/source/ui/drawfunc/drtxtob1.cxx
index 69cab28cfc6f..4f5727918d44 100644
--- a/sc/source/ui/drawfunc/drtxtob1.cxx
+++ b/sc/source/ui/drawfunc/drtxtob1.cxx
@@ -43,7 +43,7 @@ bool ScDrawTextObjectBar::ExecuteCharDlg( const SfxItemSet& 
rArgs,
     ScAbstractDialogFactory* pFact = ScAbstractDialogFactory::Create();
     ScopedVclPtr<SfxAbstractTabDialog> pDlg(pFact->CreateScCharDlg(
         mrViewData.GetDialogParent(), &rArgs,
-        mrViewData.GetSfxDocShell(), true));
+        &mrViewData.GetSfxDocShell(), true));
     if (nSlot == SID_CHAR_DLG_EFFECT)
     {
         pDlg->SetCurPageId(u"fonteffects"_ustr);
diff --git a/sc/source/ui/drawfunc/fudraw.cxx b/sc/source/ui/drawfunc/fudraw.cxx
index f7c92e396474..4da9a3db26cd 100644
--- a/sc/source/ui/drawfunc/fudraw.cxx
+++ b/sc/source/ui/drawfunc/fudraw.cxx
@@ -434,7 +434,7 @@ bool FuDraw::KeyInput(const KeyEvent& rKEvt)
                     nY = 0;
                 }
 
-                bool bReadOnly = rViewData.GetDocShell()->IsReadOnly();
+                bool bReadOnly = rViewData.GetDocShell().IsReadOnly();
 
                 if(!rKEvt.GetKeyCode().IsMod1() && !bReadOnly)
                 {
diff --git a/sc/source/ui/drawfunc/fuins2.cxx b/sc/source/ui/drawfunc/fuins2.cxx
index ece6a658ad6c..b0143dab0a53 100644
--- a/sc/source/ui/drawfunc/fuins2.cxx
+++ b/sc/source/ui/drawfunc/fuins2.cxx
@@ -83,8 +83,8 @@ namespace
 void lcl_ChartInit(const uno::Reference <embed::XEmbeddedObject>& xObj, 
ScViewData* pViewData,
                    const OUString& rRangeParam, bool bRangeIsPivotTable)
 {
-    ScDocShell* pDocShell = pViewData->GetDocShell();
-    ScDocument& rScDoc = pDocShell->GetDocument();
+    ScDocShell& rDocShell = pViewData->GetDocShell();
+    ScDocument& rScDoc = rDocShell.GetDocument();
 
     OUString aRangeString(rRangeParam);
 
@@ -142,7 +142,7 @@ void lcl_ChartInit(const uno::Reference 
<embed::XEmbeddedObject>& xObj, ScViewDa
 
     xReceiver->attachDataProvider(xDataProvider);
 
-    uno::Reference< util::XNumberFormatsSupplier > xNumberFormatsSupplier( 
getXWeak(pDocShell->GetModel()), uno::UNO_QUERY );
+    uno::Reference< util::XNumberFormatsSupplier > xNumberFormatsSupplier( 
getXWeak(rDocShell.GetModel()), uno::UNO_QUERY );
     xReceiver->attachNumberFormatsSupplier( xNumberFormatsSupplier );
 
     // Same behavior as with old chart: Always assume data series in columns
@@ -524,8 +524,8 @@ FuInsertChart::FuInsertChart(ScTabViewShell& rViewSh, 
vcl::Window* pWin, ScDrawV
     }
 
     ScViewData& rData = rViewSh.GetViewData();
-    ScDocShell* pScDocSh = rData.GetDocShell();
-    ScDocument& rScDoc   = pScDocSh->GetDocument();
+    ScDocShell& rScDocSh = rData.GetDocShell();
+    ScDocument& rScDoc   = rScDocSh.GetDocument();
     bool bUndo (rScDoc.IsUndoEnabled());
 
     if( pReqArgs )
@@ -566,14 +566,14 @@ FuInsertChart::FuInsertChart(ScTabViewShell& rViewSh, 
vcl::Window* pWin, ScDrawV
             {
                 if (bUndo)
                 {
-                    pScDocSh->GetUndoManager()->AddUndoAction(
-                        std::make_unique<ScUndoInsertTab>( *pScDocSh, nNewTab,
+                    rScDocSh.GetUndoManager()->AddUndoAction(
+                        std::make_unique<ScUndoInsertTab>( rScDocSh, nNewTab,
                                              true/*bAppend*/, aTabName ) );
                 }
 
-                pScDocSh->Broadcast( ScTablesHint( SC_TAB_INSERTED, nNewTab ) 
);
+                rScDocSh.Broadcast( ScTablesHint( SC_TAB_INSERTED, nNewTab ) );
                 rViewSh.SetTabNo( nNewTab, true );
-                pScDocSh->PostPaintExtras();            //! done afterwards ???
+                rScDocSh.PostPaintExtras();            //! done afterwards ???
             }
             else
             {
diff --git a/sc/source/ui/drawfunc/fusel.cxx b/sc/source/ui/drawfunc/fusel.cxx
index 6bb680cd49a2..6e5375c01a5b 100644
--- a/sc/source/ui/drawfunc/fusel.cxx
+++ b/sc/source/ui/drawfunc/fusel.cxx
@@ -543,8 +543,8 @@ bool FuSelection::MouseButtonUp(const MouseEvent& rMEvt)
 
     if ( bCopy && pPage )
     {
-        ScDocShell* pDocShell = rViewData.GetDocShell();
-        ScModelObj* pModelObj = ( pDocShell ? pDocShell->GetModel() : nullptr 
);
+        ScDocShell& rDocShell = rViewData.GetDocShell();
+        ScModelObj* pModelObj = rDocShell.GetModel();
         if ( pModelObj )
         {
             SCTAB nTab = rViewData.GetTabNo();
diff --git a/sc/source/ui/drawfunc/fusel2.cxx b/sc/source/ui/drawfunc/fusel2.cxx
index cc9a1478ceb4..7dc045256f9b 100644
--- a/sc/source/ui/drawfunc/fusel2.cxx
+++ b/sc/source/ui/drawfunc/fusel2.cxx
@@ -121,8 +121,8 @@ bool FuSelection::IsNoteCaptionClicked( const Point& rPos ) 
const
         const ScViewData& rViewData = rViewShell.GetViewData();
         ScDocument& rDoc = rViewData.GetDocument();
         SCTAB nTab = rViewData.GetTabNo();
-        ScDocShell* pDocSh = rViewData.GetDocShell();
-        bool bProtectDoc =  rDoc.IsTabProtected( nTab ) || (pDocSh && 
pDocSh->IsReadOnly());
+        ScDocShell& rDocSh = rViewData.GetDocShell();
+        bool bProtectDoc =  rDoc.IsTabProtected( nTab ) || rDocSh.IsReadOnly();
 
         // search the last object (on top) in the object list
         SdrObjListIter aIter( pPageView->GetObjList(), 
SdrIterMode::DeepNoGroups, true );
diff --git a/sc/source/ui/drawfunc/futext3.cxx 
b/sc/source/ui/drawfunc/futext3.cxx
index 11154d9bd7ff..1ec00b328dfa 100644
--- a/sc/source/ui/drawfunc/futext3.cxx
+++ b/sc/source/ui/drawfunc/futext3.cxx
@@ -62,8 +62,8 @@ void FuText::StopEditMode()
         OSL_ENSURE( pNote && (pNote->GetCaption() == pObject), 
"FuText::StopEditMode - missing or invalid cell note" );
     }
 
-    ScDocShell* pDocShell = rViewData.GetDocShell();
-    SfxUndoManager* pUndoMgr = rDoc.IsUndoEnabled() ? 
pDocShell->GetUndoManager() : nullptr;
+    ScDocShell& rDocShell = rViewData.GetDocShell();
+    SfxUndoManager* pUndoMgr = rDoc.IsUndoEnabled() ? 
rDocShell.GetUndoManager() : nullptr;
     if (pUndoMgr && !pUndoMgr->GetMaxUndoActionCount()) // tdf#134308 if max 
undo is 0, treat as if no undo
         pUndoMgr = nullptr;
     bool bNewNote = false;
@@ -85,7 +85,7 @@ void FuText::StopEditMode()
 
             // create a "insert note" undo action if needed
             if( bNewNote )
-                pUndoMgr->AddUndoAction( std::make_unique<ScUndoReplaceNote>( 
*pDocShell, aNotePos, pNote->GetNoteData(), true, std::move(pCalcUndo) ) );
+                pUndoMgr->AddUndoAction( std::make_unique<ScUndoReplaceNote>( 
rDocShell, aNotePos, pNote->GetNoteData(), true, std::move(pCalcUndo) ) );
             else
                 pUndoMgr->AddUndoAction( std::move(pCalcUndo) );
         }
@@ -139,7 +139,7 @@ void FuText::StopEditMode()
             // delete note from document (removes caption, but does not delete 
it)
             rDoc.ReleaseNote(aNotePos);
             // create undo action for removed note
-            pUndoMgr->AddUndoAction( std::make_unique<ScUndoReplaceNote>( 
*pDocShell, aNotePos, aNoteData, false, pDrawLayer->GetCalcUndo() ) );
+            pUndoMgr->AddUndoAction( std::make_unique<ScUndoReplaceNote>( 
rDocShell, aNotePos, aNoteData, false, pDrawLayer->GetCalcUndo() ) );
         }
         else
         {
diff --git a/sc/source/ui/formdlg/formula.cxx b/sc/source/ui/formdlg/formula.cxx
index a2cd6b4e568a..f1cf093e36b2 100644
--- a/sc/source/ui/formdlg/formula.cxx
+++ b/sc/source/ui/formdlg/formula.cxx
@@ -123,7 +123,7 @@ ScFormulaDlg::ScFormulaDlg(SfxBindings* pB, SfxChildWindow* 
pCW,
     m_pViewShell->InitFormEditData();                             // create new
     pData = m_pViewShell->GetFormEditData();
     pData->SetInputHandler(pInputHdl);
-    pData->SetDocShell(rViewData.GetDocShell());
+    pData->SetDocShell(&rViewData.GetDocShell());
 
     OSL_ENSURE(pData,"FormEditData not available");
 
diff --git a/sc/source/ui/inc/AnalysisOfVarianceDialog.hxx 
b/sc/source/ui/inc/AnalysisOfVarianceDialog.hxx
index 35c53e72778f..b620db2929ce 100644
--- a/sc/source/ui/inc/AnalysisOfVarianceDialog.hxx
+++ b/sc/source/ui/inc/AnalysisOfVarianceDialog.hxx
@@ -55,7 +55,7 @@ public:
 
 protected:
     virtual TranslateId GetUndoNameId() override;
-    virtual ScRange ApplyOutput(ScDocShell* pDocShell) override;
+    virtual ScRange ApplyOutput(ScDocShell& rDocShell) override;
 };
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/ui/inc/ChiSquareTestDialog.hxx 
b/sc/source/ui/inc/ChiSquareTestDialog.hxx
index 20e7d696c95f..4813ae207327 100644
--- a/sc/source/ui/inc/ChiSquareTestDialog.hxx
+++ b/sc/source/ui/inc/ChiSquareTestDialog.hxx
@@ -25,7 +25,7 @@ public:
 
 protected:
     virtual TranslateId GetUndoNameId() override;
-    virtual ScRange ApplyOutput(ScDocShell* pDocShell) override;
+    virtual ScRange ApplyOutput(ScDocShell& rDocShell) override;
 };
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/ui/inc/DescriptiveStatisticsDialog.hxx 
b/sc/source/ui/inc/DescriptiveStatisticsDialog.hxx
index dd2488450beb..1cf80291fa7d 100644
--- a/sc/source/ui/inc/DescriptiveStatisticsDialog.hxx
+++ b/sc/source/ui/inc/DescriptiveStatisticsDialog.hxx
@@ -25,7 +25,7 @@ public:
 
 protected:
     virtual TranslateId GetUndoNameId() override;
-    virtual ScRange ApplyOutput(ScDocShell* pDocShell) override;
+    virtual ScRange ApplyOutput(ScDocShell& rDocShell) override;
 };
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/ui/inc/ExponentialSmoothingDialog.hxx 
b/sc/source/ui/inc/ExponentialSmoothingDialog.hxx
index 54b877167805..ceb16891a572 100644
--- a/sc/source/ui/inc/ExponentialSmoothingDialog.hxx
+++ b/sc/source/ui/inc/ExponentialSmoothingDialog.hxx
@@ -31,7 +31,7 @@ public:
 
 protected:
     virtual TranslateId GetUndoNameId() override;
-    virtual ScRange ApplyOutput(ScDocShell* pDocShell) override;
+    virtual ScRange ApplyOutput(ScDocShell& rDocShell) override;
 };
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/ui/inc/FTestDialog.hxx b/sc/source/ui/inc/FTestDialog.hxx
index efd477ce08bb..71fb8dcd28b1 100644
--- a/sc/source/ui/inc/FTestDialog.hxx
+++ b/sc/source/ui/inc/FTestDialog.hxx
@@ -25,7 +25,7 @@ public:
 
 protected:
     virtual TranslateId GetUndoNameId() override;
-    virtual ScRange ApplyOutput(ScDocShell* pDocShell) override;
+    virtual ScRange ApplyOutput(ScDocShell& rDocShell) override;
 };
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/ui/inc/FourierAnalysisDialog.hxx 
b/sc/source/ui/inc/FourierAnalysisDialog.hxx
index f839b274e4d8..1427a27c472d 100644
--- a/sc/source/ui/inc/FourierAnalysisDialog.hxx
+++ b/sc/source/ui/inc/FourierAnalysisDialog.hxx
@@ -42,7 +42,7 @@ public:
 
 protected:
     virtual TranslateId GetUndoNameId() override;
-    virtual ScRange ApplyOutput(ScDocShell* pDocShell) override;
+    virtual ScRange ApplyOutput(ScDocShell& rDocShell) override;
     virtual bool InputRangesValid() override;
 
 private:
diff --git a/sc/source/ui/inc/MatrixComparisonGenerator.hxx 
b/sc/source/ui/inc/MatrixComparisonGenerator.hxx
index ab1e51d7a9b4..c38f9dd24b6b 100644
--- a/sc/source/ui/inc/MatrixComparisonGenerator.hxx
+++ b/sc/source/ui/inc/MatrixComparisonGenerator.hxx
@@ -27,7 +27,7 @@ public:
 
 protected:
     virtual TranslateId GetUndoNameId() override;
-    virtual ScRange ApplyOutput(ScDocShell* pDocShell) override;
+    virtual ScRange ApplyOutput(ScDocShell& rDocShell) override;
 
     virtual OUString getLabel() = 0;
     virtual OUString getTemplate() = 0;
diff --git a/sc/source/ui/inc/MovingAverageDialog.hxx 
b/sc/source/ui/inc/MovingAverageDialog.hxx
index 216b67c8b400..08f400b709c7 100644
--- a/sc/source/ui/inc/MovingAverageDialog.hxx
+++ b/sc/source/ui/inc/MovingAverageDialog.hxx
@@ -32,7 +32,7 @@ public:
 
 protected:
     virtual TranslateId GetUndoNameId() override;
-    virtual ScRange ApplyOutput(ScDocShell* pDocShell) override;
+    virtual ScRange ApplyOutput(ScDocShell& rDocShell) override;
 };
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/ui/inc/RegressionDialog.hxx 
b/sc/source/ui/inc/RegressionDialog.hxx
index f3783c36c91d..38f98a1a261d 100644
--- a/sc/source/ui/inc/RegressionDialog.hxx
+++ b/sc/source/ui/inc/RegressionDialog.hxx
@@ -40,7 +40,7 @@ public:
 
 protected:
     virtual TranslateId GetUndoNameId() override;
-    virtual ScRange ApplyOutput(ScDocShell* pDocShell) override;
+    virtual ScRange ApplyOutput(ScDocShell& rDocShell) override;
     virtual bool InputRangesValid() override;
 
 private:
diff --git a/sc/source/ui/inc/SamplingDialog.hxx 
b/sc/source/ui/inc/SamplingDialog.hxx
index e561092a80f1..b9f8c3aa1f36 100644
--- a/sc/source/ui/inc/SamplingDialog.hxx
+++ b/sc/source/ui/inc/SamplingDialog.hxx
@@ -71,9 +71,9 @@ private:
     sal_Int64 GetPopulationSize() const;
     void LimitSampleSizeAndPeriod();
 
-    ScRange PerformRandomSampling(ScDocShell* pDocShell);
-    ScRange PerformRandomSamplingKeepOrder(ScDocShell* pDocShell);
-    ScRange PerformPeriodicSampling(ScDocShell* pDocShell);
+    ScRange PerformRandomSampling(ScDocShell& rDocShell);
+    ScRange PerformRandomSamplingKeepOrder(ScDocShell& rDocShell);
+    ScRange PerformPeriodicSampling(ScDocShell& rDocShell);
 
     DECL_LINK( ButtonClicked, weld::Button&, void );
     DECL_LINK( GetEditFocusHandler, formula::RefEdit&, void );
diff --git a/sc/source/ui/inc/StatisticsInputOutputDialog.hxx 
b/sc/source/ui/inc/StatisticsInputOutputDialog.hxx
index e831772c8451..ca596ae12e8a 100644
--- a/sc/source/ui/inc/StatisticsInputOutputDialog.hxx
+++ b/sc/source/ui/inc/StatisticsInputOutputDialog.hxx
@@ -36,7 +36,7 @@ public:
 protected:
     void CalculateInputAndWriteToOutput();
 
-    virtual ScRange ApplyOutput(ScDocShell* pDocShell) = 0;
+    virtual ScRange ApplyOutput(ScDocShell& rDocShell) = 0;
     virtual TranslateId GetUndoNameId() = 0;
     virtual bool InputRangesValid();
     void ValidateDialogInput();
diff --git a/sc/source/ui/inc/StatisticsTwoVariableDialog.hxx 
b/sc/source/ui/inc/StatisticsTwoVariableDialog.hxx
index 150a57420913..dde63e9c8ced 100644
--- a/sc/source/ui/inc/StatisticsTwoVariableDialog.hxx
+++ b/sc/source/ui/inc/StatisticsTwoVariableDialog.hxx
@@ -35,7 +35,7 @@ public:
 protected:
     void CalculateInputAndWriteToOutput();
 
-    virtual ScRange ApplyOutput(ScDocShell* pDocShell) = 0;
+    virtual ScRange ApplyOutput(ScDocShell& rDocShell) = 0;
     virtual TranslateId GetUndoNameId() = 0;
     virtual bool InputRangesValid();
     void ValidateDialogInput();
diff --git a/sc/source/ui/inc/TTestDialog.hxx b/sc/source/ui/inc/TTestDialog.hxx
index 0ed370dce2d4..234054ec6ba3 100644
--- a/sc/source/ui/inc/TTestDialog.hxx
+++ b/sc/source/ui/inc/TTestDialog.hxx
@@ -25,7 +25,7 @@ public:
 
 protected:
     virtual TranslateId GetUndoNameId() override;
-    virtual ScRange ApplyOutput(ScDocShell* pDocShell) override;
+    virtual ScRange ApplyOutput(ScDocShell& rDocShell) override;
 };
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/ui/inc/TableFillingAndNavigationTools.hxx 
b/sc/source/ui/inc/TableFillingAndNavigationTools.hxx
index 0e678e3f4482..4171d413fb90 100644
--- a/sc/source/ui/inc/TableFillingAndNavigationTools.hxx
+++ b/sc/source/ui/inc/TableFillingAndNavigationTools.hxx
@@ -71,12 +71,12 @@ public:
 
 class AddressWalkerWriter : public AddressWalker
 {
-    ScDocShell*                         mpDocShell;
+    ScDocShell&                         mrDocShell;
     ScDocument&                         mrDocument;
     formula::FormulaGrammar::Grammar    meGrammar;
 
 public:
-    AddressWalkerWriter(const ScAddress& aInitialAddress, ScDocShell* 
pDocShell, ScDocument& rDocument,
+    AddressWalkerWriter(const ScAddress& aInitialAddress, ScDocShell& 
rDocShell, ScDocument& rDocument,
             formula::FormulaGrammar::Grammar eGrammar );
 
     void writeFormula(const OUString& aFormula);
diff --git a/sc/source/ui/inc/ZTestDialog.hxx b/sc/source/ui/inc/ZTestDialog.hxx
index 2476c38f32a7..d0a19c657cc9 100644
--- a/sc/source/ui/inc/ZTestDialog.hxx
+++ b/sc/source/ui/inc/ZTestDialog.hxx
@@ -25,7 +25,7 @@ public:
 
 protected:
     virtual TranslateId GetUndoNameId() override;
-    virtual ScRange ApplyOutput(ScDocShell* pDocShell) override;
+    virtual ScRange ApplyOutput(ScDocShell& rDocShell) override;
 };
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/ui/inc/datastreamdlg.hxx 
b/sc/source/ui/inc/datastreamdlg.hxx
index 5f1f7eec49e8..5d15d4a25cd5 100644
--- a/sc/source/ui/inc/datastreamdlg.hxx
+++ b/sc/source/ui/inc/datastreamdlg.hxx
@@ -23,7 +23,7 @@ class DataStream;
 
 class DataStreamDlg : public weld::GenericDialogController
 {
-    ScDocShell* m_pDocShell;
+    ScDocShell& m_rDocShell;
 
     std::unique_ptr<SvtURLBox> m_xCbUrl;
     std::unique_ptr<weld::Button> m_xBtnBrowse;
@@ -50,7 +50,7 @@ class DataStreamDlg : public weld::GenericDialogController
     ScRange GetStartRange();
 
 public:
-    DataStreamDlg(ScDocShell* pDocShell, weld::Window* pParent);
+    DataStreamDlg(ScDocShell& rDocShell, weld::Window* pParent);
     virtual ~DataStreamDlg() override;
 
     void Init(const DataStream& rStrm);
diff --git a/sc/source/ui/inc/inputhdl.hxx b/sc/source/ui/inc/inputhdl.hxx
index 26d03efd39b6..0f8ff07e31db 100644
--- a/sc/source/ui/inc/inputhdl.hxx
+++ b/sc/source/ui/inc/inputhdl.hxx
@@ -272,7 +272,7 @@ public:
     /// Returns true if there is a partial autocomplete suggestion.
     bool            HasPartialComplete() const              { return 
mbPartialPrefix; };
 
-    bool            IsModalMode( const SfxObjectShell* pDocSh );
+    bool            IsModalMode( const SfxObjectShell& rDocSh );
 
     void            ForgetLastPattern();
 
@@ -291,7 +291,7 @@ public:
 
     void SetDocumentDisposing( bool b );
 
-    static ReferenceMark GetReferenceMark( const ScViewData& rViewData, 
ScDocShell* pDocSh,
+    static ReferenceMark GetReferenceMark( const ScViewData& rViewData, 
ScDocShell& rDocSh,
                                     tools::Long nX1, tools::Long nX2, 
tools::Long nY1, tools::Long nY2,
                                     tools::Long nTab, const Color& rColor );
 
diff --git a/sc/source/ui/inc/namedefdlg.hxx b/sc/source/ui/inc/namedefdlg.hxx
index 63afb02645e3..0007a3322768 100644
--- a/sc/source/ui/inc/namedefdlg.hxx
+++ b/sc/source/ui/inc/namedefdlg.hxx
@@ -23,7 +23,7 @@ class ScNameDefDlg : public ScAnyRefDlgController
 private:
     bool mbUndo; //if true we need to add an undo action after creating a 
range name
     ScDocument& mrDoc;
-    ScDocShell* mpDocShell;
+    ScDocShell& mrDocShell;
 
     ScAddress maCursorPos;
     OUString maStrInfoDefault;
diff --git a/sc/source/ui/inc/optsolver.hxx b/sc/source/ui/inc/optsolver.hxx
index 35570f7d09d4..a26c63b26bff 100644
--- a/sc/source/ui/inc/optsolver.hxx
+++ b/sc/source/ui/inc/optsolver.hxx
@@ -52,7 +52,7 @@ class ScOptSolverDlg : public ScAnyRefDlgController
 {
 public:
     ScOptSolverDlg( SfxBindings* pB, SfxChildWindow* pCW, weld::Window* 
pParent,
-                 ScDocShell* pDocSh, const ScAddress& aCursorPos );
+                 ScDocShell& rDocSh, const ScAddress& aCursorPos );
     virtual ~ScOptSolverDlg() override;
 
     virtual void    SetReference( const ScRange& rRef, ScDocument& rDoc ) 
override;
@@ -64,7 +64,7 @@ private:
     OUString        maInputError;
     OUString        maConditionError;
 
-    ScDocShell*     mpDocShell;
+    ScDocShell&     mrDocShell;
     ScDocument&     mrDoc;
     const SCTAB     mnCurTab;
     bool            mbDlgLostFocus;
diff --git a/sc/source/ui/inc/sharedocdlg.hxx b/sc/source/ui/inc/sharedocdlg.hxx
index ba83b1787b84..967e15e98759 100644
--- a/sc/source/ui/inc/sharedocdlg.hxx
+++ b/sc/source/ui/inc/sharedocdlg.hxx
@@ -32,7 +32,7 @@ private:
     OUString            m_aStrUnknownUser;
     OUString            m_aStrExclusiveAccess;
 
-    ScDocShell*         mpDocShell;
+    ScDocShell&         mrDocShell;
 
     std::unique_ptr<weld::CheckButton> m_xCbShare;
     std::unique_ptr<weld::Label> m_xFtWarning;
diff --git a/sc/source/ui/inc/viewdata.hxx b/sc/source/ui/inc/viewdata.hxx
index ead20715b29b..1dcc8453cf57 100644
--- a/sc/source/ui/inc/viewdata.hxx
+++ b/sc/source/ui/inc/viewdata.hxx
@@ -275,7 +275,7 @@ private:
     ScMarkData          maMarkData;
     ScMarkData          maHighlightData;
     ScViewDataTable*    pThisTab;                   // Data of the displayed 
sheet
-    ScDocShell*         pDocShell;
+    ScDocShell&         mrDocShell;
     ScDocument&         mrDoc;
     ScTabViewShell*     pView;
     std::unique_ptr<EditView> pEditView[4];               // Belongs to the 
window
@@ -353,11 +353,11 @@ public:
     ScViewData( ScDocument& rDoc );
     ~ScViewData() COVERITY_NOEXCEPT_FALSE;
 
-    ScDocShell*     GetDocShell() const     { return pDocShell; }
+    ScDocShell&     GetDocShell() const     { return mrDocShell; }
     ScDocFunc&      GetDocFunc() const;
     SC_DLLPUBLIC ScDBFunc* GetView() const;
     ScTabViewShell* GetViewShell() const    { return pView; }
-    SfxObjectShell* GetSfxDocShell() const  { return pDocShell; }
+    SfxObjectShell& GetSfxDocShell() const  { return mrDocShell; }
     SfxBindings&    GetBindings();          // from ViewShell's ViewFrame
     SC_DLLPUBLIC SfxDispatcher& GetDispatcher();        // from ViewShell's 
ViewFrame
 
diff --git a/sc/source/ui/miscdlgs/acredlin.cxx 
b/sc/source/ui/miscdlgs/acredlin.cxx
index a137609d2f94..e056fdad1569 100644
--- a/sc/source/ui/miscdlgs/acredlin.cxx
+++ b/sc/source/ui/miscdlgs/acredlin.cxx
@@ -865,11 +865,11 @@ IMPL_LINK( ScAcceptChgDlg, RejectHandle, SvxTPView*, 
pRef, void )
             }
             return false;
         });
-        ScDocShell* pDocSh=pViewData->GetDocShell();
-        pDocSh->PostPaintExtras();
-        pDocSh->PostPaintGridAll();
-        pDocSh->GetUndoManager()->Clear();
-        pDocSh->SetDocumentModified();
+        ScDocShell& rDocSh=pViewData->GetDocShell();
+        rDocSh.PostPaintExtras();
+        rDocSh.PostPaintGridAll();
+        rDocSh.GetUndoManager()->Clear();
+        rDocSh.SetDocumentModified();
         ClearView();
         UpdateView();
     }
@@ -905,10 +905,10 @@ IMPL_LINK( ScAcceptChgDlg, AcceptHandle, SvxTPView*, 
pRef, void )
             }
             return false;
         });
-        ScDocShell* pDocSh=pViewData->GetDocShell();
-        pDocSh->PostPaintExtras();
-        pDocSh->PostPaintGridAll();
-        pDocSh->SetDocumentModified();
+        ScDocShell& rDocSh=pViewData->GetDocShell();
+        rDocSh.PostPaintExtras();
+        rDocSh.PostPaintGridAll();
+        rDocSh.SetDocumentModified();
         ClearView();
         UpdateView();
     }
@@ -967,11 +967,11 @@ IMPL_LINK_NOARG(ScAcceptChgDlg, RejectAllHandle, 
SvxTPView*, void)
 
         pViewData->SetTabNo(0);
 
-        ScDocShell* pDocSh=pViewData->GetDocShell();
-        pDocSh->PostPaintExtras();
-        pDocSh->PostPaintGridAll();
-        pDocSh->GetUndoManager()->Clear();
-        pDocSh->SetDocumentModified();
+        ScDocShell& rDocSh=pViewData->GetDocShell();
+        rDocSh.PostPaintExtras();
+        rDocSh.PostPaintGridAll();
+        rDocSh.GetUndoManager()->Clear();
+        rDocSh.SetDocumentModified();
         ClearView();
         UpdateView();
     }
@@ -993,10 +993,10 @@ IMPL_LINK_NOARG(ScAcceptChgDlg, AcceptAllHandle, 
SvxTPView*, void)
         else
             pChanges->AcceptAll();
 
-        ScDocShell* pDocSh=pViewData->GetDocShell();
-        pDocSh->PostPaintExtras();
-        pDocSh->PostPaintGridAll();
-        pDocSh->SetDocumentModified();
+        ScDocShell& rDocSh=pViewData->GetDocShell();
+        rDocSh.PostPaintExtras();
+        rDocSh.PostPaintGridAll();
+        rDocSh.SetDocumentModified();
         ClearView();
         UpdateView();
     }
@@ -1614,7 +1614,7 @@ IMPL_LINK(ScAcceptChgDlg, CommandHdl, const 
CommandEvent&, rCEvt, bool)
                 if (pEntryData)
                 {
                     ScChangeAction* pScChangeAction = 
static_cast<ScChangeAction*>(pEntryData->pData);
-                    
pViewData->GetDocShell()->ExecuteChangeCommentDialog(pScChangeAction, 
m_xDialog.get(), false);
+                    
pViewData->GetDocShell().ExecuteChangeCommentDialog(pScChangeAction, 
m_xDialog.get(), false);
                 }
             }
         }
diff --git a/sc/source/ui/miscdlgs/crnrdlg.cxx 
b/sc/source/ui/miscdlgs/crnrdlg.cxx
index 9020962ff419..d8859a1d717b 100644
--- a/sc/source/ui/miscdlgs/crnrdlg.cxx
+++ b/sc/source/ui/miscdlgs/crnrdlg.cxx
@@ -486,9 +486,9 @@ IMPL_LINK_NOARG(ScColRowNameRangesDlg, OkBtnHdl, 
weld::Button&, void)
     rDoc.GetRowNameRangesRef() = xRowNameRanges;
     // changed ranges need to take effect
     rDoc.CompileColRowNameFormula();
-    ScDocShell* pDocShell = m_rViewData.GetDocShell();
-    pDocShell->PostPaint(ScRange(0, 0, 0, rDoc.MaxCol(), rDoc.MaxRow(), 
MAXTAB), PaintPartFlags::Grid);
-    pDocShell->SetDocumentModified();
+    ScDocShell& rDocShell = m_rViewData.GetDocShell();
+    rDocShell.PostPaint(ScRange(0, 0, 0, rDoc.MaxCol(), rDoc.MaxRow(), 
MAXTAB), PaintPartFlags::Grid);
+    rDocShell.SetDocumentModified();
 
     response(RET_OK);
 }
diff --git a/sc/source/ui/miscdlgs/datafdlg.cxx 
b/sc/source/ui/miscdlgs/datafdlg.cxx
index fb7a95ea1f84..6490cdc3b5b2 100644
--- a/sc/source/ui/miscdlgs/datafdlg.cxx
+++ b/sc/source/ui/miscdlgs/datafdlg.cxx
@@ -230,7 +230,7 @@ IMPL_LINK( ScDataFormDlg, Impl_DataModifyHdl, weld::Entry&, 
rEdit, void)
 IMPL_LINK_NOARG(ScDataFormDlg, Impl_NewHdl, weld::Button&, void)
 {
     ScViewData& rViewData = pTabViewShell->GetViewData();
-    ScDocShell* pDocSh = rViewData.GetDocShell();
+    ScDocShell& rDocSh = rViewData.GetDocShell();
     if ( !pDoc )
         return;
 
@@ -249,8 +249,8 @@ IMPL_LINK_NOARG(ScDataFormDlg, Impl_NewHdl, weld::Button&, 
void)
     }
     SetButtonState();
     FillCtrls();
-    pDocSh->SetDocumentModified();
-    pDocSh->PostPaintGridAll();
+    rDocSh.SetDocumentModified();
+    rDocSh.PostPaintGridAll();
 }
 
 IMPL_LINK_NOARG(ScDataFormDlg, Impl_PrevHdl, weld::Button&, void)
@@ -288,7 +288,7 @@ IMPL_LINK_NOARG(ScDataFormDlg, Impl_RestoreHdl, 
weld::Button&, void)
 IMPL_LINK_NOARG(ScDataFormDlg, Impl_DeleteHdl, weld::Button&, void)
 {
     ScViewData& rViewData = pTabViewShell->GetViewData();
-    ScDocShell* pDocSh = rViewData.GetDocShell();
+    ScDocShell& rDocSh = rViewData.GetDocShell();
     if (!pDoc)
         return;
 
@@ -297,11 +297,11 @@ IMPL_LINK_NOARG(ScDataFormDlg, Impl_DeleteHdl, 
weld::Button&, void)
     nEndRow--;
 
     SetButtonState();
-    pDocSh->GetUndoManager()->Clear();
+    rDocSh.GetUndoManager()->Clear();
 
     FillCtrls();
-    pDocSh->SetDocumentModified();
-    pDocSh->PostPaintGridAll();
+    rDocSh.SetDocumentModified();
+    rDocSh.PostPaintGridAll();
 }
 
 IMPL_LINK_NOARG(ScDataFormDlg, Impl_CloseHdl, weld::Button&, void)
diff --git a/sc/source/ui/miscdlgs/datastreamdlg.cxx 
b/sc/source/ui/miscdlgs/datastreamdlg.cxx
index f56ebc0fb694..dff87e51722f 100644
--- a/sc/source/ui/miscdlgs/datastreamdlg.cxx
+++ b/sc/source/ui/miscdlgs/datastreamdlg.cxx
@@ -17,10 +17,10 @@
 
 namespace sc
 {
-DataStreamDlg::DataStreamDlg(ScDocShell* pDocShell, weld::Window* pParent)
+DataStreamDlg::DataStreamDlg(ScDocShell& rDocShell, weld::Window* pParent)
     : GenericDialogController(pParent, u"modules/scalc/ui/datastreams.ui"_ustr,
                               u"DataStreamDialog"_ustr)
-    , m_pDocShell(pDocShell)
+    , m_rDocShell(rDocShell)
     , m_xCbUrl(new SvtURLBox(m_xBuilder->weld_combo_box(u"url"_ustr)))
     , m_xBtnBrowse(m_xBuilder->weld_button(u"browse"_ustr))
     , m_xRBValuesInLine(m_xBuilder->weld_radio_button(u"valuesinline"_ustr))
@@ -95,7 +95,7 @@ void DataStreamDlg::UpdateEnable()
 ScRange DataStreamDlg::GetStartRange()
 {
     OUString aStr = m_xEdRange->get_text();
-    ScDocument& rDoc = m_pDocShell->GetDocument();
+    ScDocument& rDoc = m_rDocShell.GetDocument();
     ScRange aRange;
     ScRefFlags nRes = aRange.Parse(aStr, rDoc, rDoc.GetAddressConvention());
     if (((nRes & ScRefFlags::VALID) == ScRefFlags::ZERO) || !aRange.IsValid())
@@ -115,7 +115,7 @@ ScRange DataStreamDlg::GetStartRange()
 void DataStreamDlg::Init(const DataStream& rStrm)
 {
     m_xCbUrl->set_entry_text(rStrm.GetURL());
-    ScDocument& rDoc = m_pDocShell->GetDocument();
+    ScDocument& rDoc = m_rDocShell.GetDocument();
 
     ScRange aRange = rStrm.GetRange();
     ScRange aTopRange = aRange;
@@ -166,7 +166,7 @@ void DataStreamDlg::StartStream()
     DataStream::MoveType eMove
         = m_xRBRangeDown->get_active() ? DataStream::RANGE_DOWN : 
DataStream::MOVE_DOWN;
 
-    DataStream* pStream = DataStream::Set(m_pDocShell, rURL, aStartRange, 
nLimit, eMove);
+    DataStream* pStream = DataStream::Set(&m_rDocShell, rURL, aStartRange, 
nLimit, eMove);
     pStream->SetRefreshOnEmptyLine(m_xCBRefreshOnEmpty->get_active());
     DataStream::MakeToolbarVisible();
     pStream->StartImport();
diff --git a/sc/source/ui/miscdlgs/highred.cxx 
b/sc/source/ui/miscdlgs/highred.cxx
index e207c2b5cf79..476a8d62b25e 100644
--- a/sc/source/ui/miscdlgs/highred.cxx
+++ b/sc/source/ui/miscdlgs/highred.cxx
@@ -214,7 +214,7 @@ IMPL_LINK_NOARG(ScHighlightChgDlg, OKBtnHdl, weld::Button&, 
void)
     aChangeViewSet.SetTheRangeList(aLocalRangeList);
     aChangeViewSet.AdjustDateMode( rDoc );
     rDoc.SetChangeViewSettings(aChangeViewSet);
-    m_rViewData.GetDocShell()->PostPaintGridAll();
+    m_rViewData.GetDocShell().PostPaintGridAll();
     response(RET_OK);
 }
 
diff --git a/sc/source/ui/miscdlgs/instbdlg.cxx 
b/sc/source/ui/miscdlgs/instbdlg.cxx
index 3dfa986be326..1945b9ce9470 100644
--- a/sc/source/ui/miscdlgs/instbdlg.cxx
+++ b/sc/source/ui/miscdlgs/instbdlg.cxx
@@ -93,7 +93,7 @@ void ScInsertTableDlg::Init_Impl( bool bFromFile )
         m_xEdName->set_sensitive(false);
     }
 
-    bool bShared = rViewData.GetDocShell() && 
rViewData.GetDocShell()->IsDocShared();
+    bool bShared = rViewData.GetDocShell().IsDocShared();
 
     if ( !bFromFile || bShared )
     {
diff --git a/sc/source/ui/miscdlgs/optsolver.cxx 
b/sc/source/ui/miscdlgs/optsolver.cxx
index cde2774153bc..f4224c3a152e 100644
--- a/sc/source/ui/miscdlgs/optsolver.cxx
+++ b/sc/source/ui/miscdlgs/optsolver.cxx
@@ -138,13 +138,13 @@ IMPL_LINK(ScCursorRefEdit, KeyInputHdl, const KeyEvent&, 
rKEvt, bool)
 }
 
 ScOptSolverDlg::ScOptSolverDlg(SfxBindings* pB, SfxChildWindow* pCW, 
weld::Window* pParent,
-                               ScDocShell* pDocSh, const ScAddress& aCursorPos)
+                               ScDocShell& rDocSh, const ScAddress& aCursorPos)
     : ScAnyRefDlgController(pB, pCW, pParent, 
u"modules/scalc/ui/solverdlg.ui"_ustr, u"SolverDialog"_ustr)
     , maInputError(ScResId(STR_INVALIDINPUT))
     , maConditionError(ScResId(STR_INVALIDCONDITION))
 
-    , mpDocShell(pDocSh)
-    , mrDoc(pDocSh->GetDocument())
+    , mrDocShell(rDocSh)
+    , mrDoc(rDocSh.GetDocument())
     , mnCurTab(aCursorPos.Tab())
     , mbDlgLostFocus(false)
     , nScrollPos(0)
@@ -948,7 +948,7 @@ bool ScOptSolverDlg::CallSolver()       // return true -> 
close dialog after cal
 
     ReadConditions();
 
-    rtl::Reference<ScModelObj> xDocument( mpDocShell->GetModel() );
+    rtl::Reference<ScModelObj> xDocument( mrDocShell.GetModel() );
 
     ScRange aObjRange;
     if ( !ParseRef( aObjRange, m_xEdObjectiveCell->GetText(), false ) )
@@ -1153,15 +1153,15 @@ bool ScOptSolverDlg::CallSolver()       // return true 
-> close dialog after cal
         uno::Sequence<double> aSolution = xSolver->getSolution();
         if ( aSolution.getLength() == nVarCount )
         {
-            mpDocShell->LockPaint();
-            ScDocFunc &rFunc = mpDocShell->GetDocFunc();
+            mrDocShell.LockPaint();
+            ScDocFunc &rFunc = mrDocShell.GetDocFunc();
             for (nVarPos=0; nVarPos<nVarCount; ++nVarPos)
             {
                 ScAddress aCellPos;
                 ScUnoConversion::FillScAddress(aCellPos, aVariables[nVarPos]);
                 rFunc.SetValueCell(aCellPos, aSolution[nVarPos], false);
             }
-            mpDocShell->UnlockPaint();
+            mrDocShell.UnlockPaint();
         }
         //! else error?
 
@@ -1190,15 +1190,15 @@ bool ScOptSolverDlg::CallSolver()       // return true 
-> close dialog after cal
 
     if ( bRestore )         // restore old values
     {
-        mpDocShell->LockPaint();
-        ScDocFunc &rFunc = mpDocShell->GetDocFunc();
+        mrDocShell.LockPaint();
+        ScDocFunc &rFunc = mrDocShell.GetDocFunc();
         for (nVarPos=0; nVarPos<nVarCount; ++nVarPos)
         {
             ScAddress aCellPos;
             ScUnoConversion::FillScAddress( aCellPos, aVariables[nVarPos] );
             rFunc.SetValueCell(aCellPos, aOldValues[nVarPos], false);
         }
-        mpDocShell->UnlockPaint();
+        mrDocShell.UnlockPaint();
     }
 
     // Generate sensitivity report if user wants it
@@ -1236,7 +1236,7 @@ bool ScOptSolverDlg::CallSolver()       // return true -> 
close dialog after cal
             }
 
             // Insert new sheet to the document and start writing the report
-            ScDocFunc &rFunc = mpDocShell->GetDocFunc();
+            ScDocFunc &rFunc = mrDocShell.GetDocFunc();
             rFunc.InsertTable(mnCurTab + 1, sNewTabName, false, false);
             SCTAB nReportTab;
             if (!mrDoc.GetTable(sNewTabName, nReportTab))
@@ -1248,7 +1248,7 @@ bool ScOptSolverDlg::CallSolver()       // return true -> 
close dialog after cal
             // Used to input data in the new sheet
             ScAddress aOutputAddress(0, 0, nReportTab);
             ScAddress::Details mAddressDetails(mrDoc, aOutputAddress);
-            AddressWalkerWriter aOutput(aOutputAddress, mpDocShell, mrDoc,
+            AddressWalkerWriter aOutput(aOutputAddress, mrDocShell, mrDoc,
                                         
formula::FormulaGrammar::mergeToGrammar(formula::FormulaGrammar::GRAM_ENGLISH, 
mAddressDetails.eConv));
             aOutput.writeBoldString(ScResId(STR_SENSITIVITY_TITLE));
             aOutput.newLine();
diff --git a/sc/source/ui/miscdlgs/sharedocdlg.cxx 
b/sc/source/ui/miscdlgs/sharedocdlg.cxx
index 072e83562707..cff5871f6e89 100644
--- a/sc/source/ui/miscdlgs/sharedocdlg.cxx
+++ b/sc/source/ui/miscdlgs/sharedocdlg.cxx
@@ -55,13 +55,11 @@ ScShareDocumentDlg::ScShareDocumentDlg(weld::Window* 
pParent, ScViewData& rViewD
     , m_aStrNoUserData(ScResId(STR_NO_USER_DATA_AVAILABLE))
     , m_aStrUnknownUser(ScResId(STR_UNKNOWN_USER_CONFLICT))
     , m_aStrExclusiveAccess(ScResId(STR_EXCLUSIVE_ACCESS))
-    , mpDocShell(rViewData.GetDocShell())
+    , mrDocShell(rViewData.GetDocShell())
     , m_xCbShare(m_xBuilder->weld_check_button(u"share"_ustr))
     , m_xFtWarning(m_xBuilder->weld_label(u"warning"_ustr))
     , m_xLbUsers(m_xBuilder->weld_tree_view(u"users"_ustr))
 {
-    OSL_ENSURE( mpDocShell, "ScShareDocumentDlg CTOR: mpDocShell is null!" );
-
     std::vector<int> aWidths
     {
         o3tl::narrowing<int>(m_xLbUsers->get_approximate_digit_width() * 25)
@@ -71,7 +69,7 @@ ScShareDocumentDlg::ScShareDocumentDlg(weld::Window* pParent, 
ScViewData& rViewD
     m_xLbUsers->set_size_request(-1, m_xLbUsers->get_height_rows(9));
     m_xLbUsers->connect_size_allocate(LINK(this, ScShareDocumentDlg, 
SizeAllocated));
 
-    bool bIsDocShared = mpDocShell && mpDocShell->IsDocShared();
+    bool bIsDocShared = mrDocShell.IsDocShared();
     m_xCbShare->set_active(bIsDocShared);
     m_xCbShare->connect_toggled( LINK( this, ScShareDocumentDlg, ToggleHandle 
) );
     m_xFtWarning->set_sensitive(bIsDocShared);
@@ -97,16 +95,11 @@ bool ScShareDocumentDlg::IsShareDocumentChecked() const
 
 void ScShareDocumentDlg::UpdateView()
 {
-    if ( !mpDocShell )
-    {
-        return;
-    }
-
-    if ( mpDocShell->IsDocShared() )
+    if ( mrDocShell.IsDocShared() )
     {
         try
         {
-            ::svt::ShareControlFile aControlFile( 
mpDocShell->GetSharedFileURL() );
+            ::svt::ShareControlFile aControlFile( 
mrDocShell.GetSharedFileURL() );
             std::vector<LockFileEntry> aUsersData = 
aControlFile.GetUsersData();
             sal_Int32 nLength = aUsersData.size();
 
@@ -192,7 +185,7 @@ void ScShareDocumentDlg::UpdateView()
         }
         aUser += " " + m_aStrExclusiveAccess;
 
-        uno::Reference<document::XDocumentProperties> xDocProps = 
mpDocShell->GetModel()->getDocumentProperties();
+        uno::Reference<document::XDocumentProperties> xDocProps = 
mrDocShell.GetModel()->getDocumentProperties();
 
         util::DateTime uDT(xDocProps->getModificationDate());
         DateTime aDateTime(uDT);
diff --git a/sc/source/ui/namedlg/namedefdlg.cxx 
b/sc/source/ui/namedlg/namedefdlg.cxx
index 09ed2ebf5304..93edb5b83adb 100644
--- a/sc/source/ui/namedlg/namedefdlg.cxx
+++ b/sc/source/ui/namedlg/namedefdlg.cxx
@@ -30,7 +30,7 @@ ScNameDefDlg::ScNameDefDlg( SfxBindings* pB, SfxChildWindow* 
pCW, weld::Window*
     : ScAnyRefDlgController( pB, pCW, pParent, 
u"modules/scalc/ui/definename.ui"_ustr, u"DefineNameDialog"_ustr)
     , mbUndo( bUndo )
     , mrDoc(rViewData.GetDocument())
-    , mpDocShell ( rViewData.GetDocShell() )
+    , mrDocShell ( rViewData.GetDocShell() )
     , maCursorPos( aCursorPos )
     , maGlobalNameStr  ( ScResId(STR_GLOBAL_SCOPE) )
     , maErrInvalidNameStr( ScResId(STR_ERR_NAME_INVALID))
@@ -267,15 +267,15 @@ void ScNameDefDlg::AddPushed()
 
                 assert( pNewEntry);     // undo of no insertion smells fishy
                 if (pNewEntry)
-                    mpDocShell->GetUndoManager()->AddUndoAction(
-                            std::make_unique<ScUndoAddRangeData>( *mpDocShell, 
pNewEntry, nTab) );
+                    mrDocShell.GetUndoManager()->AddUndoAction(
+                            std::make_unique<ScUndoAddRangeData>( mrDocShell, 
pNewEntry, nTab) );
 
                 // set table stream invalid, otherwise RangeName won't be 
saved if no other
                 // call invalidates the stream
                 if (nTab != -1)
                     mrDoc.SetStreamValid(nTab, false);
                 SfxGetpApp()->Broadcast( SfxHint( SfxHintId::ScAreasChanged ) 
);
-                mpDocShell->SetDocumentModified();
+                mrDocShell.SetDocumentModified();
                 Close();
             }
             else
diff --git a/sc/source/ui/undo/undocell.cxx b/sc/source/ui/undo/undocell.cxx
index 656f7f54512b..688e55239bab 100644
--- a/sc/source/ui/undo/undocell.cxx
+++ b/sc/source/ui/undo/undocell.cxx
@@ -632,7 +632,7 @@ void ScUndoPrintZoom::Repeat(SfxRepeatTarget& rTarget)
     {
         ScTabViewShell& rViewShell = pViewTarget->GetViewShell();
         ScViewData& rViewData = rViewShell.GetViewData();
-        rViewData.GetDocShell()->SetPrintZoom( rViewData.GetTabNo(), 
nNewScale, nNewPages );
+        rViewData.GetDocShell().SetPrintZoom( rViewData.GetTabNo(), nNewScale, 
nNewPages );
     }
 }
 
diff --git a/sc/source/ui/unoobj/dispuno.cxx b/sc/source/ui/unoobj/dispuno.cxx
index 536d271d643f..92ce0ca562a4 100644
--- a/sc/source/ui/unoobj/dispuno.cxx
+++ b/sc/source/ui/unoobj/dispuno.cxx
@@ -211,7 +211,7 @@ void SAL_CALL ScDispatch::dispatch( const util::URL& aURL,
         ScViewData& rViewData = pViewShell->GetViewData();
         ScAddress aPos( rViewData.GetCurX(), rViewData.GetCurY(), 
rViewData.GetTabNo() );
 
-        ScDBDocFunc aFunc( *rViewData.GetDocShell() );
+        ScDBDocFunc aFunc( rViewData.GetDocShell() );
         aFunc.DoImportUno( aPos, aArgs );
         bDone = true;
     }
diff --git a/sc/source/ui/unoobj/docuno.cxx b/sc/source/ui/unoobj/docuno.cxx
index 2afb3f12f0a7..741dac02950a 100644
--- a/sc/source/ui/unoobj/docuno.cxx
+++ b/sc/source/ui/unoobj/docuno.cxx
@@ -1068,8 +1068,8 @@ bool ScModelObj::isMimeTypeSupported()
 
 static void lcl_sendLOKDocumentBackground(const ScViewData* pViewData)
 {
-    ScDocShell* pDocSh = pViewData->GetDocShell();
-    ScDocument& rDoc = pDocSh->GetDocument();
+    ScDocShell& rDocSh = pViewData->GetDocShell();
+    ScDocument& rDoc = rDocSh.GetDocument();
     const SfxPoolItem& 
rItem(rDoc.getCellAttributeHelper().getDefaultCellAttribute().GetItem(ATTR_BACKGROUND));
     const SvxBrushItem& rBackground = static_cast<const SvxBrushItem&>(rItem);
     const Color& rColor = rBackground.GetColor();
diff --git a/sc/source/ui/unoobj/viewuno.cxx b/sc/source/ui/unoobj/viewuno.cxx
index f88710e4355f..f505ae7606d6 100644
--- a/sc/source/ui/unoobj/viewuno.cxx
+++ b/sc/source/ui/unoobj/viewuno.cxx
-e 
... etc. - the rest is truncated

Reply via email to