tdf#148806 doc vba: only autoOpen PUBLIC macros

Note: this should NOT apply to Document_Open which normally
is a private subroutine and runs fine as private.

I tested and it DOES apply to all three version of AutoOpen:
-ThisDocument.AutoOpen,
-<AnyModule>.AutoOpen,
-AutoOpen.Main

Note: this is different from Excel.
Private Auto_Open runs just fine there.

Change-Id: If10c8c90c35275c2b14dc2e15fb357674fc580b8
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/141114
Tested-by: Justin Luth <jluth@mail.com>
Reviewed-by: Justin Luth <jluth@mail.com>
diff --git a/filter/source/msfilter/msvbahelper.cxx b/filter/source/msfilter/msvbahelper.cxx
index 674530d..a4e9545 100644
--- a/filter/source/msfilter/msvbahelper.cxx
+++ b/filter/source/msfilter/msvbahelper.cxx
@@ -171,13 +171,15 @@ static SfxObjectShell* findShellForUrl( const OUString& sMacroURLOrPath )
// sMod can be empty ( but we really need the library to search in )
// 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 )
static bool hasMacro(SfxObjectShell const* pShell, const OUString& sLibrary, OUString& sMod,
                     const OUString& sMacro, bool bOnlyPublic)
{
#if !HAVE_FEATURE_SCRIPTING
    (void) pShell;
    (void) sLibrary;
    (void) sMod;
    (void) sMacro;
    (void) bOnlyPublic;
#else
    if (sLibrary.isEmpty() || sMacro.isEmpty())
        return false;
@@ -202,7 +204,7 @@ static bool hasMacro( SfxObjectShell const * pShell, const OUString& sLibrary, O
        if (!pModule)
            return false;
        SbMethod* pMeth = pModule->FindMethod(sMacro, SbxClassType::Method);
        return pMeth;
        return pMeth && (!bOnlyPublic || !pMeth->IsSet(SbxFlagBits::Private));
    }

    for (auto const& rModuleRef : pBasic->GetModules())
@@ -210,6 +212,8 @@ static bool hasMacro( SfxObjectShell const * pShell, const OUString& sLibrary, O
        SbMethod* pMeth = rModuleRef->FindMethod(sMacro, SbxClassType::Method);
        if (pMeth)
        {
            if (bOnlyPublic && pMeth->IsSet(SbxFlagBits::Private))
                continue;
            sMod = rModuleRef->GetName();
            return true;
        }
@@ -257,7 +261,9 @@ static void parseMacro( const OUString& sMacro, OUString& sContainer, OUString& 

#endif

OUString resolveVBAMacro( SfxObjectShell const * pShell, const OUString& rLibName, const OUString& rModuleName, const OUString& rMacroName )
OUString resolveVBAMacro(SfxObjectShell const* pShell, const OUString& rLibName,
                         const OUString& rModuleName, const OUString& rMacroName,
                         bool bOnlyPublic)
{
#if !HAVE_FEATURE_SCRIPTING
    (void) pShell;
@@ -269,7 +275,7 @@ OUString resolveVBAMacro( SfxObjectShell const * pShell, const OUString& rLibNam
    {
        OUString aLibName = rLibName.isEmpty() ?  getDefaultProjectName( pShell ) : rLibName ;
        OUString aModuleName = rModuleName;
        if( hasMacro( pShell, aLibName, aModuleName, rMacroName ) )
        if (hasMacro( pShell, aLibName, aModuleName, rMacroName, bOnlyPublic))
            return aLibName + "." + aModuleName + "." + rMacroName;
    }
#endif
@@ -440,7 +446,7 @@ MacroResolvedInfo resolveVBAMacro( SfxObjectShell* pShell, const OUString& Macro

    for (auto const& search : sSearchList)
    {
        aRes.mbFound = hasMacro( pShell, search, sModule, sProcedure );
        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 1568970..c1ad7fd 100644
--- a/include/filter/msfilter/msvbahelper.hxx
+++ b/include/filter/msfilter/msvbahelper.hxx
@@ -58,7 +58,9 @@ struct MSFILTER_DLLPUBLIC MacroResolvedInfo
MSFILTER_DLLPUBLIC OUString makeMacroURL( std::u16string_view sMacroName );
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 );
MSFILTER_DLLPUBLIC OUString resolveVBAMacro(SfxObjectShell const* pShell, const OUString& rLibName,
                                            const OUString& rModuleName,
                                            const OUString& rMacroName, bool bOnlyPublic);
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 cbe8ca7..e9bd0f4 100644
--- a/vbahelper/source/vbahelper/vbaeventshelperbase.cxx
+++ b/vbahelper/source/vbahelper/vbaeventshelperbase.cxx
@@ -369,8 +369,18 @@ VbaEventsHelperBase::ModulePathMap& VbaEventsHelperBase::updateModulePathMap( co
        const EventHandlerInfo& rInfo = rEventInfo.second;
        if( rInfo.mnModuleType == nModuleType )
        {
            // Only in Word, Auto* only runs if defined as Public, not Private.
            const bool bOnlyPublic
                = getImplementationName() == "SwVbaEventsHelper"
                  && (rInfo.mnEventId == css::script::vba::VBAEventId::DOCUMENT_AUTO_NEW
                      || rInfo.mnEventId == css::script::vba::VBAEventId::AUTO_NEW
                      || rInfo.mnEventId == css::script::vba::VBAEventId::DOCUMENT_AUTO_OPEN
                      || rInfo.mnEventId == css::script::vba::VBAEventId::AUTO_OPEN
                      || rInfo.mnEventId == css::script::vba::VBAEventId::DOCUMENT_AUTO_CLOSE
                      || rInfo.mnEventId == css::script::vba::VBAEventId::AUTO_CLOSE);

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