|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 张文洲 于 2014-4-24 14:30 编辑
最近经常遇见关于更新数据连接数据源路径的问题,下面这段代码应该可以实现更新数据源路径为当前工作簿路径,有需要的可以尝试下- Private Sub Workbook_Open()
- Dim strCon As String, iPath As String, i As Integer, iFlag As String, iStr As String, oledb As OLEDBConnection
- For i= 1 To ActiveWorkbook.Connections.Count
- Set oledb = ActiveWorkbook.Connections(i).OLEDBConnection
- strCon = oledb.Connection
- Select Case Left(strCon, 5)
- Case "ODBC;"
- iFlag = "DBQ="
- Case "OLEDB"
- iFlag = "Source="
- Case Else
- Exit Sub
- End Select
- iStr = Split(Split(strCon, iFlag)(1), ";")(0)
- iPath = Left(iStr, InStrRev(iStr, "") - 1)
- With oledb
- .Connection = VBA.Replace(strCon, iPath, ThisWorkbook.Path)
- .CommandText = VBA.Replace(.CommandText, iPath, ThisWorkbook.Path)
- End With
- Next
- End Sub
复制代码 |
|