WorkbookBase.Connections Property

Definition

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

public:
 property Microsoft::Office::Interop::Excel::Connections ^ Connections { Microsoft::Office::Interop::Excel::Connections ^ get(); };
public Microsoft.Office.Interop.Excel.Connections Connections { get; }
member this.Connections : Microsoft.Office.Interop.Excel.Connections
Public ReadOnly Property Connections As Connections

Property Value

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 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);
    }
}
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

Applies to