vba 禁用工作簿连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7843586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disabling Workbook Connections
提问by TheNoodle
I have an Excel 2007 .xltm workbook template with several connections to SQL Server. I did not list the xltm's directory as a Trusted Location, so by default, external connections were disabled. This works well, because I have macros (signed & trusted) that would later enable these connections after a series of inputs using Workbook_Open
.
我有一个 Excel 2007 .xltm 工作簿模板,其中包含多个到 SQL Server 的连接。我没有将 xltm 的目录列为受信任位置,因此默认情况下,外部连接被禁用。这很有效,因为我有宏(已签名和受信任),稍后会在使用Workbook_Open
.
However, I recently added the directory as a trusted location, and now the connections refresh automatically (regardless of Connection.ODBCConnection.RefreshOnFileOpen
being set to False
..)
但是,我最近将该目录添加为受信任位置,现在连接会自动刷新(无论Connection.ODBCConnection.RefreshOnFileOpen
是否设置为False
..)
So, I was hoping to find a simple command such as ActiveWorkbook.DisableConnections
(since there is an ActiveWorkbook.EnableConnections
...)
所以,我希望找到一个简单的命令,例如ActiveWorkbook.DisableConnections
(因为有一个ActiveWorkbook.EnableConnections
......)
All I see is the read-only ActiveWorkbook.ConnectionsDisabled
... How do I set it to true?
我看到的只是只读ActiveWorkbook.ConnectionsDisabled
......我如何将其设置为true?
采纳答案by TheNoodle
Figured it out. I run the following function once I'm done modifying the file, and this prevents the template from automatically refreshing the data.
弄清楚了。修改完文件后,我运行以下函数,这会阻止模板自动刷新数据。
Private Sub DisableConnections()
Dim conn As Object
For Each conn In ActiveWorkbook.Connections
conn.ODBCConnection.EnableRefresh = False
Next
End Sub
Depending on my setup, I might swap ODBCConnection
for OLEDBConnection
.
根据我的设置,我可能会换ODBCConnection
的OLEDBConnection
。