--- sc/inc/scextopt.hxx	2005-09-08 18:53:33.000000000 +0100
+++ sc/inc/scextopt.hxx	2007-07-19 15:11:29.000000000 +0100
@@ -141,6 +141,8 @@ public:
     const String&       GetCodeName( size_t nIdx ) const;
     /** Appends a codename for a sheet. */
     void                AppendCodeName( const String& rCodeName );
+    void                SetCodeName( const String& rCodeName, size_t nIdx );
+    void                DeleteCodeName( size_t nIdx );
 
 private:
     ::std::auto_ptr< ScExtDocOptionsImpl > mxImpl;
--- sc/source/ui/view/scextopt.cxx	2006-07-21 16:07:47.000000000 +0100
+++ sc/source/ui/view/scextopt.cxx	2007-07-19 16:20:54.000000000 +0100
@@ -140,14 +140,16 @@ void ScExtTabSettingsCont::CopyFromMap( 
 
 // ============================================================================
 
+typedef ::std::map< size_t, String > TabCodeNameMap;
 /** Implementation struct for ScExtDocOptions containing all members. */
 struct ScExtDocOptionsImpl
 {
-    typedef ::std::vector< String > StringVec;
 
     ScExtDocSettings    maDocSett;          /// Global document settings.
     ScExtTabSettingsCont maTabSett;         /// Settings for all sheets.
-    StringVec           maCodeNames;        /// Codenames for all sheets (VBA module names).
+//    StringVec           maCodeNames;        /// Codenames for all sheets (VBA module names).
+    TabCodeNameMap      maCodeNames;        /// Codenames for all sheets (VBA module names).
+
     bool                mbChanged;          /// Use only if something has been changed.
 
     explicit            ScExtDocOptionsImpl();
@@ -223,8 +225,22 @@ const String& ScExtDocOptions::GetCodeNa
 
 void ScExtDocOptions::AppendCodeName( const String& rCodeName )
 {
-    mxImpl->maCodeNames.push_back( rCodeName );
+    mxImpl->maCodeNames[ mxImpl->maCodeNames.size() ] = rCodeName;
 }
 
+
+void ScExtDocOptions::SetCodeName( const String& rCodeName, size_t nIdx )
+{
+    // No holes allowed
+    DBG_ASSERT( nIdx <= GetCodeNameCount(), "ScExtDocOptions::GetCodeName - invalid index" );
+    mxImpl->maCodeNames[ nIdx ] = rCodeName;
+}
+
+void ScExtDocOptions::DeleteCodeName( size_t nIdx )
+{
+	TabCodeNameMap::iterator it = mxImpl->maCodeNames.find( nIdx );
+	if ( it != mxImpl->maCodeNames.end() )
+		mxImpl->maCodeNames.erase( it );
+}
 // ============================================================================
 
--- sc/source/core/data/document.cxx	2007-09-06 14:47:12.000000000 +0800
+++ sc/source/core/data/document.cxx	2007-09-06 14:48:36.000000000 +0800
@@ -105,6 +105,7 @@
 #endif
 #include "autonamecache.hxx"
 #include "bcaslot.hxx"
+#include "scextopt.hxx"
 
 struct ScDefaultAttr
 {
@@ -291,6 +292,7 @@ BOOL ScDocument::InsertTab( SCTAB nPos, 
 {
 	SCTAB	nTabCount = GetTableCount();
 	BOOL	bValid = ValidTab(nTabCount);
+	ScExtDocOptions* pExtOptions = GetExtDocOptions();
 	if ( !bExternalDocument )	// sonst rName == "'Doc'!Tab", vorher pruefen
 		bValid = (bValid && ValidNewTabName(rName));
 	if (bValid)
@@ -298,6 +300,12 @@ BOOL ScDocument::InsertTab( SCTAB nPos, 
 		if (nPos == SC_TAB_APPEND || nPos == nTabCount)
 		{
 			pTab[nTabCount] = new ScTable(this, nTabCount, rName);
+			// if we are mapping codenames then a new tab always has has
+			// codename = sheetname
+			if ( pExtOptions && ( pExtOptions->GetCodeNameCount() > 0 ) )
+			{
+					pExtOptions->SetCodeName( rName, nTabCount );
+			}
 			++nMaxTableNumber;
 			if ( bExternalDocument )
 				pTab[nTabCount]->SetVisible( FALSE );
@@ -328,10 +336,30 @@ BOOL ScDocument::InsertTab( SCTAB nPos, 
 				for (i = 0; i <= MAXTAB; i++)
 					if (pTab[i])
 						pTab[i]->UpdateInsertTab(nPos);
+
 				for (i = nTabCount; i > nPos; i--)
+				{
 					pTab[i] = pTab[i - 1];
+					if ( pTab[i] && pExtOptions && ( pExtOptions->GetCodeNameCount() > 0 ) )
+					{
+						String sCodeName = pExtOptions->GetCodeName( i - 1 );
+						if ( sCodeName.Len() )
+						{
+							OSL_TRACE("Associating codeName %s with tab %d", rtl::OUStringToOString( sCodeName, RTL_TEXTENCODING_UTF8 ).getStr(), i );
+							pExtOptions->SetCodeName( sCodeName, i );
+						}
+					}
+				}
+
+				if ( pExtOptions && ( pExtOptions->GetCodeNameCount() > 0 ) )
+				{
+						OSL_TRACE("Inserting codeName %s with tab %d", rtl::OUStringToOString( rName, RTL_TEXTENCODING_UTF8 ).getStr(),nPos );
+						pExtOptions->SetCodeName( rName, nPos );
+				}
+
 				pTab[nPos] = new ScTable(this, nPos, rName);
 				++nMaxTableNumber;
+
 				for (i = 0; i <= MAXTAB; i++)
 					if (pTab[i])
 						pTab[i]->UpdateCompile();
@@ -369,6 +397,7 @@ BOOL ScDocument::DeleteTab( SCTAB nTab, 
 			SCTAB nTabCount = GetTableCount();
 			if (nTabCount > 1)
 			{
+				ScExtDocOptions* pExtOptions = GetExtDocOptions();
 				BOOL bOldAutoCalc = GetAutoCalc();
 				SetAutoCalc( FALSE );	// Mehrfachberechnungen vermeiden
 				ScRange aRange( 0, 0, nTab, MAXCOL, MAXROW, nTab );
@@ -417,7 +446,25 @@ BOOL ScDocument::DeleteTab( SCTAB nTab, 
 									pRefUndoDoc ? pRefUndoDoc->pTab[i] : 0);
 				delete pTab[nTab];
 				for (i=nTab + 1; i < nTabCount; i++)
+				{
+					if ( pTab[i - 1 ] && pExtOptions && ( pExtOptions->GetCodeNameCount() > 0 ) )
+					{
+						String sCodeName = pExtOptions->GetCodeName( i );
+						if ( sCodeName.Len() )
+						{
+							OSL_TRACE("Associating codeName %s with tab %d", rtl::OUStringToOString( sCodeName, RTL_TEXTENCODING_UTF8 ).getStr(), i - 1 );
+							pExtOptions->SetCodeName( sCodeName, i - 1 );
+						}
+					}
 					pTab[i - 1] = pTab[i];
+
+				}
+				if ( pTab[i - 1 ] && pExtOptions && ( pExtOptions->GetCodeNameCount() > 0 ) )
+				{
+					OSL_TRACE("Deleting codename %s as %d", rtl::OUStringToOString(  pExtOptions->GetCodeName( nTabCount-1 ), RTL_TEXTENCODING_UTF8 ).getStr(),  nTabCount - 1 ) ;
+					pExtOptions->DeleteCodeName( nTabCount - 1 );
+				}
+
 				pTab[nTabCount - 1] = NULL;
 				--nMaxTableNumber;
 				for (i = 0; i <= MAXTAB; i++)
