# VBA

Source: https://docs.interop.io/desktop/capabilities/data-sharing/shared-contexts/vba/index.html

## Opening or Creating Contexts

Define a [`GlueContextManager`](https://docs.interop.io/desktop/getting-started/how-to/interop-enable-your-apps/vba/index.md#classes-gluecontextmanager) instance and set its value using the [`GetGlueContext`](https://docs.interop.io/desktop/getting-started/how-to/interop-enable-your-apps/vba/index.md#classes-glue42-getgluecontext) method passing the name of a context as an argument. Use the [`Open`](https://docs.interop.io/desktop/getting-started/how-to/interop-enable-your-apps/vba/index.md#classes-gluecontextmanager-open) method of the `GlueContextManager` instance to open the selected context. If a context with the specified name doesn't exist, it will be automatically created.

The following example demonstrates a subroutine opening a context:

```vbnet
Dim WithEvents MyContext As GlueContextManager

Private Sub OpenContext()
    On Error GoTo HandleErrors

    Set MyContext = Glue.GetGlueContext("MyContext")
    MyContext.Open
    Exit Sub

    HandleErrors:
    ' Handle exceptions.

End Sub
```

> ⚠️ *Note that when you declare a `GlueContextManager` instance with `WithEvents`, your app is automatically subscribed for the events exposed by `GlueContextManager` and you must provide implementations for handling these events.*

## Subscribing for Context Updates

To subscribe for context updates, use the [`HandleContextUpdate`](https://docs.interop.io/desktop/getting-started/how-to/interop-enable-your-apps/vba/index.md#classes-gluecontextmanager-handlecontextupdate) event exposed by the [`GlueContextManager`](https://docs.interop.io/desktop/getting-started/how-to/interop-enable-your-apps/vba/index.md#classes-gluecontextmanager) instance. Its handler is executed when the context data has been updated.

The following example demonstrates a context update handler:

```vbnet
Private Sub MyContext_HandleContextUpdate(ByVal contextUpdate As IGlueContextUpdate)

    Dim context As GlueContextManager
    Set context = contextUpdate.GetContext

    Dim ContextName as String
    ContextName = context.GetContextInfo.Name

    Dim Data
    Set Data = context.GetReflectData("")
    ' or
    Dim JsonData As String
    JsonData = context.GetDataAsJson("")

    ' Examine the updated data.
    ...

    On Error GoTo HandleErrors
    Exit Sub

    HandleErrors:
    ' Handle exceptions.

End Sub
```

## Updating Contexts

Use the [`SetValue`](https://docs.interop.io/desktop/getting-started/how-to/interop-enable-your-apps/vba/index.md#classes-gluecontextmanager-setvalue) method of the [`GlueContextManager`](https://docs.interop.io/desktop/getting-started/how-to/interop-enable-your-apps/vba/index.md#classes-gluecontextmanager) instance to update the context data:

```vbnet
' Create a root composite value and add data in it.
Dim Data
Set Data = Glue.CreateGlueValues

Dim variantArray(0 To 1) As Variant
variantArray(0) = 1
variantArray(1) = "textValue"
Data("myTuple") = variantArray

' Updating a context.
MyContext.SetValue "data", Data
```

Alternatively, use the [`UpdateContextDataJson`](https://docs.interop.io/desktop/getting-started/how-to/interop-enable-your-apps/vba/index.md#classes-gluecontextmanager-updatecontextdatajson) method to update the context data using a JSON-formatted string:

```vbnet
MyContext.UpdateContextDataJson "data", "{""myTuple"":[1,""textValue""]}"
```

To remove an existing composite or elementary value from the context, use the [`Remove`](https://docs.interop.io/desktop/getting-started/how-to/interop-enable-your-apps/vba/index.md#classes-gluecontextmanager-remove) method:

```vbnet
MyContext.Remove "data.myTuple"
```

## Listing All Available Contexts

To get a list of all available contexts, use the [`GetKnownContexts`](https://docs.interop.io/desktop/getting-started/how-to/interop-enable-your-apps/vba/index.md#classes-glue42-getknowncontexts) method:

```vbnet
Dim AllContexts() as GlueContext

AllContexts = Glue.GetKnownContexts()
```
