sw/inc/unocoll.hxx                           |   10 -
 sw/inc/unotxdoc.hxx                          |    3 
 sw/source/core/unocore/unocoll.cxx           |  234 +++++++++------------------
 sw/source/core/unocore/unocontentcontrol.cxx |   21 --
 sw/source/core/unocore/unofield.cxx          |   28 +--
 sw/source/core/unocore/unoidx.cxx            |   31 +--
 sw/source/core/unocore/unoredlines.cxx       |   19 --
 sw/source/core/unocore/unosett.cxx           |    2 
 sw/source/uibase/uno/unotxdoc.cxx            |  214 ++++++++++++------------
 9 files changed, 231 insertions(+), 331 deletions(-)

New commits:
commit b7faeeb19254a9cb696050016be052d776b2ef99
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Wed Dec 27 12:45:20 2023 +0600
Commit:     Xisco Fauli <xiscofa...@libreoffice.org>
CommitDate: Tue Jan 2 12:16:06 2024 +0100

    tdf#158862: check if document is initialized
    
    Change-Id: I5b380c0629a756a5d7f0f06fb95fab996360b821
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/161366
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>
    (cherry picked from commit 4df22b4d3d4ae1ec0c4d32f614bd2cd8ec13143e)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/161321
    Reviewed-by: Xisco Fauli <xiscofa...@libreoffice.org>

diff --git a/sw/inc/unocoll.hxx b/sw/inc/unocoll.hxx
index 90f634ab9cbc..74009f9f2450 100644
--- a/sw/inc/unocoll.hxx
+++ b/sw/inc/unocoll.hxx
@@ -44,19 +44,19 @@ const SvEventDescription* sw_GetSupportedMacroItems();
 class SwUnoCollection
 {
     SwDoc*      m_pDoc;
-    bool    m_bObjectValid;
 
     public:
         SwUnoCollection(SwDoc* p) :
-            m_pDoc(p),
-            m_bObjectValid(true){}
+            m_pDoc(p){}
 
     virtual ~SwUnoCollection() {}
 
     virtual void                Invalidate();
-    bool                        IsValid() const {return m_bObjectValid;}
 
-    SwDoc*          GetDoc() const {return m_pDoc;}
+protected:
+    bool IsValid() const { return m_pDoc; }
+
+    SwDoc& GetDoc() const;
 };
 
 /** entries in this enum are mapped in an array in unocoll.cxx */
diff --git a/sw/inc/unotxdoc.hxx b/sw/inc/unotxdoc.hxx
index 01619df1d382..32ae6c27e452 100644
--- a/sw/inc/unotxdoc.hxx
+++ b/sw/inc/unotxdoc.hxx
@@ -210,6 +210,9 @@ private:
     virtual SdrModel& getSdrModelFromUnoModel() const override;
 
     virtual ~SwXTextDocument() override;
+
+    SwDoc& GetDocOrThrow() const;
+
 public:
     SwXTextDocument(SwDocShell* pShell);
 
diff --git a/sw/source/core/unocore/unocoll.cxx 
b/sw/source/core/unocore/unocoll.cxx
index 1b2c4f3c07d0..d0f33324214b 100644
--- a/sw/source/core/unocore/unocoll.cxx
+++ b/sw/source/core/unocore/unocoll.cxx
@@ -75,6 +75,7 @@
 #include <cppuhelper/implbase.hxx>
 #include <sfx2/event.hxx>
 #include <sal/log.hxx>
+#include <tools/debug.hxx>
 
 using namespace ::com::sun::star;
 using namespace ::com::sun::star::document;
@@ -857,21 +858,19 @@ sal_Int32 SwXTextTables::getCount()
     SolarMutexGuard aGuard;
     sal_Int32 nRet = 0;
     if(IsValid())
