2010-09-19 47 views
0

我並不認爲自己是Excel的主人,所以當這些東西到達時,我通常可以編寫一個盡力而爲的宏來理清所有批量更新,但這個人有我難倒。通過宏更新Excel 2007中的所有OLEDB連接

當我們將我們的報道說,連接到一個OLEDB源和Cube他們跨以下兩個連接設置來:

.RefreshOnFileOpen = True 
.RefreshPeriod = 10 

我需要所有的這些工作簿中要更新:

.RefreshOnFileOpen = False 
.RefreshPeriod = 0 

錄製宏,變化在以下VB腳本一個連接的結果:

Sub ChangeConn() 
' 
' ChangeConn Macro 
' 

' 
    With ActiveWorkbook.Connections("Connection").OLEDBConnection 
     .CommandText = Array("Something") 
     .CommandType = xlCmdCube 
     .Connection = Array(_ 
     "OLEDB;Provider=MSOLAP.3;Persist Security Info=True;User ID="""";Initial Catalog=Something;Data Source=Something;Location=Something" _ 
     , _ 
     "Something;MDX Compatibility=1;Safety Options=2;MDX Missing Member Mode=Error" _ 
     ) 
     .RefreshOnFileOpen = False 
     .SavePassword = False 
     .SourceConnectionFile = "" 
     .MaxDrillthroughRecords = 1000 
     .ServerCredentialsMethod = xlCredentialsMethodIntegrated 
     .AlwaysUseConnectionFile = False 
     .RetrieveInOfficeUILang = False 
     .ServerFillColor = False 
     .ServerFontStyle = False 
     .ServerNumberFormat = False 
     .ServerTextColor = False 
    End With 
    With ActiveWorkbook.Connections("Connection") 
     .Name = "Connection" 
     .Description = "" 
    End With 
End Sub 

所以我想,下面的宏將工作:

Sub FixConn() 
For Each OLEDBConnection In ActiveWorkbook.Connections 
    .RefreshOnFileOpen = False 
     .RefreshPeriod = 0 
Next OLEDBConnection 
End Sub 

但是,沒有......所以我要繼續努力摸不着頭腦,但如果有人在這裏可以幫助我,將是巨大的。

[編輯#1]感謝安德魯我更近了一步,我在這裏追求這個鏈接。似乎我在錯誤的地方改變了這個東西。 :) http://msdn.microsoft.com/en-us/library/bb213295(office.12).aspx

[解決方法]

Sub ReFixConn() 
    For Each PivotCache In ActiveWorkbook.PivotCaches 
     With PivotCache 
      .RefreshOnFileOpen = False 
      .RefreshPeriod = 0 
     End With 
    Next PivotCache 
End Sub 

回答

0

你忘了帶塊:

Sub FixConn() 
For Each OLEDBConnection In ActiveWorkbook.Connections 
    With OLEDBConnection 
    .RefreshOnFileOpen = False 
    .RefreshPeriod = 0 
    End With 
Next OLEDBConnection 
End Sub 
+0

感謝隊友。這讓我更靠近一步。正如我所說這不是我學到的東西。現在唯一的問題是,我得到一個錯誤「對象不支持這個屬性或方法」,這聽起來像我想改變我想改變此設置在錯誤的區域.. – Linnay 2010-09-19 23:46:41

+0

解決:) 子ReFixConn() 對於每個PivotCache在ActiveWorkbook.PivotCaches 隨着PivotCache .RefreshOnFileOpen =假 .RefreshPeriod = 0 結束隨着 接着PivotCache 結束子 – Linnay 2010-09-19 23:55:32

0

這應該工作以及:

Sub FixConn() 
Dim cn as Workbookconnection 
For Each cn In ActiveWorkbook.Connections 
    cn.OLEDBConnection.RefreshOnFileOpen = False 
    cn.OLEDBConnection.RefreshPeriod = 0 
next 
End Sub