sw/inc/strings.hrc                           |    1 
 sw/qa/extras/ooxmlexport/data/tdf125338.docm |binary
 sw/qa/extras/ooxmlexport/ooxmlexport9.cxx    |   10 ++++++-
 sw/source/filter/ww8/docxexport.cxx          |    3 +-
 sw/source/filter/ww8/docxexportfilter.cxx    |   35 +++++++++++++++++++++++----
 5 files changed, 43 insertions(+), 6 deletions(-)

New commits:
commit cbca5617387aaa39c9ee86272166bb3df7874b25
Author:     offtkp <parisop...@gmail.com>
AuthorDate: Wed Dec 14 22:21:34 2022 +0200
Commit:     Justin Luth <jl...@mail.com>
CommitDate: Tue Dec 20 14:46:21 2022 +0000

    tdf#125338 Do not export macros when saving as .docx
    
    Docx files that have macros should be saved as .docm. Now displays a
    warning that macros won't be saved if exporting a .docm as a .docx and
    doesn't export them.
    
    Change-Id: I178855baa6c0a7d32fd9b00c83a817230b33a1a1
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/144197
    Tested-by: Jenkins
    Reviewed-by: Justin Luth <jl...@mail.com>

diff --git a/sw/inc/strings.hrc b/sw/inc/strings.hrc
index afd0c2873fad..425d31f88d30 100644
--- a/sw/inc/strings.hrc
+++ b/sw/inc/strings.hrc
@@ -331,6 +331,7 @@
 #define STR_SOUTH                               NC_("STR_SOUTH", "South")
 #define STR_SUM                                 NC_("STR_SUM", "Sum")
 #define STR_INVALID_AUTOFORMAT_NAME             
NC_("STR_INVALID_AUTOFORMAT_NAME", "You have entered an invalid name.\nThe 
desired AutoFormat could not be created. \nTry again using a different name.")
+#define STR_CANT_SAVE_MACROS                    NC_("STR_CANT_SAVE_MACROS", 
"This document has macros, but macros will not be saved in this file format.")
 #define STR_NUMERIC                             NC_("STR_NUMERIC", "Numeric")
 #define STR_ROW                                 NC_("STR_ROW", "Rows")
 #define STR_COL                                 NC_("STR_COL", "Column")
diff --git a/sw/qa/extras/ooxmlexport/data/tdf125338.docm 
b/sw/qa/extras/ooxmlexport/data/tdf125338.docm
new file mode 100644
index 000000000000..44e943531d30
Binary files /dev/null and b/sw/qa/extras/ooxmlexport/data/tdf125338.docm differ
diff --git a/sw/qa/extras/ooxmlexport/ooxmlexport9.cxx 
b/sw/qa/extras/ooxmlexport/ooxmlexport9.cxx
index 26a7e7167667..a066f88ba13b 100644
--- a/sw/qa/extras/ooxmlexport/ooxmlexport9.cxx
+++ b/sw/qa/extras/ooxmlexport/ooxmlexport9.cxx
@@ -129,7 +129,7 @@ DECLARE_OOXMLEXPORT_TEST(testTdf109063, "tdf109063.docx")
     CPPUNIT_ASSERT_EQUAL(0, getShapes());
 }
 
-CPPUNIT_TEST_FIXTURE(Test, testTdf108269)
+CPPUNIT_TEST_FIXTURE(DocmTest, testTdf108269)
 {
     loadAndReload("tdf108269.docm");
     uno::Reference<packages::zip::XZipFileAccess2> xNameAccess = 
packages::zip::ZipFileAccess::createWithURL(comphelper::getComponentContext(m_xSFactory),
 maTempFile.GetURL());
@@ -139,6 +139,14 @@ CPPUNIT_TEST_FIXTURE(Test, testTdf108269)
     CPPUNIT_ASSERT(xNameAccess->hasByName("word/vbaData.xml"));
 }
 
