任何人都可以建议一个VBA过程来替换Excel SQl Server OLEDB连接字符串的元素吗?
我已尝试使用下面的代码,但我认为这与dbconnection
变量的数据类型有关。
Sub ModifyConnection()
Dim ComHostname As String
ComHostname = Environ$("computername")
Call MsgBox(ComHostname)
Dim dbconnection As String
dbconnection = ActiveWorkbook.Connections("ConnectionName").OLEDBConnection.connection
dbconnection = Replace(dbconnection, "___BTN_____\", ComHostname & "\")
Set ActiveWorkbook.Connections("ConnectionName").OLEDBConnection.connection = dbconnection
End Sub
代码尝试将连接字符串中的计算机主机名实例替换为当前计算机的实例。
它抛出错误424 - 需要对象。
答案 0 :(得分:0)
事实证明这是一个连接名称错误的简单案例。操作代码如下:
Sub ModifyConnection()
Dim ComHostname As String
ComHostname = Environ$("computername")
Call MsgBox(ComHostname)
Dim dbconnection As String
dbconnection = ActiveWorkbook.Connections("ConnectionName").OLEDBConnection.connection
dbconnection = Replace(dbconnection, "???BTN?????\", ComHostname & "\")
ActiveWorkbook.Connections("ConnectionName").OLEDBConnection.connection = dbconnection
End Sub