tdf#148806 tdf#151393 xls vba: no Auto_Open from ThisWorksheet

Unlike Word, ThisWorksheet cannot hold auto-running
subroutines for Open/Close/New.

This fixes a LO 7.4 regression caused by
commit beb6c62e990599d91ac5d9183164c94d269027d3.

Change-Id: Idb8f72775d9392b306cb924ee776821272b12f3b
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/141127
Tested-by: Jenkins
Reviewed-by: Justin Luth <jluth@mail.com>
diff --git a/filter/source/msfilter/msvbahelper.cxx b/filter/source/msfilter/msvbahelper.cxx
index 59d34ca..c7e8413 100644
--- a/filter/source/msfilter/msvbahelper.cxx
+++ b/filter/source/msfilter/msvbahelper.cxx
@@ -172,7 +172,7 @@ static SfxObjectShell* findShellForUrl( const OUString& sMacroURLOrPath )
// if sMod is empty and a macro is found then sMod is updated
// if sMod is empty, only standard modules will be searched (no class, document, form modules)
static bool hasMacro(SfxObjectShell const* pShell, const OUString& sLibrary, OUString& sMod,
                     const OUString& sMacro, bool bOnlyPublic)
                     const OUString& sMacro, bool bOnlyPublic, const OUString& sSkipModule)
{
#if !HAVE_FEATURE_SCRIPTING
    (void) pShell;
@@ -180,6 +180,7 @@ static bool hasMacro(SfxObjectShell const* pShell, const OUString& sLibrary, OUS
    (void) sMod;
    (void) sMacro;
    (void) bOnlyPublic;
    (void) sSkipModule;
#else
    if (sLibrary.isEmpty() || sMacro.isEmpty())
        return false;
@@ -212,7 +213,8 @@ static bool hasMacro(SfxObjectShell const* pShell, const OUString& sLibrary, OUS
        SbMethod* pMeth = rModuleRef->FindMethod(sMacro, SbxClassType::Method);
        if (pMeth)
        {
            if (bOnlyPublic && pMeth->IsSet(SbxFlagBits::Private))
            if ((bOnlyPublic && pMeth->IsSet(SbxFlagBits::Private))
                || rModuleRef->GetName() == sSkipModule)
                continue;
            sMod = rModuleRef->GetName();
            return true;
@@ -263,7 +265,7 @@ static void parseMacro( const OUString& sMacro, OUString& sContainer, OUString& 

OUString resolveVBAMacro(SfxObjectShell const* pShell, const OUString& rLibName,
                         const OUString& rModuleName, const OUString& rMacroName,
                         bool bOnlyPublic)
                         bool bOnlyPublic, const OUString& sSkipModule)
{
#if !HAVE_FEATURE_SCRIPTING
    (void) pShell;
@@ -271,12 +273,13 @@ OUString resolveVBAMacro(SfxObjectShell const* pShell, const OUString& rLibName,
    (void) rModuleName;
    (void) rMacroName;
    (void) bOnlyPublic;
    (void) sSkipModule;
#else
    if( pShell )
    {
        OUString aLibName = rLibName.isEmpty() ?  getDefaultProjectName( pShell ) : rLibName ;
        OUString aModuleName = rModuleName;
        if (hasMacro( pShell, aLibName, aModuleName, rMacroName, bOnlyPublic))
        if (hasMacro(pShell, aLibName, aModuleName, rMacroName, bOnlyPublic, sSkipModule))
            return aLibName + "." + aModuleName + "." + rMacroName;
    }
#endif
@@ -447,7 +450,7 @@ MacroResolvedInfo resolveVBAMacro( SfxObjectShell* pShell, const OUString& Macro

    for (auto const& search : sSearchList)
    {
        aRes.mbFound = hasMacro(pShell, search, sModule, sProcedure, /*bOnlyPublic=*/false);
        aRes.mbFound = hasMacro(pShell, search, sModule, sProcedure, /*bOnlyPublic=*/false, "");
        if ( aRes.mbFound )
        {
            sContainer = search;
diff --git a/include/filter/msfilter/msvbahelper.hxx b/include/filter/msfilter/msvbahelper.hxx
index c1ad7fd..90f1f8a 100644
--- a/include/filter/msfilter/msvbahelper.hxx
+++ b/include/filter/msfilter/msvbahelper.hxx
@@ -60,7 +60,8 @@ MSFILTER_DLLPUBLIC OUString extractMacroName( std::u16string_view rMacroUrl );
MSFILTER_DLLPUBLIC OUString getDefaultProjectName( SfxObjectShell const * pShell );
MSFILTER_DLLPUBLIC OUString resolveVBAMacro(SfxObjectShell const* pShell, const OUString& rLibName,
                                            const OUString& rModuleName,
                                            const OUString& rMacroName, bool bOnlyPublic);
                                            const OUString& rMacroName, bool bOnlyPublic,
                                            const OUString& sSkipModule);
MSFILTER_DLLPUBLIC MacroResolvedInfo resolveVBAMacro( SfxObjectShell* pShell, const OUString& rMacroName, bool bSearchGlobalTemplates = false );
MSFILTER_DLLPUBLIC bool executeMacro( SfxObjectShell* pShell, const OUString& sMacroName, css::uno::Sequence< css::uno::Any >& aArgs, css::uno::Any& aRet, const css::uno::Any& aCaller );
/// @throws css::uno::RuntimeException
diff --git a/vbahelper/source/vbahelper/vbaeventshelperbase.cxx b/vbahelper/source/vbahelper/vbaeventshelperbase.cxx
index e9bd0f4..7d6e1bb 100644
--- a/vbahelper/source/vbahelper/vbaeventshelperbase.cxx
+++ b/vbahelper/source/vbahelper/vbaeventshelperbase.cxx
@@ -364,11 +364,32 @@ VbaEventsHelperBase::ModulePathMap& VbaEventsHelperBase::updateModulePathMap( co
    sal_Int32 nModuleType = getModuleType( rModuleName );
    // search for all event handlers
    ModulePathMap& rPathMap = maEventPaths[ rModuleName ];

    // Use WORKBOOK_OPEN as a way to get the codename for ThisWorkbook
    OUString sThisWorkbook;
    if (getImplementationName() == "ScVbaEventsHelper")
    {
        EventHandlerInfo& rThisWorksheetInfo
            = maEventInfos[css::script::vba::VBAEventId::WORKBOOK_OPEN];
        css::uno::Sequence<css::uno::Any> aNoArgs;
        sThisWorkbook = implGetDocumentModuleName(rThisWorksheetInfo, aNoArgs);
    }

    for( const auto& rEventInfo : maEventInfos )
    {
        const EventHandlerInfo& rInfo = rEventInfo.second;
        if( rInfo.mnModuleType == nModuleType )
        {
            OUString sSkipModule;
            // Only in Calc, ignore Auto_* in ThisWorkbook
            if (getImplementationName() == "ScVbaEventsHelper"
                && (rInfo.mnEventId == css::script::vba::VBAEventId::AUTO_NEW
                    || rInfo.mnEventId == css::script::vba::VBAEventId::AUTO_OPEN
                    || rInfo.mnEventId == css::script::vba::VBAEventId::AUTO_CLOSE))
            {
                sSkipModule = sThisWorkbook;
            }

            // Only in Word, Auto* only runs if defined as Public, not Private.
            const bool bOnlyPublic
                = getImplementationName() == "SwVbaEventsHelper"
@@ -380,7 +401,7 @@ VbaEventsHelperBase::ModulePathMap& VbaEventsHelperBase::updateModulePathMap( co
                      || rInfo.mnEventId == css::script::vba::VBAEventId::AUTO_CLOSE);

            OUString sName = resolveVBAMacro(mpShell, maLibraryName, rModuleName,
                                             rInfo.maMacroName, bOnlyPublic);
                                             rInfo.maMacroName, bOnlyPublic, sSkipModule);
            // Only in Word (with lowest priority), an Auto* module can execute a "Public Sub Main"
            if (sName.isEmpty() && rModuleName.isEmpty()
                && getImplementationName() == "SwVbaEventsHelper")
@@ -390,7 +411,7 @@ VbaEventsHelperBase::ModulePathMap& VbaEventsHelperBase::updateModulePathMap( co
                    || rInfo.mnEventId == css::script::vba::VBAEventId::AUTO_CLOSE)
                {
                    sName = resolveVBAMacro(mpShell, maLibraryName, rInfo.maMacroName, "Main",
                                            bOnlyPublic);
                                            bOnlyPublic, sSkipModule);
                }
            }
            rPathMap[rInfo.mnEventId] = sName;