--- basic.bak/source/classes/sb.cxx	2006-09-18 09:58:54.000000000 +0100
+++ basic/source/classes/sb.cxx	2006-09-26 11:31:12.000000000 +0100
@@ -701,11 +701,10 @@ SbModule* SbClassFactory::FindClass( con
 	return pMod;
 }
 
-
 ////////////////////////////////////////////////////////////////////////////
 
-StarBASIC::StarBASIC( StarBASIC* p )
-	: SbxObject( String( RTL_CONSTASCII_USTRINGPARAM("StarBASIC") ) )
+StarBASIC::StarBASIC( StarBASIC* p, BOOL bIsDocBasic )
+	: SbxObject( String( RTL_CONSTASCII_USTRINGPARAM("StarBASIC") ) ), bDocBasic( bIsDocBasic )
 {
 	SetParent( p );
 	pLibInfo = NULL;
--- basic.bak/inc/sbstar.hxx	2006-09-18 09:58:46.000000000 +0100
+++ basic/inc/sbstar.hxx	2006-09-26 11:31:12.000000000 +0100
@@ -85,6 +85,7 @@ class SB_DLLPUBLIC StarBASIC : public Sb
 	Link			aBreakHdl;			// Breakpoint-Handler
 	BOOL			bNoRtl;				// TRUE: RTL nicht durchsuchen
 	BOOL			bBreak;				// TRUE: Break, sonst Step
+	BOOL			bDocBasic;			
 	BasicLibInfo*	pLibInfo;			// Infoblock fuer Basic-Manager
 	SbLanguageMode	eLanguageMode;		// LanguageMode des Basic-Objekts
 protected:
@@ -110,7 +111,7 @@ public:
 	SBX_DECL_PERSIST_NODATA(SBXCR_SBX,SBXID_BASIC,1);
 	TYPEINFO();
 
-	StarBASIC( StarBASIC* pParent = NULL );
+	StarBASIC( StarBASIC* pParent = NULL, BOOL bIsDocBasic = FALSE );
 
 	// #51727 SetModified ueberladen, damit der Modified-
 	// Zustand nicht an den Parent weitergegeben wird.
@@ -209,6 +210,7 @@ public:
 	static void StaticEnableReschedule( BOOL bReschedule );
 
 	SbxObjectRef getRTL( void ) { return pRtl; }
+	BOOL IsDocBasic() { return bDocBasic; }
 };
 
 
--- basic.bak/inc/basmgr.hxx	2006-09-18 09:58:46.000000000 +0100
+++ basic/inc/basmgr.hxx	2006-09-26 11:16:09.000000000 +0100
@@ -165,6 +165,7 @@ private:
 	String				aName;
 	String				maStorageName;
 	BOOL				bBasMgrModified;
+	BOOL				mbDocMgr;
 
 	BasicManagerImpl*	mpImpl;
 
@@ -189,8 +190,8 @@ protected:
 
 public:
 					TYPEINFO();
-                    BasicManager( SotStorage& rStorage, const String& rBaseURL, StarBASIC* pParentFromStdLib = NULL, String* pLibPath = NULL );
-					BasicManager( StarBASIC* pStdLib, String* pLibPath = NULL );
+                    BasicManager( SotStorage& rStorage, const String& rBaseURL, StarBASIC* pParentFromStdLib = NULL, String* pLibPath = NULL, BOOL bDocMgr = FALSE );
+					BasicManager( StarBASIC* pStdLib, String* pLibPath = NULL, BOOL bDocMgr = FALSE );
                     ~BasicManager();
 
 	void			SetStorageName( const String& rName )	{ maStorageName = rName; }
--- basic.bak/source/basmgr/basmgr.cxx	2006-06-19 18:38:25.000000000 +0100
+++ basic/source/basmgr/basmgr.cxx	2006-09-26 11:52:18.000000000 +0100
@@ -677,7 +677,7 @@ void BasicLibInfo::CalcRelStorageName( c
 		SetRelStorageName( String() );
 }
 
