开发者

Updating All OLEDB Connections in Excel 2007 via Macro

I don't profess to be any master of Excel, so when these things arrive I can generally write a 开发者_运维知识库best-effort macro to sort out any bulk updates, but this one has me stumped.

When we migrate our reports that connect to an OLEDB source and Cube they are coming across with the following two connection settings:

.RefreshOnFileOpen = True
.RefreshPeriod = 10

I need all of these in a workbook to be update to:

.RefreshOnFileOpen = False
.RefreshPeriod = 0

Recording a macro and changing one connection results in the following VB script:

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

So I thought that the following Macro would work:

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

But no... So I'm going to keep trying to figure this out, but if anyone here can help me that would be great.

[EDIT#1] Thanks to Andrew I'm a step closer and I'm pursuing this link here. Seems I was altering this object in the wrong place. :) http://msdn.microsoft.com/en-us/library/bb213295(office.12).aspx

[SOLUTION]

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


You forgot the With block:

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


This should work as well:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