Class Connections (Excel VBA)
A collection of WorkbookConnection objects for the specified workbook. To use a Connections class variable it first needs to be instantiated, for example
Dim cnns as Connections
Set cnns = ActiveWorkbook.Connections
For Each
Here is an example of processing the Connections items in a collection.
Dim wbconConnection As WorkbookConnection
For Each wbconConnection In ActiveWorkbook.Connections
Next wbconConnection
Add2
Add2 (Name, Description, ConnectionString, CommandText, lCmdtype, CreateModelConnection, ImportRelationships)
Dim strName As String: strName =
Dim strDescription As String: strDescription =
Dim strConnectionString As String: strConnectionString =
Dim strCommandText As String: strCommandText =
Dim wbconAdd2 As WorkbookConnection
Set wbconAdd2 = ActiveWorkbook.Connections.Add2(Name:=strName, Description:=strDescription, ConnectionString:=strConnectionString, CommandText:=strCommandText)
Arguments
The following arguments are required:
Name - String
Description - String
ConnectionString - String
CommandText - String
Optional arguments
The following arguments are optional
lCmdtype
CreateModelConnection
ImportRelationships
AddFromFile
Adds a connection from the specified file.
AddFromFile (Filename, CreateModelConnection, ImportRelationships)
Dim strFilename As String: strFilename =
Dim wbconAddFromFile As WorkbookConnection
Set wbconAddFromFile = ActiveWorkbook.Connections.AddFromFile(Filename:=strFilename)
Arguments
The following argument is required
Filename (String) - Name of the file.
Optional arguments
The following arguments are optional
CreateModelConnection (Boolean) - Specifies whether to create the connection to the model.
ImportRelationships (Boolean) - Specifies whether to import the connection relationship.
Count
Returns the number of objects in the collection.
Dim lngCount As Long
lngCount = ActiveWorkbook.Connections.Count
Item
This method creates a connection item.
Item (Index)
Index: Index value of the item.
Dim wbconConnection As WorkbookConnection
Set wbconConnection = ActiveWorkbook.Connections(Index:=1)