MaintainConnection 属性 (Excel)QueryTable.MaintainConnection property (Excel)

如果从刷新数据开始直至关闭工作簿,都一直保留指向指定数据源的连接,则为 TrueTrue if the connection to the specified data source is maintained after the refresh and until the workbook is closed. 默认值为 TrueThe default value is True. 读取/写入 BooleanRead/write Boolean.



_表达式_一个代表查询表对象**** 的变量。expression A variable that represents a QueryTable object.


仅当查询表或数据透视表缓存的**QueryType** 属性设置为xloledbquery 时时, 才能设置MaintainConnection属性。You can set the MaintainConnection property only if the QueryType property of the query table or PivotTable cache is set to xlOLEDBQuery.

如果预计会频繁对服务器进行查询,则可将此属性设置为 True,这样能减少重新连接的时间因而可提高性能。If you anticipate frequent queries to a server, setting this property to True might improve performance by reducing reconnection time. 将此属性设置为 False,将会关闭一个打开的连接。Setting the property to False causes an open connection to be closed.

如果使用用户界面导入数据, 则会将来自 web 查询或文本查询的数据作为一个查询表**** 对象导入, 而将所有其他外部数据作为**ListObject** 对象导入。If you import data by using the user interface, data from a web query or a text query is imported as a QueryTable object, while all other external data is imported as a ListObject object.

如果使用对象模型导入数据, 则必须将来自 web 查询或文本查询的数据作为查询表导入****, 而所有其他外部数据均可作为ListObject或查询表导入。 ****If you import data by using the object model, data from a web query or a text query must be imported as a QueryTable, while all other external data can be imported as either a ListObject or a QueryTable.

您可以使用ListObject的查询表属性来访问MaintainConnection属性。 You can use the QueryTable property of the ListObject to access the MaintainConnection property.


本示例新建一个基于 OLAP 提供程序的数据透视表高速缓存, 然后基于活动工作表的 A3 单元格上的缓存创建一个新的数据透视表。This example creates a new PivotTable cache based on an OLAP provider, and then it creates a new PivotTable report based on the cache at cell A3 on the active worksheet. 本示例将在初始刷新后终止连接。The example terminates the connection after the initial refresh.

With ActiveWorkbook.PivotCaches.Add(SourceType:=xlExternal) 
 .Connection = _ 
 "OLEDB;Provider=MSOLAP;Location=srvdata;Initial Catalog=National" 
 .MaintainConnection = False 
 .CreatePivotTable TableDestination:=Range("A3"), _ 
 TableName:= "PivotTable1" 
End With 
With ActiveSheet.PivotTables("PivotTable1") 
 .SmallGrid = False 
 .PivotCache.RefreshPeriod = 0 
 With .CubeFields("[state]") 
 .Orientation = xlColumnField 
 .Position = 0 
 End With 
 With .CubeFields("[Measures].[Count Of au_id]") 
 .Orientation = xlDataField 
 .Position = 0 
 End With 
End With

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.