Code:
' Global variables to hold the reference key and key context values
Private RefKey() As Byte
Private ContextData() As Byte
Public Sub ReferenceKeyFromFace() 'Set a reference to the active document
Dim oDoc As Document
Set oDoc = ThisApplication.ActiveDocument
' Check to make sure a single face is in the select set.
If oDoc.SelectSet.Count <> 1 _
Or Not Typeof oDoc.SelectSet.Item(1) Is Face Then
MsgBox "A single face must be selected."
Exit Sub
End If
' Get the Face from the select set.
Dim oFace As Face
Set oFace = oDoc.SelectSet.Item(1)
' Clear the select set
oDoc.SelectSet.Clear
' Create a key context (required to obtain ref keys for BRep entities)
Dim KeyContext As Long
KeyContext = oDoc.ReferenceKeyManager.CreateKeyContext
' Get a reference key for the face
Call oFace.GetReferenceKey(RefKey, KeyContext)
' Save KeyContext as a byte array for future use
Call oDoc.ReferenceKeyManager.SaveContextToArray(KeyContext, ContextData)
End Sub
Public Sub FaceFromReferenceKey()
'Set a reference to the active document
Dim oDoc As Document
Set oDoc = ThisApplication.ActiveDocument
' Retrieve ContextKey from byte array
Dim KeyContext As Long
KeyContext = oDoc.ReferenceKeyManager.LoadContextFromArray(ContextData)
' Bind reference key to the Face object
Dim oFace As Face
Set oFace = oDoc.ReferenceKeyManager.BindKeyToObject(RefKey, KeyContext, kFaceObject)
' Select the face
oDoc.SelectSet.Select oFace
End Sub