-BasicManager::BasicManager( SotStorage& rStorage, const String& rBaseURL, StarBASIC* pParentFromStdLib, String* pLibPath )
+BasicManager::BasicManager( SotStorage& rStorage, const String& rBaseURL, StarBASIC* pParentFromStdLib, String* pLibPath, BOOL bDocMgr ) : mbDocMgr( bDocMgr )
 {
 	DBG_CTOR( BasicManager, 0 );
 
@@ -881,7 +881,7 @@ void BasicManager::SetLibraryContainerIn
 }
 
 
-BasicManager::BasicManager( StarBASIC* pSLib, String* pLibPath )
+BasicManager::BasicManager( StarBASIC* pSLib, String* pLibPath, BOOL bDocMgr ): mbDocMgr( bDocMgr )
 {
 	DBG_CTOR( BasicManager, 0 );
 	Init();
@@ -974,7 +974,7 @@ void BasicManager::ImpMgrNotLoaded( cons
 void BasicManager::ImpCreateStdLib( StarBASIC* pParentFromStdLib )
 {
 	BasicLibInfo* pStdLibInfo = CreateLibInfo();
-	StarBASIC* pStdLib = new StarBASIC( pParentFromStdLib );
+	StarBASIC* pStdLib = new StarBASIC( pParentFromStdLib, mbDocMgr );
 	pStdLibInfo->SetLib( pStdLib );
 	pStdLib->SetName( String::CreateFromAscii(szStdLibName) );
 	pStdLibInfo->SetLibName( String::CreateFromAscii(szStdLibName) );
@@ -2264,7 +2264,7 @@ StarBASIC* BasicManager::CreateLibForLib
 		return 0;
 
 	BasicLibInfo* pLibInfo = CreateLibInfo();
-	StarBASIC* pNew = new StarBASIC( GetStdLib() );
+	StarBASIC* pNew = new StarBASIC( GetStdLib(), mbDocMgr );
 	GetStdLib()->Insert( pNew );
 	pNew->SetFlag( SBX_EXTSEARCH | SBX_DONTSTORE );
 	pLibInfo->SetLib( pNew );
--- sfx2.bak/source/doc/objxtor.cxx	2006-06-19 23:30:36.000000000 +0100
+++ sfx2/source/doc/objxtor.cxx	2006-09-26 12:14:54.000000000 +0100
@@ -678,7 +678,7 @@ void SfxObjectShell::InitBasicManager_Im
 		SotStorageRef xDummyStor = new SotStorage( ::rtl::OUString() );
         pImp->pBasicMgr = pBasicManager = new BasicManager( *xDummyStor, String() /* TODO/LATER: xStorage */,
 															pAppBasic,
-															&aAppBasicDir );
+															&aAppBasicDir, TRUE );
 		if ( pImp->pBasicMgr->HasErrors() )
 		{
 			// handle errors
@@ -705,7 +705,7 @@ void SfxObjectShell::InitBasicManager_Im
 		// create new BASIC-manager
 		StarBASIC *pBas = new StarBASIC(pAppBasic);
 		pBas->SetFlag( SBX_EXTSEARCH );
-		pImp->pBasicMgr = pBasicManager = new BasicManager( pBas );
+		pImp->pBasicMgr = pBasicManager = new BasicManager( pBas, NULL, TRUE );
 	}
 
 	// Basic container
--- basic.bak/source/comp/dim.cxx	2006-09-18 09:58:54.000000000 +0100
+++ basic/source/comp/dim.cxx	2006-09-26 17:25:17.000000000 +0100
@@ -197,9 +197,15 @@ void SbiParser::DefVar( SbiOpcode eOp, B
 	if( eCurTok == PUBLIC || eCurTok == GLOBAL )
     {
 		bSwitchPool = TRUE;		// im richtigen Moment auf globalen Pool schalten
+
     	if( eCurTok == GLOBAL )
             bPersistantGlobal = TRUE;
     }
+	// behavior in VBA is that a module scope variable's lifetime is 
+	// tied to the document. e.g. a module scope variable is global
+   	if(  GetBasic()->IsDocBasic() && bVBASupportOn && !pProc ) 
+		bPersistantGlobal = TRUE;
+
 	// PRIVATE ist Synonym fuer DIM
 	// _CONST_?
 	BOOL bConst = FALSE;
@@ -331,7 +337,7 @@ void SbiParser::DefVar( SbiOpcode eOp, B
 			{
 				case SbGLOBAL:	eOp2 = bPersistantGlobal ? _GLOBAL_P : _GLOBAL; 
                                 goto global;
-				case SbPUBLIC:	eOp2 = _PUBLIC;
+				case SbPUBLIC:	eOp2 = bPersistantGlobal ? _PUBLIC_P : _PUBLIC ;
 								// AB 9.7.97, #40689, kein eigener Opcode mehr
 								/*
 								if( bStatic )
--- basic.bak/source/inc/runtime.hxx	2006-09-18 09:58:54.000000000 +0100
+++ basic/source/inc/runtime.hxx	2006-09-26 17:29:02.000000000 +0100
@@ -460,7 +460,7 @@ class SbiRuntime
 	bool checkClass_Impl( const SbxVariableRef& refVal, const String& aClass, bool bRaiseErrors );
 	void StepCLOSE( UINT32 ),   StepPRCHAR( UINT32 ),   StepARGTYP( UINT32 );
 	// Alle Opcodes mit zwei Operanden
-	void StepRTL( UINT32, UINT32 ),		StepPUBLIC( UINT32, UINT32 );		
+	void StepRTL( UINT32, UINT32 ),		StepPUBLIC( UINT32, UINT32 ), StepPUBLIC_P( UINT32, UINT32 ) ;		
 	void StepPUBLIC_Impl( UINT32, UINT32, bool bUsedForClassModule );
 	void StepFIND( UINT32, UINT32 ),    StepELEM( UINT32, UINT32 );
 	void StepGLOBAL( UINT32, UINT32 ),  StepLOCAL( UINT32, UINT32 );
--- basic.bak/source/runtime/runtime.cxx	2006-09-18 09:58:54.000000000 +0100
+++ basic/source/runtime/runtime.cxx	2006-09-26 17:27:22.000000000 +0100
@@ -210,6 +210,7 @@ SbiRuntime::pStep2 SbiRuntime::aStep2[] 
 	MEMBER(SbiRuntime::StepFIND_G),    	// Sucht globale Variable mit Spezialbehandlung wegen _GLOBAL_P
 	MEMBER(SbiRuntime::StepDCREATE_REDIMP), // Objekt-Array redimensionieren (+StringID+StringID)
 	MEMBER(SbiRuntime::StepFIND_CM),    // Search inside a class module (CM) to enable global search in time
+	MEMBER(SbiRuntime::StepPUBLIC_P),    // Search inside a class module (CM) to enable global search in time
 };
 
 
--- basic.bak/source/runtime/step2.cxx	2006-09-18 09:58:54.000000000 +0100
+++ basic/source/runtime/step2.cxx	2006-09-26 17:25:08.000000000 +0100
@@ -1081,6 +1081,14 @@ void SbiRuntime::StepPUBLIC( UINT32 nOp1
 	StepPUBLIC_Impl( nOp1, nOp2, false );
 }
 
+void SbiRuntime::StepPUBLIC_P( UINT32 nOp1, UINT32 nOp2 )
+{
+    // Creates module variable that isn't reinitialised when
+    // between invocations ( for VBASupport & document basic only )
+    if( pMod->pImage->bFirstInit )
+	StepPUBLIC( nOp1, nOp2 );
+}
+
 // Einrichten einer globalen Variablen (+StringID+Typ)
 
 void SbiRuntime::StepGLOBAL( UINT32 nOp1, UINT32 nOp2 )
--- basic.bak/source/inc/opcodes.hxx	2006-09-18 09:58:54.000000000 +0100
+++ basic/source/inc/opcodes.hxx	2006-09-26 17:25:46.000000000 +0100
@@ -165,6 +165,7 @@ enum SbiOpcode {
 	_FIND_G,        	// Sucht globale Variable mit Spezialbehandlung wegen _GLOBAL_P
     _DCREATE_REDIMP,	// Objekt-Array redimensionieren (+StringId+StringID)
 	_FIND_CM,        	// Search inside a class module (CM) to enable global search in time
+	_PUBLIC_P, 			//  Module global Variable (persisted between calls)(+StringID+Typ)
 	SbOP2_END
 
 };
