VBA 列出 SQL 个连接及其在多个 sheet Excel 工作簿中的属性

VBA to List SQL connections and their properties within multi sheet Excel workbooks

我继承了许多复杂的 Excel 多工作表工作簿,其中包含许多不同的 SQL 数据连接。有些受密码保护,有些有连接文件,有些在打开时刷新数据等。 我需要记录和检查这些。我更愿意以编程方式执行此操作,而不是使用 "Connections" 对话框手动搜索每个连接。 是否有一种方法可以遍历每个工作表并提供 "Usage" 和 "Definition" 选项卡中可用的各种 "Connection Properties" 的列表? 我相当精通 VBA 但无法弄清楚这一点!

到目前为止我唯一的起始代码是

Private Sub cmd_TestIt_Click()<br>
 Dim wb As Workbook<br>
 Dim wks As Worksheet
 Dim pt As PivotTable
 Dim pc As PivotCache

 Set wb = ActiveWorkbook
 For Each wks In Worksheets
     For Each pt In wks.PivotTables
         Set pc = wb.PivotCaches(pt.CacheIndex)
         mystring = "Data Range : " & pc.SourceData
         MsgBox mystring
  Next
Next
End Sub

这为我提供了数据连接,但我确实需要该数据连接的属性
--21/08/2015 --- 好的。我在路上。我有可用的代码,但它很笨重。
Cmd_testit 有效,但应该有一些方法可以添加一个 For .. 每个连接 items.As 我只能做一个连接。

Private Sub cmd_TestIt_Click()
Dim wb As Workbook
Dim splitz() As String
Dim j As Integer
Set wb = ActiveWorkbook

mystring = mystring + "Refresh on Open=" +    CStr(wb.Connections.item(1).OLEDBConnection.RefreshOnFileOpen) + ";"
mystring = mystring + "Save Password=" + CStr(wb.Connections.item(1).OLEDBConnection.SavePassword) + ";"
mystring = mystring + "Connection file=" +    wb.Connections.item(1).OLEDBConnection.SourceConnectionFile
mystring = mystring + wb.Connections.item(1).OLEDBConnection.Connection
splitz = Split(mystring, ";")
mystring = ""
For j = 0 To UBound(splitz)
    mystring = mystring + splitz(j) + vbCrLf
Next
MsgBox mystring
End Sub

我正在使用 "Test2" 进行尝试,但找不到实现此目的的神奇组合。

Private Sub Test2_Click()
Dim wb As Workbook
Dim splitz() As String
Dim j As Integer
Dim item As Items
Set wb = ActiveWorkbook
For Each item In wb.Connections
    mystring = mystring + "Refresh on Open=" + CStr(item.OLEDBConnection.RefreshOnFileOpen) + ";"
    mystring = mystring + "Save Password=" +   CStr(item.OLEDBConnection.SavePassword) + ";"
    mystring = mystring + "Connection file=" + item.OLEDBConnection.SourceConnectionFile
    mystring = mystring + item.OLEDBConnection.Connection
    splitz = Split(mystring, ";")
    mystring = ""
    For j = 0 To UBound(splitz)
        mystring = mystring + splitz(j) + vbCrLf
   Next
   MsgBox mystring
Next
End Sub

破解了!如果我不将项目定义为项目,这似乎可行!
我现在要用输出文件替换 MsgBox。
然后我会在记事本中打开文件!
如果有更好的方法,请告诉我!

Private Sub Test2_Click()
    Dim wb As Workbook
    Dim splitz() As String
    Dim j As Integer
    Dim item
    Dim StrCon As String
    Set wb = ActiveWorkbook
    For Each item In wb.Connections
        StrCon = ""
        StrCon = "Refresh on Open=" +     CStr(item.OLEDBConnection.RefreshOnFileOpen) + ";"
        StrCon = StrCon + "Save Password=" + CStr(item.OLEDBConnection.SavePassword) + ";"
        StrCon = StrCon + "Connection file=" + item.OLEDBConnection.SourceConnectionFile + ";"
        StrCon = StrCon + item.OLEDBConnection.Connection
        splitz = Split(StrCon, ";")
        StrCon = ""
        For j = 0 To UBound(splitz)
            StrCon = StrCon + splitz(j) + vbCrLf
        Next
        MsgBox StrCon
    Next
End Sub