+CPPUNIT_TEST_FIXTURE(Test, testTdf125338)
+{
+    loadAndSave("tdf125338.docm");
+    uno::Reference<packages::zip::XZipFileAccess2> xNameAccess = 
packages::zip::ZipFileAccess::createWithURL(comphelper::getComponentContext(m_xSFactory),
 maTempFile.GetURL());
+    // docm files should not retain macros when saved as docx
+    CPPUNIT_ASSERT(!xNameAccess->hasByName("word/vbaProject.bin"));
+}
+
 DECLARE_OOXMLEXPORT_TEST(testTdf92045, "tdf92045.docx")
 {
     // This was true, <w:effect w:val="none"/> resulted in setting the 
blinking font effect.
diff --git a/sw/source/filter/ww8/docxexport.cxx 
b/sw/source/filter/ww8/docxexport.cxx
index ea59775eda15..f060773576eb 100644
--- a/sw/source/filter/ww8/docxexport.cxx
+++ b/sw/source/filter/ww8/docxexport.cxx
@@ -549,7 +549,8 @@ ErrCode DocxExport::ExportDocument_Impl()
 
     WriteEmbeddings();
 
-    WriteVBA();
+    if (m_bDocm)
+        WriteVBA();
 
     m_aLinkedTextboxesHelper.clear();   //final cleanup
     m_pStyles.reset();
diff --git a/sw/source/filter/ww8/docxexportfilter.cxx 
b/sw/source/filter/ww8/docxexportfilter.cxx
index 08308ca9398c..5ffd32f4c25b 100644
--- a/sw/source/filter/ww8/docxexportfilter.cxx
+++ b/sw/source/filter/ww8/docxexportfilter.cxx
@@ -63,6 +63,37 @@ bool DocxExportFilter::exportDocument()
         pViewShell->GetPostItMgr()->UpdateDataOnActiveSidebarWin();
     }
 
+    OUString aFilterName;
+    auto& rMediaDescriptor = getMediaDescriptor();
+    rMediaDescriptor[utl::MediaDescriptor::PROP_FILTERNAME] >>= aFilterName;
+    bool bDocm = aFilterName.endsWith("VBA");
+
+    if (!bDocm)
+    {
+        // Check whether application is in headless mode
+        if (!Application::IsHeadlessModeEnabled())
+        {
+            uno::Reference<document::XStorageBasedDocument> 
xStorageBasedDocument(
+                pDoc->GetDocShell()->GetBaseModel(), uno::UNO_QUERY);
+            if (xStorageBasedDocument.is())
+            {
+                uno::Reference<embed::XStorage> xDocumentStorage =
+                    xStorageBasedDocument->getDocumentStorage();
+                if (xDocumentStorage.is() && 
xDocumentStorage->hasByName(u"_MS_VBA_Macros"))
+                {
+                    // Let user know that macros won't be saved in this format
+                    std::unique_ptr<weld::MessageDialog> 
xBox(Application::CreateMessageDialog(
+                            nullptr,
+                            VclMessageType::Warning, VclButtonsType::OkCancel,
+                            SwResId(STR_CANT_SAVE_MACROS))
+                    );
+                    if (xBox->run() == RET_CANCEL)
+                        return false;
+                }
+            }
+        }
+    }
+
     // get SwPaM*
     // FIXME so far we get SwPaM for the entire document; probably we should
     // be able to output just the selection as well - though no idea how to
@@ -75,10 +106,6 @@ bool DocxExportFilter::exportDocument()
     pCurPam->SetMark();
     *pCurPam->GetPoint() = *aPam.Start();
 
-    OUString aFilterName;
-    getMediaDescriptor()[utl::MediaDescriptor::PROP_FILTERNAME] >>= 
aFilterName;
-    bool bDocm = aFilterName.endsWith("VBA");
-
     // export the document
     // (in a separate block so that it's destructed before the commit)
     {

Reply via email to