-        nRet = 
static_cast<sal_Int32>(GetDoc()->GetTableFrameFormatCount(true));
+        nRet = static_cast<sal_Int32>(GetDoc().GetTableFrameFormatCount(true));
     return nRet;
 }
 
 uno::Any SAL_CALL SwXTextTables::getByIndex(sal_Int32 nInputIndex)
 {
     SolarMutexGuard aGuard;
-    if (!IsValid())
-        throw uno::RuntimeException();
 
     if (nInputIndex < 0)
         throw IndexOutOfBoundsException();
 
     size_t nIndex = static_cast<size_t>(nInputIndex);
-    for(SwTableFormat* pFormat: *GetDoc()->GetTableFrameFormats())
+    for(SwTableFormat* pFormat: *GetDoc().GetTableFrameFormats())
     {
         if(!pFormat->IsUsed())
             continue;
@@ -892,14 +891,12 @@ uno::Any SwXTextTables::getByName(const OUString& 
rItemName)
 {
     SolarMutexGuard aGuard;
     uno::Any aRet;
-    if(!IsValid())
-        throw uno::RuntimeException();
 
-    const size_t nCount = GetDoc()->GetTableFrameFormatCount(true);
+    const size_t nCount = GetDoc().GetTableFrameFormatCount(true);
     uno::Reference< XTextTable >  xTable;
     for( size_t i = 0; i < nCount; ++i)
     {
-        SwFrameFormat& rFormat = GetDoc()->GetTableFrameFormat(i, true);
+        SwFrameFormat& rFormat = GetDoc().GetTableFrameFormat(i, true);
         if (rItemName == rFormat.GetName())
         {
             xTable = SwXTextTables::GetObject(rFormat);
@@ -916,16 +913,14 @@ uno::Any SwXTextTables::getByName(const OUString& 
rItemName)
 uno::Sequence< OUString > SwXTextTables::getElementNames()
 {
     SolarMutexGuard aGuard;
-    if(!IsValid())
-        throw uno::RuntimeException();
-    const size_t nCount = GetDoc()->GetTableFrameFormatCount(true);
+    const size_t nCount = GetDoc().GetTableFrameFormatCount(true);
     uno::Sequence<OUString> aSeq(static_cast<sal_Int32>(nCount));
     if(nCount)
     {
         OUString* pArray = aSeq.getArray();
         for( size_t i = 0; i < nCount; ++i)
         {
-            SwFrameFormat& rFormat = GetDoc()->GetTableFrameFormat(i, true);
+            SwFrameFormat& rFormat = GetDoc().GetTableFrameFormat(i, true);
 
             pArray[i] = rFormat.GetName();
         }
@@ -937,13 +932,11 @@ sal_Bool SwXTextTables::hasByName(const OUString& rName)
 {
     SolarMutexGuard aGuard;
     bool bRet= false;
-    if(!IsValid())
-        throw uno::RuntimeException();
 
-    const size_t nCount = GetDoc()->GetTableFrameFormatCount(true);
+    const size_t nCount = GetDoc().GetTableFrameFormatCount(true);
     for( size_t i = 0; i < nCount; ++i)
     {
-        SwFrameFormat& rFormat = GetDoc()->GetTableFrameFormat(i, true);
+        SwFrameFormat& rFormat = GetDoc().GetTableFrameFormat(i, true);
         if (rName == rFormat.GetName())
         {
             bRet = true;
@@ -962,9 +955,7 @@ uno::Type SAL_CALL
 sal_Bool SwXTextTables::hasElements()
 {
     SolarMutexGuard aGuard;
-    if(!IsValid())
-        throw uno::RuntimeException();
-    return 0 != GetDoc()->GetTableFrameFormatCount(true);
+    return 0 != GetDoc().GetTableFrameFormatCount(true);
 }
 
 OUString SwXTextTables::getImplementationName()
@@ -1153,19 +1144,17 @@ SwXFrames::~SwXFrames()
 uno::Reference<container::XEnumeration> SwXFrames::createEnumeration()
 {
     SolarMutexGuard aGuard;
-    if(!IsValid())
-        throw uno::RuntimeException();
     switch(m_eType)
     {
         case FLYCNTTYPE_FRM:
             return uno::Reference< container::XEnumeration >(
-                new SwXFrameEnumeration<FLYCNTTYPE_FRM>(*GetDoc()));
+                new SwXFrameEnumeration<FLYCNTTYPE_FRM>(GetDoc()));
         case FLYCNTTYPE_GRF:
             return uno::Reference< container::XEnumeration >(
-                new SwXFrameEnumeration<FLYCNTTYPE_GRF>(*GetDoc()));
+                new SwXFrameEnumeration<FLYCNTTYPE_GRF>(GetDoc()));
         case FLYCNTTYPE_OLE:
             return uno::Reference< container::XEnumeration >(
-                new SwXFrameEnumeration<FLYCNTTYPE_OLE>(*GetDoc()));
+                new SwXFrameEnumeration<FLYCNTTYPE_OLE>(GetDoc()));
         default:
             throw uno::RuntimeException();
     }
@@ -1174,21 +1163,17 @@ uno::Reference<container::XEnumeration> 
SwXFrames::createEnumeration()
 sal_Int32 SwXFrames::getCount()
 {
     SolarMutexGuard aGuard;
-    if(!IsValid())
-        throw uno::RuntimeException();
     // Ignore TextBoxes for TextFrames.
-    return static_cast<sal_Int32>(GetDoc()->GetFlyCount(m_eType, 
/*bIgnoreTextBoxes=*/m_eType == FLYCNTTYPE_FRM));
+    return static_cast<sal_Int32>(GetDoc().GetFlyCount(m_eType, 
/*bIgnoreTextBoxes=*/m_eType == FLYCNTTYPE_FRM));
 }
 
 uno::Any SwXFrames::getByIndex(sal_Int32 nIndex)
 {
     SolarMutexGuard aGuard;
-    if(!IsValid())
-        throw uno::RuntimeException();
     if(nIndex < 0)
         throw IndexOutOfBoundsException();
     // Ignore TextBoxes for TextFrames.
-    SwFrameFormat* pFormat = GetDoc()->GetFlyNum(static_cast<size_t>(nIndex), 
m_eType, /*bIgnoreTextBoxes=*/m_eType == FLYCNTTYPE_FRM);
+    SwFrameFormat* pFormat = GetDoc().GetFlyNum(static_cast<size_t>(nIndex), 
m_eType, /*bIgnoreTextBoxes=*/m_eType == FLYCNTTYPE_FRM);
     if(!pFormat)
         throw IndexOutOfBoundsException();
     return lcl_UnoWrapFrame(pFormat, m_eType);
@@ -1197,19 +1182,17 @@ uno::Any SwXFrames::getByIndex(sal_Int32 nIndex)
 uno::Any SwXFrames::getByName(const OUString& rName)
 {
     SolarMutexGuard aGuard;
-    if(!IsValid())
-        throw uno::RuntimeException();
     const SwFrameFormat* pFormat;
     switch(m_eType)
     {
         case FLYCNTTYPE_GRF:
-            pFormat = GetDoc()->FindFlyByName(rName, SwNodeType::Grf);
+            pFormat = GetDoc().FindFlyByName(rName, SwNodeType::Grf);
             break;
         case FLYCNTTYPE_OLE:
-            pFormat = GetDoc()->FindFlyByName(rName, SwNodeType::Ole);
+            pFormat = GetDoc().FindFlyByName(rName, SwNodeType::Ole);
             break;
         default:
-            pFormat = GetDoc()->FindFlyByName(rName, SwNodeType::Text);
+            pFormat = GetDoc().FindFlyByName(rName, SwNodeType::Text);
             break;
     }
     if(!pFormat)
@@ -1220,8 +1203,6 @@ uno::Any SwXFrames::getByName(const OUString& rName)
 uno::Sequence<OUString> SwXFrames::getElementNames()
 {
     SolarMutexGuard aGuard;
-    if(!IsValid())
-        throw uno::RuntimeException();
     const Reference<XEnumeration> xEnum = createEnumeration();
     std::vector<OUString> vNames;
     while(xEnum->hasMoreElements())
@@ -1237,16 +1218,14 @@ uno::Sequence<OUString> SwXFrames::getElementNames()
 sal_Bool SwXFrames::hasByName(const OUString& rName)
 {
     SolarMutexGuard aGuard;
-    if(!IsValid())
-        throw uno::RuntimeException();
     switch(m_eType)
     {
         case FLYCNTTYPE_GRF:
-            return GetDoc()->FindFlyByName(rName, SwNodeType::Grf) != nullptr;
+            return GetDoc().FindFlyByName(rName, SwNodeType::Grf) != nullptr;
         case FLYCNTTYPE_OLE:
-            return GetDoc()->FindFlyByName(rName, SwNodeType::Ole) != nullptr;
+            return GetDoc().FindFlyByName(rName, SwNodeType::Ole) != nullptr;
         default:
-            return GetDoc()->FindFlyByName(rName, SwNodeType::Text) != nullptr;
+            return GetDoc().FindFlyByName(rName, SwNodeType::Text) != nullptr;
     }
 }
 
@@ -1269,9 +1248,7 @@ uno::Type SAL_CALL SwXFrames::getElementType()
 sal_Bool SwXFrames::hasElements()
 {
     SolarMutexGuard aGuard;
-    if(!IsValid())
-        throw uno::RuntimeException();
-    return GetDoc()->GetFlyCount(m_eType) > 0;
+    return GetDoc().GetFlyCount(m_eType) > 0;
 }
 
 
@@ -1374,9 +1351,7 @@ SwXTextSections::~SwXTextSections()
 sal_Int32 SwXTextSections::getCount()
 {
     SolarMutexGuard aGuard;
-    if(!IsValid())
-        throw uno::RuntimeException();
-    const SwSectionFormats& rSectFormats = GetDoc()->GetSections();
+    const SwSectionFormats& rSectFormats = GetDoc().GetSections();
     size_t nCount = rSectFormats.size();
     for(size_t i = nCount; i; --i)
     {
@@ -1388,41 +1363,29 @@ sal_Int32 SwXTextSections::getCount()
 
 uno::Any SwXTextSections::getByIndex(sal_Int32 nIndex)
 {
+    if (nIndex < 0)
+        throw IndexOutOfBoundsException();
+    size_t nIndex2 = nIndex;
     SolarMutexGuard aGuard;
-    uno::Reference< XTextSection >  xRet;
-    if(!IsValid())
-        throw uno::RuntimeException();
-
-    SwSectionFormats& rFormats = GetDoc()->GetSections();
 
-    const SwSectionFormats& rSectFormats = GetDoc()->GetSections();
+    const SwSectionFormats& rSectFormats = GetDoc().GetSections();
     const size_t nCount = rSectFormats.size();
     for(size_t i = 0; i < nCount; ++i)
     {
         if( !rSectFormats[i]->IsInNodesArr())
-            nIndex ++;
-        else if(static_cast<size_t>(nIndex) == i)
-            break;
-        if(static_cast<size_t>(nIndex) == i)
-            break;
+            nIndex2++;
+        else if (nIndex2 == i)
+            return Any(GetObject(*rSectFormats[i]));
     }
-    if(nIndex < 0 || o3tl::make_unsigned(nIndex) >= rFormats.size())
-        throw IndexOutOfBoundsException();
-
-    SwSectionFormat* pFormat = rFormats[nIndex];
-    xRet = GetObject(*pFormat);
-
-    return Any(xRet);
+    throw IndexOutOfBoundsException();
 }
 
 uno::Any SwXTextSections::getByName(const OUString& rName)
 {
     SolarMutexGuard aGuard;
     uno::Any aRet;
-    if(!IsValid())
-        throw uno::RuntimeException();
 
-    SwSectionFormats& rFormats = GetDoc()->GetSections();
+    SwSectionFormats& rFormats = GetDoc().GetSections();
     uno::Reference< XTextSection >  xSect;
     for(size_t i = 0; i < rFormats.size(); ++i)
     {
@@ -1444,10 +1407,8 @@ uno::Any SwXTextSections::getByName(const OUString& 
rName)
 uno::Sequence< OUString > SwXTextSections::getElementNames()
 {
     SolarMutexGuard aGuard;
-    if(!IsValid())
-        throw uno::RuntimeException();
-    size_t nCount = GetDoc()->GetSections().size();
-    SwSectionFormats& rSectFormats = GetDoc()->GetSections();
+    SwSectionFormats& rSectFormats = GetDoc().GetSections();
+    size_t nCount = rSectFormats.size();
     for(size_t i = nCount; i; --i)
     {
         if( !rSectFormats[i - 1]->IsInNodesArr())
@@ -1457,15 +1418,14 @@ uno::Sequence< OUString > 
SwXTextSections::getElementNames()
     uno::Sequence<OUString> aSeq(nCount);
     if(nCount)
     {
-        SwSectionFormats& rFormats = GetDoc()->GetSections();
         OUString* pArray = aSeq.getArray();
         size_t nIndex = 0;
         for( size_t i = 0; i < nCount; ++i, ++nIndex)
         {
-            const SwSectionFormat* pFormat = rFormats[nIndex];
+            const SwSectionFormat* pFormat = rSectFormats[nIndex];
             while(!pFormat->IsInNodesArr())
             {
-                pFormat = rFormats[++nIndex];
+                pFormat = rSectFormats[++nIndex];
             }
             pArray[i] = pFormat->GetSection()->GetSectionName();
         }
@@ -1479,7 +1439,7 @@ sal_Bool SwXTextSections::hasByName(const OUString& rName)
     bool bRet = false;
     if(IsValid())
     {
-        SwSectionFormats& rFormats = GetDoc()->GetSections();
+        SwSectionFormats& rFormats = GetDoc().GetSections();
         for(size_t i = 0; i < rFormats.size(); ++i)
         {
             const SwSectionFormat* pFormat = rFormats[i];
@@ -1508,10 +1468,8 @@ sal_Bool SwXTextSections::hasElements()
 {
     SolarMutexGuard aGuard;
     size_t nCount = 0;
-    if(!IsValid())
-        throw uno::RuntimeException();
 
-    SwSectionFormats& rFormats = GetDoc()->GetSections();
+    SwSectionFormats& rFormats = GetDoc().GetSections();
     nCount = rFormats.size();
 
     return nCount > 0;
@@ -1547,11 +1505,9 @@ SwXBookmarks::~SwXBookmarks()
 sal_Int32 SwXBookmarks::getCount()
 {
     SolarMutexGuard aGuard;
-    if(!IsValid())
-        throw uno::RuntimeException();
 
     sal_Int32 count(0);
-    IDocumentMarkAccess* const pMarkAccess = 
GetDoc()->getIDocumentMarkAccess();
+    IDocumentMarkAccess* const pMarkAccess = GetDoc().getIDocumentMarkAccess();
     for (IDocumentMarkAccess::const_iterator_t ppMark =
             pMarkAccess->getBookmarksBegin();
          ppMark != pMarkAccess->getBookmarksEnd(); ++ppMark)
@@ -1568,9 +1524,8 @@ sal_Int32 SwXBookmarks::getCount()
 uno::Any SwXBookmarks::getByIndex(sal_Int32 nIndex)
 {
     SolarMutexGuard aGuard;
-    if(!IsValid())
-        throw uno::RuntimeException();
-    IDocumentMarkAccess* const pMarkAccess = 
GetDoc()->getIDocumentMarkAccess();
+    auto& rDoc = GetDoc();
+    IDocumentMarkAccess* const pMarkAccess = rDoc.getIDocumentMarkAccess();
     if(nIndex < 0 || nIndex >= pMarkAccess->getBookmarksCount())
         throw IndexOutOfBoundsException();
 
@@ -1586,7 +1541,7 @@ uno::Any SwXBookmarks::getByIndex(sal_Int32 nIndex)
             {
                 uno::Any aRet;
                 const uno::Reference< text::XTextContent > xRef =
-                    SwXBookmark::CreateXBookmark(*GetDoc(), *ppMark);
+                    SwXBookmark::CreateXBookmark(rDoc, *ppMark);
                 aRet <<= xRef;
                 return aRet;
             }
@@ -1599,17 +1554,16 @@ uno::Any SwXBookmarks::getByIndex(sal_Int32 nIndex)
 uno::Any SwXBookmarks::getByName(const OUString& rName)
 {
     SolarMutexGuard aGuard;
-    if(!IsValid())
-        throw uno::RuntimeException();
 
-    IDocumentMarkAccess* const pMarkAccess = 
GetDoc()->getIDocumentMarkAccess();
+    auto& rDoc = GetDoc();
+    IDocumentMarkAccess* const pMarkAccess = rDoc.getIDocumentMarkAccess();
     IDocumentMarkAccess::const_iterator_t ppBkmk = 
pMarkAccess->findBookmark(rName);
     if(ppBkmk == pMarkAccess->getBookmarksEnd())
         throw NoSuchElementException();
 
     uno::Any aRet;
     const uno::Reference< text::XTextContent > xRef =
-        SwXBookmark::CreateXBookmark(*GetDoc(), *ppBkmk);
+        SwXBookmark::CreateXBookmark(rDoc, *ppBkmk);
     aRet <<= xRef;
     return aRet;
 }
@@ -1617,11 +1571,9 @@ uno::Any SwXBookmarks::getByName(const OUString& rName)
 uno::Sequence< OUString > SwXBookmarks::getElementNames()
 {
     SolarMutexGuard aGuard;
-    if(!IsValid())
-        throw uno::RuntimeException();
 
     std::vector< OUString > ret;
-    IDocumentMarkAccess* const pMarkAccess = 
GetDoc()->getIDocumentMarkAccess();
+    IDocumentMarkAccess* const pMarkAccess = GetDoc().getIDocumentMarkAccess();
     for (IDocumentMarkAccess::const_iterator_t ppMark =
             pMarkAccess->getBookmarksBegin();
          ppMark != pMarkAccess->getBookmarksEnd(); ++ppMark)
@@ -1638,10 +1590,8 @@ uno::Sequence< OUString > SwXBookmarks::getElementNames()
 sal_Bool SwXBookmarks::hasByName(const OUString& rName)
 {
     SolarMutexGuard aGuard;
-    if(!IsValid())
-        throw uno::RuntimeException();
 
-    IDocumentMarkAccess* const pMarkAccess = 
GetDoc()->getIDocumentMarkAccess();
+    IDocumentMarkAccess* const pMarkAccess = GetDoc().getIDocumentMarkAccess();
     return pMarkAccess->findBookmark(rName) != pMarkAccess->getBookmarksEnd();
 }
 
@@ -1653,10 +1603,8 @@ uno::Type SAL_CALL SwXBookmarks::getElementType()
 sal_Bool SwXBookmarks::hasElements()
 {
     SolarMutexGuard aGuard;
-    if(!IsValid())
-        throw uno::RuntimeException();
 
-    IDocumentMarkAccess* const pMarkAccess = 
GetDoc()->getIDocumentMarkAccess();
+    IDocumentMarkAccess* const pMarkAccess = GetDoc().getIDocumentMarkAccess();
     for (IDocumentMarkAccess::const_iterator_t ppMark =
             pMarkAccess->getBookmarksBegin();
          ppMark != pMarkAccess->getBookmarksEnd(); ++ppMark)
@@ -1682,29 +1630,24 @@ 
SwXNumberingRulesCollection::~SwXNumberingRulesCollection()
 sal_Int32 SwXNumberingRulesCollection::getCount()
 {
     SolarMutexGuard aGuard;
-    if(!IsValid())
-        throw uno::RuntimeException();
-    return GetDoc()->GetNumRuleTable().size();
+    return GetDoc().GetNumRuleTable().size();
 }
 
 uno::Any SwXNumberingRulesCollection::getByIndex(sal_Int32 nIndex)
 {
+    if (nIndex < 0)
+        throw IndexOutOfBoundsException();
+
     SolarMutexGuard aGuard;
-    uno::Any aRet;
-    if(!IsValid())
-        throw uno::RuntimeException();
 
-    uno::Reference< XIndexReplace >  xRef;
-    if ( o3tl::make_unsigned(nIndex) < GetDoc()->GetNumRuleTable().size() )
+    if (SwDoc& rDoc = GetDoc(); o3tl::make_unsigned(nIndex) < 
rDoc.GetNumRuleTable().size())
     {
-        xRef = new SwXNumberingRules( *GetDoc()->GetNumRuleTable()[ nIndex ], 
GetDoc());
-        aRet <<= xRef;
+        uno::Reference<XIndexReplace> xRef(
+            new SwXNumberingRules(*rDoc.GetNumRuleTable()[nIndex], &rDoc));
+        return uno::Any(xRef);
     }
 
-    if(!xRef.is())
-        throw IndexOutOfBoundsException();
-
-    return aRet;
+    throw IndexOutOfBoundsException();
 }
 
 uno::Type SAL_CALL SwXNumberingRulesCollection::getElementType()
@@ -1715,9 +1658,7 @@ uno::Type SAL_CALL 
SwXNumberingRulesCollection::getElementType()
 sal_Bool SwXNumberingRulesCollection::hasElements()
 {
     SolarMutexGuard aGuard;
-    if(!IsValid())
-        throw uno::RuntimeException();
-    return !GetDoc()->GetNumRuleTable().empty();
+    return !GetDoc().GetNumRuleTable().empty();
 }
 
 OUString SwXFootnotes::getImplementationName()
@@ -1748,14 +1689,13 @@ SwXFootnotes::~SwXFootnotes()
 sal_Int32 SwXFootnotes::getCount()
 {
     SolarMutexGuard aGuard;
-    if(!IsValid())
-        throw uno::RuntimeException();
     sal_Int32 nCount = 0;
-    const size_t nFootnoteCnt = GetDoc()->GetFootnoteIdxs().size();
+    auto& rIdxs = GetDoc().GetFootnoteIdxs();
+    const size_t nFootnoteCnt = rIdxs.size();
     SwTextFootnote* pTextFootnote;
     for( size_t n = 0; n < nFootnoteCnt; ++n )
     {
-        pTextFootnote = GetDoc()->GetFootnoteIdxs()[ n ];
+        pTextFootnote = rIdxs[n];
         const SwFormatFootnote& rFootnote = pTextFootnote->GetFootnote();
         if ( rFootnote.IsEndNote() != m_bEndnote )
             continue;
@@ -1769,22 +1709,22 @@ uno::Any SwXFootnotes::getByIndex(sal_Int32 nIndex)
     SolarMutexGuard aGuard;
     uno::Any aRet;
     sal_Int32 nCount = 0;
-    if(!IsValid())
-        throw uno::RuntimeException();
 
-    const size_t nFootnoteCnt = GetDoc()->GetFootnoteIdxs().size();
+    auto& rDoc = GetDoc();
+    auto& rIdxs = rDoc.GetFootnoteIdxs();
+    const size_t nFootnoteCnt = rIdxs.size();
     SwTextFootnote* pTextFootnote;
     uno::Reference< XFootnote >  xRef;
     for( size_t n = 0; n < nFootnoteCnt; ++n )
     {
-        pTextFootnote = GetDoc()->GetFootnoteIdxs()[ n ];
+        pTextFootnote = rIdxs[n];
         const SwFormatFootnote& rFootnote = pTextFootnote->GetFootnote();
         if ( rFootnote.IsEndNote() != m_bEndnote )
             continue;
 
         if(nCount == nIndex)
         {
-            xRef = SwXFootnote::CreateXFootnote(*GetDoc(),
+            xRef = SwXFootnote::CreateXFootnote(rDoc,
                     &const_cast<SwFormatFootnote&>(rFootnote));
             aRet <<= xRef;
             break;
@@ -1805,9 +1745,7 @@ uno::Type SAL_CALL SwXFootnotes::getElementType()
 sal_Bool SwXFootnotes::hasElements()
 {
     SolarMutexGuard aGuard;
-    if(!IsValid())
-        throw uno::RuntimeException();
-    return !GetDoc()->GetFootnoteIdxs().empty();
+    return !GetDoc().GetFootnoteIdxs().empty();
 }
 
 Reference<XFootnote>    SwXFootnotes::GetObject( SwDoc& rDoc, const 
SwFormatFootnote& rFormat )
@@ -1842,25 +1780,22 @@ SwXReferenceMarks::~SwXReferenceMarks()
 sal_Int32 SwXReferenceMarks::getCount()
 {
     SolarMutexGuard aGuard;
-    if(!IsValid())
-        throw uno::RuntimeException();
-    return GetDoc()->GetRefMarks();
+    return GetDoc().GetRefMarks();
 }
 
 uno::Any SwXReferenceMarks::getByIndex(sal_Int32 nIndex)
 {
     SolarMutexGuard aGuard;
     uno::Any aRet;
-    if(!IsValid())
-        throw uno::RuntimeException();
     uno::Reference< XTextContent >  xRef;
     if(0 <= nIndex && nIndex < SAL_MAX_UINT16)
     {
+        auto& rDoc = GetDoc();
         SwFormatRefMark *const pMark = const_cast<SwFormatRefMark*>(
-                GetDoc()->GetRefMark(o3tl::narrowing<sal_uInt16>(nIndex)));
+                rDoc.GetRefMark(o3tl::narrowing<sal_uInt16>(nIndex)));
         if(pMark)
         {
-            xRef = SwXReferenceMark::CreateXReferenceMark(*GetDoc(), pMark);
+            xRef = SwXReferenceMark::CreateXReferenceMark(rDoc, pMark);
             aRet <<= xRef;
         }
     }
@@ -1873,16 +1808,15 @@ uno::Any SwXReferenceMarks::getByName(const OUString& 
rName)
 {
     SolarMutexGuard aGuard;
     uno::Any aRet;
-    if(!IsValid())
-        throw uno::RuntimeException();
 
+    auto& rDoc = GetDoc();
     SwFormatRefMark *const pMark =
-        const_cast<SwFormatRefMark*>(GetDoc()->GetRefMark(rName));
+        const_cast<SwFormatRefMark*>(rDoc.GetRefMark(rName));
     if(!pMark)
         throw NoSuchElementException();
 
     uno::Reference<XTextContent> const xRef =
-        SwXReferenceMark::CreateXReferenceMark(*GetDoc(), pMark);
+        SwXReferenceMark::CreateXReferenceMark(rDoc, pMark);
     aRet <<= xRef;
 
     return aRet;
@@ -1892,11 +1826,9 @@ uno::Sequence< OUString > 
SwXReferenceMarks::getElementNames()
 {
     SolarMutexGuard aGuard;
     uno::Sequence<OUString> aRet;
-    if(!IsValid())
-        throw uno::RuntimeException();
 
     std::vector<OUString> aStrings;
-    const sal_uInt16 nCount = GetDoc()->GetRefMarks( &aStrings );
+    const sal_uInt16 nCount = GetDoc().GetRefMarks(&aStrings);
     aRet.realloc(nCount);
     OUString* pNames = aRet.getArray();
     for(sal_uInt16 i = 0; i < nCount; i++)
@@ -1908,9 +1840,7 @@ uno::Sequence< OUString > 
SwXReferenceMarks::getElementNames()
 sal_Bool SwXReferenceMarks::hasByName(const OUString& rName)
 {
     SolarMutexGuard aGuard;
-    if(!IsValid())
-        throw uno::RuntimeException();
-    return nullptr != GetDoc()->GetRefMark( rName);
+    return nullptr != GetDoc().GetRefMark( rName);
 }
 
 uno::Type SAL_CALL SwXReferenceMarks::getElementType()
@@ -1921,14 +1851,20 @@ uno::Type SAL_CALL SwXReferenceMarks::getElementType()
 sal_Bool SwXReferenceMarks::hasElements()
 {
     SolarMutexGuard aGuard;
-    if(!IsValid())
+    return 0 != GetDoc().GetRefMarks();
+}
+
+SwDoc& SwUnoCollection::GetDoc() const
+{
+    DBG_TESTSOLARMUTEX();
+    if (!m_pDoc)
         throw uno::RuntimeException();
-    return 0 != GetDoc()->GetRefMarks();
+    return *m_pDoc;
 }
 
 void SwUnoCollection::Invalidate()
 {
-    m_bObjectValid = false;
+    SolarMutexGuard aGuard;
     m_pDoc = nullptr;
 }
 
diff --git a/sw/source/core/unocore/unocontentcontrol.cxx 
b/sw/source/core/unocore/unocontentcontrol.cxx
index fd77b4a77f9e..5d54d60e9b5d 100644
--- a/sw/source/core/unocore/unocontentcontrol.cxx
+++ b/sw/source/core/unocore/unocontentcontrol.cxx
@@ -1424,24 +1424,14 @@ sal_Int32 SwXContentControls::getCount()
 {
     SolarMutexGuard aGuard;
 
-    if (!IsValid())
-    {
-        throw uno::RuntimeException();
-    }
-
-    return GetDoc()->GetContentControlManager().GetCount();
+    return GetDoc().GetContentControlManager().GetCount();
 }
 
 uno::Any SwXContentControls::getByIndex(sal_Int32 nIndex)
 {
     SolarMutexGuard aGuard;
 
-    if (!IsValid())
-    {
-        throw uno::RuntimeException();
-    }
-
-    SwContentControlManager& rManager = GetDoc()->GetContentControlManager();
+    SwContentControlManager& rManager = GetDoc().GetContentControlManager();
     if (nIndex < 0 || o3tl::make_unsigned(nIndex) >= rManager.GetCount())
     {
         throw lang::IndexOutOfBoundsException();
@@ -1462,12 +1452,7 @@ sal_Bool SwXContentControls::hasElements()
 {
     SolarMutexGuard aGuard;
 
-    if (!IsValid())
-    {
-        throw uno::RuntimeException();
-    }
-
-    return !GetDoc()->GetContentControlManager().IsEmpty();
+    return !GetDoc().GetContentControlManager().IsEmpty();
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/source/core/unocore/unofield.cxx 
b/sw/source/core/unocore/unofield.cxx
index 74b530ed5a28..d73c0b59b34e 100644
--- a/sw/source/core/unocore/unofield.cxx
+++ b/sw/source/core/unocore/unofield.cxx
@@ -2727,8 +2727,6 @@ static SwFieldIds lcl_GetIdByName( OUString& rName, 
OUString& rTypeName )
 uno::Any SwXTextFieldMasters::getByName(const OUString& rName)
 {
     SolarMutexGuard aGuard;
-    if(!GetDoc())
-        throw uno::RuntimeException();
 
     OUString sName(rName), sTypeName;
     const SwFieldIds nResId = lcl_GetIdByName( sName, sTypeName );
@@ -2738,14 +2736,15 @@ uno::Any SwXTextFieldMasters::getByName(const OUString& 
rName)
             css::uno::Reference<css::uno::XInterface>());
 
     sName = sName.copy(std::min(sTypeName.getLength()+1, sName.getLength()));
-    SwFieldType* pType = 
GetDoc()->getIDocumentFieldsAccess().GetFieldType(nResId, sName, true);
+    auto& rDoc = GetDoc();
+    SwFieldType* pType = rDoc.getIDocumentFieldsAccess().GetFieldType(nResId, 
sName, true);
     if(!pType)
         throw container::NoSuchElementException(
             "SwXTextFieldMasters::getByName(" + rName + ")",
             css::uno::Reference<css::uno::XInterface>());
 
     uno::Reference<beans::XPropertySet> const xRet(
-            SwXFieldMaster::CreateXFieldMaster(GetDoc(), pType));
+            SwXFieldMaster::CreateXFieldMaster(&rDoc, pType));
     return uno::Any(xRet);
 }
 
@@ -2786,10 +2785,8 @@ bool SwXTextFieldMasters::getInstanceName(
 uno::Sequence< OUString > SwXTextFieldMasters::getElementNames()
 {
     SolarMutexGuard aGuard;
-    if(!GetDoc())
-        throw uno::RuntimeException();
 
-    const SwFieldTypes* pFieldTypes = 
GetDoc()->getIDocumentFieldsAccess().GetFieldTypes();
+    const SwFieldTypes* pFieldTypes = 
GetDoc().getIDocumentFieldsAccess().GetFieldTypes();
     const size_t nCount = pFieldTypes->size();
 
     std::vector<OUString> aFieldNames;
@@ -2810,8 +2807,6 @@ uno::Sequence< OUString > 
SwXTextFieldMasters::getElementNames()
 sal_Bool SwXTextFieldMasters::hasByName(const OUString& rName)
 {
     SolarMutexGuard aGuard;
-    if(!GetDoc())
-        throw uno::RuntimeException();
 
     OUString sName(rName), sTypeName;
     const SwFieldIds nResId = lcl_GetIdByName( sName, sTypeName );
@@ -2819,7 +2814,7 @@ sal_Bool SwXTextFieldMasters::hasByName(const OUString& 
rName)
     if( SwFieldIds::Unknown != nResId )
     {
         sName = sName.copy(std::min(sTypeName.getLength()+1, 
sName.getLength()));
-        bRet = nullptr != 
GetDoc()->getIDocumentFieldsAccess().GetFieldType(nResId, sName, true);
+        bRet = nullptr != 
GetDoc().getIDocumentFieldsAccess().GetFieldType(nResId, sName, true);
     }
     return bRet;
 }
@@ -2882,9 +2877,7 @@ void SwXTextFieldTypes::Invalidate()
 uno::Reference< container::XEnumeration >  
SwXTextFieldTypes::createEnumeration()
 {
     SolarMutexGuard aGuard;
-    if(!IsValid())
-        throw uno::RuntimeException();
-    return new SwXFieldEnumeration(*GetDoc());
+    return new SwXFieldEnumeration(GetDoc());
 }
 
 uno::Type  SwXTextFieldTypes::getElementType()
@@ -2904,11 +2897,10 @@ void SAL_CALL SwXTextFieldTypes::refresh()
 {
     {
         SolarMutexGuard aGuard;
-        if (!IsValid())
-            throw uno::RuntimeException();
-        UnoActionContext aContext(GetDoc());
-        GetDoc()->getIDocumentStatistics().UpdateDocStat( false, true );
-        GetDoc()->getIDocumentFieldsAccess().UpdateFields(false);
+        auto& rDoc = GetDoc();
+        UnoActionContext aContext(&rDoc);
+        rDoc.getIDocumentStatistics().UpdateDocStat(false, true);
+        rDoc.getIDocumentFieldsAccess().UpdateFields(false);
     }
     // call refresh listeners (without SolarMutex locked)
     lang::EventObject const event(getXWeak());
diff --git a/sw/source/core/unocore/unoidx.cxx 
b/sw/source/core/unocore/unoidx.cxx
index 2505050d8cae..3617c22b8da3 100644
--- a/sw/source/core/unocore/unoidx.cxx
+++ b/sw/source/core/unocore/unoidx.cxx
@@ -2337,11 +2337,8 @@ SwXDocumentIndexes::getCount()
 {
     SolarMutexGuard aGuard;
 
-    if(!IsValid())
-        throw uno::RuntimeException();
-
     sal_uInt32 nRet = 0;
-    const SwSectionFormats& rFormats = GetDoc()->GetSections();
+    const SwSectionFormats& rFormats = GetDoc().GetSections();
     for( size_t n = 0; n < rFormats.size(); ++n )
     {
         const SwSection* pSect = rFormats[ n ]->GetSection();
@@ -2359,12 +2356,10 @@ SwXDocumentIndexes::getByIndex(sal_Int32 nIndex)
 {
     SolarMutexGuard aGuard;
 
-    if(!IsValid())
-        throw uno::RuntimeException();
-
     sal_Int32 nIdx = 0;
 
-    const SwSectionFormats& rFormats = GetDoc()->GetSections();
+    auto& rDoc = GetDoc();
+    const SwSectionFormats& rFormats = rDoc.GetSections();
     for( size_t n = 0; n < rFormats.size(); ++n )
     {
         SwSection* pSect = rFormats[ n ]->GetSection();
@@ -2374,7 +2369,7 @@ SwXDocumentIndexes::getByIndex(sal_Int32 nIndex)
         {
            const uno::Reference< text::XDocumentIndex > xTmp =
                SwXDocumentIndex::CreateXDocumentIndex(
-                   *GetDoc(), static_cast<SwTOXBaseSection *>(pSect));
+                   rDoc, static_cast<SwTOXBaseSection *>(pSect));
            uno::Any aRet;
            aRet <<= xTmp;
            return aRet;
@@ -2389,10 +2384,8 @@ SwXDocumentIndexes::getByName(const OUString& rName)
 {
     SolarMutexGuard aGuard;
 
-    if(!IsValid())
-        throw uno::RuntimeException();
-
-    const SwSectionFormats& rFormats = GetDoc()->GetSections();
+    auto& rDoc = GetDoc();
+    const SwSectionFormats& rFormats = rDoc.GetSections();
     for( size_t n = 0; n < rFormats.size(); ++n )
     {
         SwSection* pSect = rFormats[ n ]->GetSection();
@@ -2403,7 +2396,7 @@ SwXDocumentIndexes::getByName(const OUString& rName)
         {
            const uno::Reference< text::XDocumentIndex > xTmp =
                SwXDocumentIndex::CreateXDocumentIndex(
-                   *GetDoc(), static_cast<SwTOXBaseSection *>(pSect));
+                   rDoc, static_cast<SwTOXBaseSection *>(pSect));
            uno::Any aRet;
            aRet <<= xTmp;
            return aRet;
@@ -2417,10 +2410,7 @@ SwXDocumentIndexes::getElementNames()
 {
     SolarMutexGuard aGuard;
 
-    if(!IsValid())
-        throw uno::RuntimeException();
-
-    const SwSectionFormats& rFormats = GetDoc()->GetSections();
+    const SwSectionFormats& rFormats = GetDoc().GetSections();
     sal_Int32 nCount = 0;
     for( size_t n = 0; n < rFormats.size(); ++n )
     {
@@ -2452,10 +2442,7 @@ SwXDocumentIndexes::hasByName(const OUString& rName)
 {
     SolarMutexGuard aGuard;
 
-    if(!IsValid())
-        throw uno::RuntimeException();
-
-    const SwSectionFormats& rFormats = GetDoc()->GetSections();
+    const SwSectionFormats& rFormats = GetDoc().GetSections();
     for( size_t n = 0; n < rFormats.size(); ++n )
     {
         SwSection const*const pSect = rFormats[ n ]->GetSection();
diff --git a/sw/source/core/unocore/unoredlines.cxx 
b/sw/source/core/unocore/unoredlines.cxx
index 572bb7756647..315513632d68 100644
--- a/sw/source/core/unocore/unoredlines.cxx
+++ b/sw/source/core/unocore/unoredlines.cxx
@@ -49,31 +49,26 @@ SwXRedlines::~SwXRedlines()
 sal_Int32 SwXRedlines::getCount(  )
 {
     SolarMutexGuard aGuard;
-    if(!IsValid())
-        throw uno::RuntimeException();
-    const SwRedlineTable& rRedTable = 
GetDoc()->getIDocumentRedlineAccess().GetRedlineTable();
+    const SwRedlineTable& rRedTable = 
GetDoc().getIDocumentRedlineAccess().GetRedlineTable();
     return rRedTable.size();
 }
 
 uno::Any SwXRedlines::getByIndex(sal_Int32 nIndex)
 {
     SolarMutexGuard aGuard;
-    if(!IsValid())
-        throw uno::RuntimeException();
-    const SwRedlineTable& rRedTable = 
GetDoc()->getIDocumentRedlineAccess().GetRedlineTable();
+    auto& rDoc = GetDoc();
+    const SwRedlineTable& rRedTable = 
rDoc.getIDocumentRedlineAccess().GetRedlineTable();
     if ((nIndex < 0) || (rRedTable.size() <= o3tl::make_unsigned(nIndex)))
         throw lang::IndexOutOfBoundsException();
 
-    uno::Reference <beans::XPropertySet> xRet = SwXRedlines::GetObject( 
*rRedTable[nIndex], *GetDoc() );
+    uno::Reference<beans::XPropertySet> xRet = 
SwXRedlines::GetObject(*rRedTable[nIndex], rDoc);
     return uno::Any(xRet);
 }
 
 uno::Reference< container::XEnumeration >  SwXRedlines::createEnumeration()
 {
     SolarMutexGuard aGuard;
-    if(!IsValid())
-        throw uno::RuntimeException();
-    return uno::Reference< container::XEnumeration >(new 
SwXRedlineEnumeration(*GetDoc()));
+    return uno::Reference< container::XEnumeration >(new 
SwXRedlineEnumeration(GetDoc()));
 }
 
 uno::Type SwXRedlines::getElementType(  )
@@ -84,9 +79,7 @@ uno::Type SwXRedlines::getElementType(  )
 sal_Bool SwXRedlines::hasElements(  )
 {
     SolarMutexGuard aGuard;
-    if(!IsValid())
-        throw uno::RuntimeException();
-    const SwRedlineTable& rRedTable = 
GetDoc()->getIDocumentRedlineAccess().GetRedlineTable();
+    const SwRedlineTable& rRedTable = 
GetDoc().getIDocumentRedlineAccess().GetRedlineTable();
     return !rRedTable.empty();
 }
 
diff --git a/sw/source/core/unocore/unosett.cxx 
b/sw/source/core/unocore/unosett.cxx
index 1f25b8989402..bfc4b00a3432 100644
--- a/sw/source/core/unocore/unosett.cxx
+++ b/sw/source/core/unocore/unosett.cxx
@@ -1049,6 +1049,8 @@ SwXNumberingRules::SwXNumberingRules(SwDocShell& rDocSh) :
     m_pPropertySet(GetNumberingRulesSet()),
     m_bOwnNumRuleCreated(false)
 {
+    if (!m_pDocShell->GetDoc())
+        throw uno::RuntimeException("Unitialized shell passed to 
SwXNumberingRules constructor");
     m_pImpl->StartListening(GetPageDescNotifier(m_pDocShell->GetDoc()));
 }
 
diff --git a/sw/source/uibase/uno/unotxdoc.cxx 
b/sw/source/uibase/uno/unotxdoc.cxx
index 66a05836fa38..f5e0904fa8d3 100644
--- a/sw/source/uibase/uno/unotxdoc.cxx
+++ b/sw/source/uibase/uno/unotxdoc.cxx
@@ -84,6 +84,7 @@
 #include <com/sun/star/lang/DisposedException.hpp>
 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
 #include <com/sun/star/lang/NoSupportException.hpp>
+#include <com/sun/star/lang/NotInitializedException.hpp>
 #include <com/sun/star/beans/PropertyAttribute.hpp>
 #include <com/sun/star/beans/XFastPropertySet.hpp>
 #include <com/sun/star/beans/XPropertyAccess.hpp>
@@ -380,10 +381,19 @@ SwXTextDocument::SwXTextDocument(SwDocShell* pShell)
 {
 }
 
+SwDoc& SwXTextDocument::GetDocOrThrow() const
+{
+    if (SwDoc* pDoc = m_pDocShell->GetDoc())
+        return *pDoc;
+    throw css::lang::NotInitializedException(
+        u"Document not initialized by a call to attachResource() or 
load()"_ustr,
+        const_cast<SwXTextDocument*>(this)->getXWeak());
+}
+
 SdrModel& SwXTextDocument::getSdrModelFromUnoModel() const
 {
-    
OSL_ENSURE(m_pDocShell->GetDoc()->getIDocumentDrawModelAccess().GetOrCreateDrawModel(),
 "No SdrModel in SwDoc, should not happen");
-    return 
*m_pDocShell->GetDoc()->getIDocumentDrawModelAccess().GetDrawModel();
+    
assert(GetDocOrThrow().getIDocumentDrawModelAccess().GetOrCreateDrawModel());
+    return *GetDocOrThrow().getIDocumentDrawModelAccess().GetDrawModel();
 }
 
 SwXTextDocument::~SwXTextDocument()
@@ -410,7 +420,7 @@ SwXDocumentPropertyHelper * 
SwXTextDocument::GetPropertyHelper ()
 {
     if(!mxPropertyHelper.is())
     {
-        mxPropertyHelper = new 
SwXDocumentPropertyHelper(*m_pDocShell->GetDoc());
+        mxPropertyHelper = new SwXDocumentPropertyHelper(GetDocOrThrow());
     }
     return mxPropertyHelper.get();
 }
@@ -441,7 +451,7 @@ void SwXTextDocument::GetNumberFormatter()
             = 
comphelper::getFromUnoTunnel<SvNumberFormatsSupplierObj>(xNumTunnel);
         OSL_ENSURE(pNumFormat, "No number formatter available");
         if (pNumFormat && !pNumFormat->GetNumberFormatter())
-            
pNumFormat->SetNumberFormatter(m_pDocShell->GetDoc()->GetNumberFormatter());
+            
pNumFormat->SetNumberFormatter(GetDocOrThrow().GetNumberFormatter());
     }
 }
 
@@ -720,12 +730,13 @@ sal_Int32 SwXTextDocument::replaceAll(const Reference< 
util::XSearchDescriptor >
     //try attribute search first
     if(pSearch->HasSearchAttributes()||pSearch->HasReplaceAttributes())
     {
+        auto& pool = GetDocOrThrow().GetAttrPool();
         SfxItemSetFixed<RES_CHRATR_BEGIN, RES_CHRATR_END-1,
                             RES_PARATR_BEGIN, RES_PARATR_END-1,
-                            RES_FRMATR_BEGIN, RES_FRMATR_END-1>  
aSearch(m_pDocShell->GetDoc()->GetAttrPool());
+                            RES_FRMATR_BEGIN, RES_FRMATR_END-1>  aSearch(pool);
         SfxItemSetFixed<RES_CHRATR_BEGIN, RES_CHRATR_END-1,
                             RES_PARATR_BEGIN, RES_PARATR_END-1,
-                            RES_FRMATR_BEGIN, RES_FRMATR_END-1>  
aReplace(m_pDocShell->GetDoc()->GetAttrPool());
+                            RES_FRMATR_BEGIN, RES_FRMATR_END-1>  
aReplace(pool);
         pSearch->FillSearchItemSet(aSearch);
         pSearch->FillReplaceItemSet(aReplace);
         bool bCancel;
@@ -838,7 +849,7 @@ SwUnoCursor* SwXTextDocument::FindAny(const Reference< 
util::XSearchDescriptor >
                     RES_TXTATR_INETFMT, RES_TXTATR_CHARFMT,
                     RES_PARATR_BEGIN, RES_PARATR_END - 1,
                     RES_FRMATR_BEGIN, RES_FRMATR_END - 1>
-                 aSearch( m_pDocShell->GetDoc()->GetAttrPool() );
+                 aSearch( GetDocOrThrow().GetAttrPool() );
             pSearch->FillSearchItemSet(aSearch);
             bool bCancel;
             nResult = pUnoCursor->FindAttrs(aSearch, !pSearch->m_bStyles,
@@ -899,7 +910,7 @@ Reference< XInterface >  SwXTextDocument::findFirst(const 
Reference< util::XSear
     if(nResult)
     {
         const uno::Reference< text::XText >  xParent =
-            ::sw::CreateParentXText(*m_pDocShell->GetDoc(),
+            ::sw::CreateParentXText(GetDocOrThrow(),
                     *pResultCursor->GetPoint());
         xRet = *new SwXTextCursor(xParent, *pResultCursor);
     }
@@ -921,7 +932,7 @@ Reference< XInterface >  SwXTextDocument::findNext(const 
Reference< XInterface >
     if(nResult)
     {
         const uno::Reference< text::XText >  xParent =
-            ::sw::CreateParentXText(*m_pDocShell->GetDoc(),
+            ::sw::CreateParentXText(GetDocOrThrow(),
                     *pResultCursor->GetPoint());
 
         xRet = *new SwXTextCursor(xParent, *pResultCursor);
@@ -938,7 +949,7 @@ Sequence< beans::PropertyValue > 
SwXTextDocument::getPagePrintSettings()
 
     beans::PropertyValue* pArray = aSeq.getArray();
     SwPagePreviewPrtData aData;
-    const SwPagePreviewPrtData* pData = 
m_pDocShell->GetDoc()->GetPreviewPrtData();
+    const SwPagePreviewPrtData* pData = GetDocOrThrow().GetPreviewPrtData();
     if(pData)
         aData = *pData;
     Any aVal;
@@ -1013,8 +1024,9 @@ void SwXTextDocument::setPagePrintSettings(const 
Sequence< beans::PropertyValue
         throw DisposedException("", static_cast< XTextDocument* >(this));
 
     SwPagePreviewPrtData aData;
+    SwDoc& rDoc = GetDocOrThrow();
     //if only a few properties are coming, then use the current settings
-    const SwPagePreviewPrtData* pData = 
m_pDocShell->GetDoc()->GetPreviewPrtData();
+    const SwPagePreviewPrtData* pData = rDoc.GetPreviewPrtData();
     if(pData)
         aData = *pData;
     for(const beans::PropertyValue& rProperty : aSettings)
@@ -1073,7 +1085,7 @@ void SwXTextDocument::setPagePrintSettings(const 
Sequence< beans::PropertyValue
         if(bException)
             throw RuntimeException();
     }
-    m_pDocShell->GetDoc()->SetPreviewPrtData(&aData);
+    rDoc.SetPreviewPrtData(&aData);
 
 }
 
@@ -1085,7 +1097,7 @@ void SwXTextDocument::printPages(const Sequence< 
beans::PropertyValue >& xOption
 
     SfxViewFrame* pFrame = SfxViewFrame::LoadHiddenDocument( *m_pDocShell, 
SfxInterfaceId(7) );
     SfxRequest aReq(FN_PRINT_PAGEPREVIEW, SfxCallMode::SYNCHRON,
-                                m_pDocShell->GetDoc()->GetAttrPool());
+                                GetDocOrThrow().GetAttrPool());
     aReq.AppendItem(SfxBoolItem(FN_PRINT_PAGEPREVIEW, true));
 
     for ( const beans::PropertyValue &rProp : xOptions )
@@ -1296,11 +1308,11 @@ Reference< drawing::XDrawPage >  
SwXTextDocument::getDrawPage()
         throw DisposedException("", static_cast< XTextDocument* >(this));
     if(!m_xDrawPage.is())
     {
-        SwDoc* pDoc = m_pDocShell->GetDoc();
+        SwDoc& rDoc = GetDocOrThrow();
         // #i52858#
-        SwDrawModel* pModel = 
pDoc->getIDocumentDrawModelAccess().GetOrCreateDrawModel();
+        SwDrawModel* pModel = 
rDoc.getIDocumentDrawModelAccess().GetOrCreateDrawModel();
         SdrPage* pPage = pModel->GetPage( 0 );
-        m_xDrawPage = new SwFmDrawPage(pDoc, pPage);
+        m_xDrawPage = new SwFmDrawPage(&rDoc, pPage);
     }
     return m_xDrawPage;
 }
@@ -1595,7 +1607,7 @@ css::uno::Reference<css::uno::XInterface> 
SwXTextDocument::create(
     const SwServiceType nType = 
SwXServiceProvider::GetProviderType(rServiceName);
     if (nType != SwServiceType::Invalid)
     {
-        return SwXServiceProvider::MakeInstance(nType, *m_pDocShell->GetDoc());
+        return SwXServiceProvider::MakeInstance(nType, GetDocOrThrow());
     }
     if (rServiceName == "com.sun.star.drawing.DashTable")
     {
@@ -1801,19 +1813,21 @@ void SwXTextDocument::setPropertyValue(const OUString& 
rPropertyName, const Any&
         case WID_DOC_CHANGES_RECORD:
         case WID_DOC_CHANGES_SHOW:
         {
+            SwDoc& rDoc = GetDocOrThrow();
+            sw::DocumentRedlineManager& rRedlineManager = 
rDoc.GetDocumentRedlineManager();
             bool bSet = *o3tl::doAccess<bool>(aValue);
-            RedlineFlags eMode = 
m_pDocShell->GetDoc()->getIDocumentRedlineAccess().GetRedlineFlags();
+            RedlineFlags eMode = rRedlineManager.GetRedlineFlags();
             if(WID_DOC_CHANGES_SHOW == pEntry->nWID)
             {
                 eMode |= RedlineFlags(RedlineFlags::ShowInsert | 
RedlineFlags::ShowDelete);
                 if( !bSet )
-                    
m_pDocShell->GetDoc()->GetDocumentRedlineManager().SetHideRedlines(true);
+                    rRedlineManager.SetHideRedlines(true);
             }
             else if(WID_DOC_CHANGES_RECORD == pEntry->nWID)
             {
                 eMode = bSet ? eMode|RedlineFlags::On : 
eMode&~RedlineFlags::On;
             }
-            
m_pDocShell->GetDoc()->getIDocumentRedlineAccess().SetRedlineFlags( eMode );
+            rRedlineManager.SetRedlineFlags(eMode);
         }
         break;
         case  WID_DOC_CHANGES_PASSWORD:
@@ -1821,13 +1835,13 @@ void SwXTextDocument::setPropertyValue(const OUString& 
rPropertyName, const Any&
             Sequence <sal_Int8> aNew;
             if(aValue >>= aNew)
             {
-                SwDoc* pDoc = m_pDocShell->GetDoc();
-                pDoc->getIDocumentRedlineAccess().SetRedlinePassword(aNew);
+                auto& rRedlineAccess = 
GetDocOrThrow().getIDocumentRedlineAccess();
+                rRedlineAccess.SetRedlinePassword(aNew);
                 if(aNew.hasElements())
                 {
-                    RedlineFlags eMode = 
pDoc->getIDocumentRedlineAccess().GetRedlineFlags();
+                    RedlineFlags eMode = rRedlineAccess.GetRedlineFlags();
                     eMode |= RedlineFlags::On;
-                    pDoc->getIDocumentRedlineAccess().SetRedlineFlags( eMode );
+                    rRedlineAccess.SetRedlineFlags(eMode);
                 }
             }
         }
@@ -1836,7 +1850,7 @@ void SwXTextDocument::setPropertyValue(const OUString& 
rPropertyName, const Any&
         {
             OUString sURL;
             aValue >>= sURL;
-            m_pDocShell->GetDoc()->SetTOIAutoMarkURL(sURL);
+            GetDocOrThrow().SetTOIAutoMarkURL(sURL);
         }
         break;
         case WID_DOC_HIDE_TIPS :
@@ -1844,7 +1858,8 @@ void SwXTextDocument::setPropertyValue(const OUString& 
rPropertyName, const Any&
         break;
         case WID_DOC_REDLINE_DISPLAY:
         {
-            RedlineFlags eRedMode = 
m_pDocShell->GetDoc()->getIDocumentRedlineAccess().GetRedlineFlags();
+            auto& rRedlineAccess = GetDocOrThrow().getIDocumentRedlineAccess();
+            RedlineFlags eRedMode = rRedlineAccess.GetRedlineFlags();
             eRedMode = eRedMode & (~RedlineFlags::ShowMask);
             sal_Int16 nSet = 0;
             aValue >>= nSet;
@@ -1858,82 +1873,75 @@ void SwXTextDocument::setPropertyValue(const OUString& 
rPropertyName, const Any&
                 break;
                 default: throw IllegalArgumentException();
             }
-            
m_pDocShell->GetDoc()->getIDocumentRedlineAccess().SetRedlineFlags(eRedMode);
+            rRedlineAccess.SetRedlineFlags(eRedMode);
         }
         break;
         case WID_DOC_TWO_DIGIT_YEAR:
         {
             sal_Int16 nYear = 0;
             aValue >>= nYear;
-            SfxRequest aRequest ( SID_ATTR_YEAR2000, SfxCallMode::SLOT, 
m_pDocShell->GetDoc()->GetAttrPool());
+            SfxRequest aRequest ( SID_ATTR_YEAR2000, SfxCallMode::SLOT, 
GetDocOrThrow().GetAttrPool());
             aRequest.AppendItem(SfxUInt16Item( SID_ATTR_YEAR2000, static_cast 
< sal_uInt16 > ( nYear ) ) );
             m_pDocShell->Execute ( aRequest );
         }
         break;
         case WID_DOC_AUTOMATIC_CONTROL_FOCUS:
         {
-            SwDrawModel * pDrawDoc = 
m_pDocShell->GetDoc()->getIDocumentDrawModelAccess().GetDrawModel();
+            auto& rDrawModelAccess = 
GetDocOrThrow().getIDocumentDrawModelAccess();
             bool bAuto = *o3tl::doAccess<bool>(aValue);
+            // if setting to true, and we don't have an
+            // SdrModel, then we are changing the default and
+            // must thus create an SdrModel, if we don't have an
+            // SdrModel and we are leaving the default at false,
+            // we don't need to make an SdrModel and can do nothing
+            // #i52858# - method name changed
+            SwDrawModel* pDrawDoc
+                = bAuto ? rDrawModelAccess.GetOrCreateDrawModel() : 
rDrawModelAccess.GetDrawModel();
 
             if ( nullptr != pDrawDoc )
                 pDrawDoc->SetAutoControlFocus( bAuto );
-            else if (bAuto)
-            {
-                // if setting to true, and we don't have an
-                // SdrModel, then we are changing the default and
-                // must thus create an SdrModel, if we don't have an
-                // SdrModel and we are leaving the default at false,
-                // we don't need to make an SdrModel and can do nothing
-                // #i52858# - method name changed
-                pDrawDoc = 
m_pDocShell->GetDoc()->getIDocumentDrawModelAccess().GetOrCreateDrawModel();
-                pDrawDoc->SetAutoControlFocus ( bAuto );
-            }
         }
         break;
         case WID_DOC_APPLY_FORM_DESIGN_MODE:
         {
-            SwDrawModel * pDrawDoc = 
m_pDocShell->GetDoc()->getIDocumentDrawModelAccess().GetDrawModel();
+            auto& rDrawModelAccess = 
GetDocOrThrow().getIDocumentDrawModelAccess();
             bool bMode = *o3tl::doAccess<bool>(aValue);
+            // if setting to false, and we don't have an
+            // SdrModel, then we are changing the default and
+            // must thus create an SdrModel, if we don't have an
+            // SdrModel and we are leaving the default at true,
+            // we don't need to make an SdrModel and can do
+            // nothing
+            // #i52858# - method name changed
+            SwDrawModel* pDrawDoc
+                = bMode ? rDrawModelAccess.GetDrawModel() : 
rDrawModelAccess.GetOrCreateDrawModel();
 
             if ( nullptr != pDrawDoc )
                 pDrawDoc->SetOpenInDesignMode( bMode );
-            else if (!bMode)
-            {
-                // if setting to false, and we don't have an
-                // SdrModel, then we are changing the default and
-                // must thus create an SdrModel, if we don't have an
-                // SdrModel and we are leaving the default at true,
-                // we don't need to make an SdrModel and can do
-                // nothing
-                // #i52858# - method name changed
-                pDrawDoc = 
m_pDocShell->GetDoc()->getIDocumentDrawModelAccess().GetOrCreateDrawModel();
-                pDrawDoc->SetOpenInDesignMode ( bMode );
-            }
         }
         break;
         // #i42634# New property to set the bInReading
         // flag at the document, used during binary import
         case WID_DOC_LOCK_UPDATES :
         {
-            SwDoc* pDoc = m_pDocShell->GetDoc();
             bool bBool (false);
             if( aValue >>= bBool )
             {
-              pDoc->SetInReading( bBool );
+                GetDocOrThrow().SetInReading( bBool );
             }
         }
         break;
         case WID_DOC_WRITERFILTER:
         {
-            SwDoc* pDoc = m_pDocShell->GetDoc();
+            SwDoc& rDoc = GetDocOrThrow();
             bool bBool = {};
             if (aValue >>= bBool)
             { // HACK: writerfilter has to use API to set this :(
-                bool bOld = pDoc->IsInWriterfilterImport();
-                pDoc->SetInWriterfilterImport(bBool);
+                bool bOld = rDoc.IsInWriterfilterImport();
+                rDoc.SetInWriterfilterImport(bBool);
                 if (bOld && !bBool)
                 {
-                    pDoc->getIDocumentFieldsAccess().SetFieldsDirty(false, 
nullptr, SwNodeOffset(0));
+                    rDoc.getIDocumentFieldsAccess().SetFieldsDirty(false, 
nullptr, SwNodeOffset(0));
                 }
             }
         }
@@ -1946,7 +1954,7 @@ void SwXTextDocument::setPropertyValue(const OUString& 
rPropertyName, const Any&
         {
             bool bDefaultPageMode( false );
             aValue >>= bDefaultPageMode;
-            m_pDocShell->GetDoc()->SetDefaultPageMode( bDefaultPageMode );
+            GetDocOrThrow().SetDefaultPageMode( bDefaultPageMode );
         }
         break;
         case WID_DOC_INTEROP_GRAB_BAG:
@@ -1955,10 +1963,11 @@ void SwXTextDocument::setPropertyValue(const OUString& 
rPropertyName, const Any&
 
         default:
         {
-            const SfxPoolItem& rItem = 
m_pDocShell->GetDoc()->GetDefault(pEntry->nWID);
+            SwDoc& rDoc = GetDocOrThrow();
+            const SfxPoolItem& rItem = rDoc.GetDefault(pEntry->nWID);
             std::unique_ptr<SfxPoolItem> pNewItem(rItem.Clone());
             pNewItem->PutValue(aValue, pEntry->nMemberId);
-            m_pDocShell->GetDoc()->SetDefault(*pNewItem);
+            rDoc.SetDefault(*pNewItem);
         }
     }
 }
@@ -1978,7 +1987,7 @@ Any SwXTextDocument::getPropertyValue(const OUString& 
rPropertyName)
         // [ index, styleIntPtr, list_id ]
         std::vector<css::uno::Sequence<css::uno::Any>> nodes;
 
-        const SwDoc& rDoc = *m_pDocShell->GetDoc();
+        const SwDoc& rDoc = GetDocOrThrow();
         for (const SwNumRule* pNumRule : rDoc.GetNumRuleTable())
         {
             SwNumRule::tTextNodeList textNodes;
@@ -2010,7 +2019,7 @@ Any SwXTextDocument::getPropertyValue(const OUString& 
rPropertyName)
         case  WID_DOC_PARA_COUNT     :
         case  WID_DOC_WORD_COUNT     :
         {
-            const SwDocStat& 
rStat(m_pDocShell->GetDoc()->getIDocumentStatistics().GetUpdatedDocStat( false, 
true ));
+            const SwDocStat& 
rStat(GetDocOrThrow().getIDocumentStatistics().GetUpdatedDocStat( false, true 
));
             sal_Int32 nValue;
             switch(pEntry->nWID)
             {
@@ -2029,7 +2038,7 @@ Any SwXTextDocument::getPropertyValue(const OUString& 
rPropertyName)
         case WID_DOC_CHANGES_RECORD:
         case WID_DOC_CHANGES_SHOW:
         {
-            const RedlineFlags eMode = 
m_pDocShell->GetDoc()->getIDocumentRedlineAccess().GetRedlineFlags();
+            const RedlineFlags eMode = 
GetDocOrThrow().getIDocumentRedlineAccess().GetRedlineFlags();
             bool bSet = false;
             if(WID_DOC_CHANGES_SHOW == pEntry->nWID)
             {
@@ -2043,20 +2052,17 @@ Any SwXTextDocument::getPropertyValue(const OUString& 
rPropertyName)
         }
         break;
         case  WID_DOC_CHANGES_PASSWORD:
-        {
-            SwDoc* pDoc = m_pDocShell->GetDoc();
-            aAny <<= pDoc->getIDocumentRedlineAccess().GetRedlinePassword();
-        }
+            aAny <<= 
GetDocOrThrow().getIDocumentRedlineAccess().GetRedlinePassword();
         break;
         case WID_DOC_AUTO_MARK_URL :
-            aAny <<= m_pDocShell->GetDoc()->GetTOIAutoMarkURL();
+            aAny <<= GetDocOrThrow().GetTOIAutoMarkURL();
         break;
         case WID_DOC_HIDE_TIPS :
             aAny <<= SW_MOD()->GetModuleConfig()->IsHideFieldTips();
         break;
         case WID_DOC_REDLINE_DISPLAY:
         {
-            RedlineFlags eRedMode = 
m_pDocShell->GetDoc()->getIDocumentRedlineAccess().GetRedlineFlags();
+            RedlineFlags eRedMode = 
GetDocOrThrow().getIDocumentRedlineAccess().GetRedlineFlags();
             eRedMode = eRedMode & RedlineFlags::ShowMask;
             sal_Int16 nRet = RedlineDisplayType::NONE;
             if(RedlineFlags::ShowInsert == eRedMode)
@@ -2077,12 +2083,12 @@ Any SwXTextDocument::getPropertyValue(const OUString& 
rPropertyName)
         break;
         case WID_DOC_TWO_DIGIT_YEAR:
         {
-            aAny <<= static_cast < sal_Int16 > 
(m_pDocShell->GetDoc()->GetNumberFormatter ()->GetYear2000());
+            aAny <<= static_cast < sal_Int16 > 
(GetDocOrThrow().GetNumberFormatter ()->GetYear2000());
         }
         break;
         case WID_DOC_AUTOMATIC_CONTROL_FOCUS:
         {
-            SwDrawModel * pDrawDoc = 
m_pDocShell->GetDoc()->getIDocumentDrawModelAccess().GetDrawModel();
+            SwDrawModel * pDrawDoc = 
GetDocOrThrow().getIDocumentDrawModelAccess().GetDrawModel();
             bool bAuto;
             if ( nullptr != pDrawDoc )
                 bAuto = pDrawDoc->GetAutoControlFocus();
@@ -2093,7 +2099,7 @@ Any SwXTextDocument::getPropertyValue(const OUString& 
rPropertyName)
         break;
         case WID_DOC_APPLY_FORM_DESIGN_MODE:
         {
-            SwDrawModel * pDrawDoc = 
m_pDocShell->GetDoc()->getIDocumentDrawModelAccess().GetDrawModel();
+            SwDrawModel * pDrawDoc = 
GetDocOrThrow().getIDocumentDrawModelAccess().GetDrawModel();
             bool bMode;
             if ( nullptr != pDrawDoc )
                 bMode = pDrawDoc->GetOpenInDesignMode();
@@ -2122,7 +2128,7 @@ Any SwXTextDocument::getPropertyValue(const OUString& 
rPropertyName)
             aAny <<= getRuntimeUID();
         break;
         case WID_DOC_LOCK_UPDATES :
-            aAny <<= m_pDocShell->GetDoc()->IsInReading();
+            aAny <<= GetDocOrThrow().IsInReading();
         break;
         case WID_DOC_BUILDID:
             aAny <<= maBuildId;
@@ -2136,7 +2142,7 @@ Any SwXTextDocument::getPropertyValue(const OUString& 
rPropertyName)
 
         default:
         {
-            const SfxPoolItem& rItem = 
m_pDocShell->GetDoc()->GetDefault(pEntry->nWID);
+            const SfxPoolItem& rItem = 
GetDocOrThrow().GetDefault(pEntry->nWID);
             rItem.QueryValue(aAny, pEntry->nMemberId);
         }
     }
@@ -2233,11 +2239,11 @@ void SwXTextDocument::updateLinks(  )
     if(!IsValid())
         throw DisposedException("", static_cast< XTextDocument* >(this));
 
-    SwDoc* pDoc = m_pDocShell->GetDoc();
-    sfx2::LinkManager& rLnkMan = 
pDoc->getIDocumentLinksAdministration().GetLinkManager();
+    SwDoc& rDoc = GetDocOrThrow();
+    sfx2::LinkManager& rLnkMan = 
rDoc.getIDocumentLinksAdministration().GetLinkManager();
     if( !rLnkMan.GetLinks().empty() )
     {
-        UnoActionContext aAction(pDoc);
+        UnoActionContext aAction(&rDoc);
         rLnkMan.UpdateAllLinks( false, true, nullptr );
     }
 }
@@ -2812,8 +2818,8 @@ uno::Sequence< beans::PropertyValue > SAL_CALL 
SwXTextDocument::getRenderer(
     // #i117783#
     if ( m_bApplyPagePrintSettingsFromXPagePrintable )
     {
-        const SwPagePreviewPrtData* pPagePrintSettings =
-                                        
m_pDocShell->GetDoc()->GetPreviewPrtData();
+        SwDoc& rDoc = GetDocOrThrow();
+        const SwPagePreviewPrtData* pPagePrintSettings = 
rDoc.GetPreviewPrtData();
         if ( pPagePrintSettings &&
              ( pPagePrintSettings->GetRow() > 1 ||
                pPagePrintSettings->GetCol() > 1 ) )
@@ -2843,23 +2849,20 @@ uno::Sequence< beans::PropertyValue > SAL_CALL 
SwXTextDocument::getRenderer(
             pRenderer[ nRenderDataIdxStart + 6 ].Value <<= 
pPagePrintSettings->GetHorzSpace();
             pRenderer[ nRenderDataIdxStart + 7 ].Name  = "NUpVerticalSpacing";
             pRenderer[ nRenderDataIdxStart + 7 ].Value <<= 
pPagePrintSettings->GetVertSpace();
+            if (Printer* pPrinter = 
rDoc.getIDocumentDeviceAccess().getPrinter(false))
             {
-                Printer* pPrinter = 
m_pDocShell->GetDoc()->getIDocumentDeviceAccess().getPrinter( false );
-                if ( pPrinter )
+                awt::Size aNewPageSize;
+                const Size aPageSize = pPrinter->PixelToLogic( 
pPrinter->GetPaperSizePixel(), MapMode( MapUnit::Map100thMM ) );
+                aNewPageSize = awt::Size( aPageSize.Width(), 
aPageSize.Height() );
+                if ( ( pPagePrintSettings->GetLandscape() &&
+                       aPageSize.Width() < aPageSize.Height() ) ||
+                     ( !pPagePrintSettings->GetLandscape() &&
+                       aPageSize.Width() > aPageSize.Height() ) )
                 {
-                    awt::Size aNewPageSize;
-                    const Size aPageSize = pPrinter->PixelToLogic( 
pPrinter->GetPaperSizePixel(), MapMode( MapUnit::Map100thMM ) );
-                    aNewPageSize = awt::Size( aPageSize.Width(), 
aPageSize.Height() );
-                    if ( ( pPagePrintSettings->GetLandscape() &&
-                           aPageSize.Width() < aPageSize.Height() ) ||
-                         ( !pPagePrintSettings->GetLandscape() &&
-                           aPageSize.Width() > aPageSize.Height() ) )
-                    {
-                        aNewPageSize = awt::Size( aPageSize.Height(), 
aPageSize.Width() );
-                    }
-                    pRenderer[ nRenderDataIdxStart + 8 ].Name  = 
"NUpPaperSize";
-                    pRenderer[ nRenderDataIdxStart + 8 ].Value <<= 
aNewPageSize;
+                    aNewPageSize = awt::Size( aPageSize.Height(), 
aPageSize.Width() );
                 }
+                pRenderer[ nRenderDataIdxStart + 8 ].Name  = "NUpPaperSize";
+                pRenderer[ nRenderDataIdxStart + 8 ].Value <<= aNewPageSize;
             }
         }
 
@@ -3078,8 +3081,7 @@ Reference<XNameContainer> SAL_CALL 
SwXTextDocument::getXForms()
     SolarMutexGuard aGuard;
     if ( !m_pDocShell )
         throw DisposedException( OUString(), static_cast< XTextDocument* >( 
this ) );
-    SwDoc* pDoc = m_pDocShell->GetDoc();
-    return pDoc->getXForms();
+    return GetDocOrThrow().getXForms();
 }
 
 uno::Reference< text::XFlatParagraphIterator > SAL_CALL 
SwXTextDocument::getFlatParagraphIterator(::sal_Int32 nTextMarkupType, sal_Bool 
bAutomatic)
@@ -3092,7 +3094,7 @@ uno::Reference< text::XFlatParagraphIterator > SAL_CALL 
SwXTextDocument::getFlat
     }
 
     return SwUnoCursorHelper::CreateFlatParagraphIterator(
-            *m_pDocShell->GetDoc(), nTextMarkupType, bAutomatic);
+            GetDocOrThrow(), nTextMarkupType, bAutomatic);
 }
 
 uno::Reference< util::XCloneable > SwXTextDocument::createClone(  )
@@ -3105,7 +3107,7 @@ uno::Reference< util::XCloneable > 
SwXTextDocument::createClone(  )
     // SfxObjectShellRef is used here, since the model should control object 
lifetime after creation
     // and thus SfxObjectShellLock is not allowed here
     // the model holds reference to the shell, so the shell will not 
destructed at the end of method
-    SfxObjectShellRef pShell = m_pDocShell->GetDoc()->CreateCopy(false, false);
+    SfxObjectShellRef pShell = GetDocOrThrow().CreateCopy(false, false);
     uno::Reference< frame::XModel > xNewModel = pShell->GetModel();
     uno::Reference< embed::XStorage > xNewStorage = 
::comphelper::OStorageHelper::GetTemporaryStorage( );
     uno::Sequence< beans::PropertyValue > aTempMediaDescriptor;
@@ -3146,7 +3148,7 @@ void SwXTextDocument::paintTile( VirtualDevice &rDevice,
 
     // Draw Form controls
     comphelper::LibreOfficeKit::setTiledPainting(true);
-    SwDrawModel* pDrawLayer = 
m_pDocShell->GetDoc()->getIDocumentDrawModelAccess().GetDrawModel();
+    SwDrawModel* pDrawLayer = 
GetDocOrThrow().getIDocumentDrawModelAccess().GetDrawModel();
     SdrPage* pPage = pDrawLayer->GetPage(sal_uInt16(0));
     SdrView* pDrawView = pViewShell->GetDrawView();
     SwEditWin& rEditWin = m_pDocShell->GetView()->GetEditWin();
@@ -3287,7 +3289,7 @@ void 
SwXTextDocument::getTrackedChanges(tools::JsonWriter& rJson)
         return;
 
     const SwRedlineTable& rRedlineTable
-        = m_pDocShell->GetDoc()->getIDocumentRedlineAccess().GetRedlineTable();
+        = GetDocOrThrow().getIDocumentRedlineAccess().GetRedlineTable();
     for (SwRedlineTable::size_type i = 0; i < rRedlineTable.size(); ++i)
     {
         auto redlineNode = rJson.startStruct();
@@ -3731,7 +3733,7 @@ void SwXTextDocument::postMouseEvent(int nType, int nX, 
int nY, int nCount, int
         return;
 
     // try to forward mouse event to controls
-    SwDrawModel* pDrawLayer = 
m_pDocShell->GetDoc()->getIDocumentDrawModelAccess().GetDrawModel();
+    SwDrawModel* pDrawLayer = 
GetDocOrThrow().getIDocumentDrawModelAccess().GetDrawModel();
     SdrPage* pPage = pDrawLayer->GetPage(sal_uInt16(0));
     SdrView* pDrawView = pWrtViewShell->GetDrawView();
     SwEditWin& rEditWin = m_pDocShell->GetView()->GetEditWin();
@@ -3888,14 +3890,14 @@ uno::Sequence< lang::Locale > SAL_CALL 
SwXTextDocument::getDocumentLanguages(
         throw IllegalArgumentException("nScriptTypes ranges from 1 to 7!", 
Reference< XInterface >(), 1);
     if (!m_pDocShell)
         throw DisposedException();
-    SwDoc* pDoc = m_pDocShell->GetDoc();
+    SwDoc& rDoc = GetDocOrThrow();
 
     // avoid duplicate values
     std::set< LanguageType > aAllLangs;
 
     //USER STYLES
 
-    const SwCharFormats *pFormats = pDoc->GetCharFormats();
+    const SwCharFormats *pFormats = rDoc.GetCharFormats();
     for(size_t i = 0; i < pFormats->size(); ++i)
     {
         const SwAttrSet &rAttrSet = (*pFormats)[i]->GetAttrSet();
@@ -3920,7 +3922,7 @@ uno::Sequence< lang::Locale > SAL_CALL 
SwXTextDocument::getDocumentLanguages(
         }
     }
 
-    const SwTextFormatColls *pColls = pDoc->GetTextFormatColls();
+    const SwTextFormatColls *pColls = rDoc.GetTextFormatColls();
     for (size_t i = 0; i < pColls->size(); ++i)
     {
         const SwAttrSet &rAttrSet = (*pColls)[i]->GetAttrSet();
@@ -3954,7 +3956,7 @@ uno::Sequence< lang::Locale > SAL_CALL 
SwXTextDocument::getDocumentLanguages(
     for (IStyleAccess::SwAutoStyleFamily i : aFam)
     {
         std::vector< std::shared_ptr<SfxItemSet> > rStyles;
-        pDoc->GetIStyleAccess().getAllStyles(rStyles, i);
+        rDoc.GetIStyleAccess().getAllStyles(rStyles, i);
         while (!rStyles.empty())
         {
             std::shared_ptr<SfxItemSet> pStyle = rStyles.back();

Reply via email to