Partilhar via


WorkbookBase.Connections Property

Gets the collection of connections between the workbook and a data source.

Namespace:  Microsoft.Office.Tools.Excel
Assembly:  Microsoft.Office.Tools.Excel.v4.0.Utilities (in Microsoft.Office.Tools.Excel.v4.0.Utilities.dll)

Syntax

'Declaration
Public ReadOnly Property Connections As Connections
public Connections Connections { get; }

Property Value

Type: Microsoft.Office.Interop.Excel.Connections
A Microsoft.Office.Interop.Excel.Connections collection that contains the connections between the workbook and a data source.

Examples

The following code example adds a new data connection to the specified CSV file. Next, the example iterates through all connections that exist in the workbook and displays the name, type, and description of each connection. This example requires a CSV file named SalesData.csv at the root of drive C. The CSV file should contain comma-separated data. When you run this example, Excel starts the Text Import Wizard; follow the steps in this wizard to create the data connection to the CSV file.

This example is for a document-level customization.

Private Sub WorkbookConnections()
    Dim myConnection As Excel.WorkbookConnection = _
        Me.Connections.AddFromFile("c:\SalesData.csv")
    myConnection.Description = "Data stored in a CSV file." 

    For Each connection As Excel.WorkbookConnection In Me.Connections
        MessageBox.Show(connection.Name + " " + connection.Type.ToString() _
            + " " + connection.Description)
    Next 
End Sub
private void WorkbookConnections()
{
    Excel.WorkbookConnection myConnection =
        this.Connections.AddFromFile(@"C:\SalesData.csv");
    myConnection.Description = "Data stored in a CSV file.";

    foreach (Excel.WorkbookConnection connection in this.Connections)
    {
        MessageBox.Show(
            "Connection Name: " + connection.Name 
            + "\r\nConnection Type: " + connection.Type.ToString()
            + "\r\nDescription: " + connection.Description);
    }
}

.NET Framework Security

See Also

Reference

WorkbookBase Class

Microsoft.Office.Tools.Excel Namespace