I would like to use piece of the code in several modules of Workbook VBA. Basically this is used to establish connection to file management system via it's client. I use it in a lot of macros and have to copy this to each module separately. Is it possible to convert this to some kind Public function or macro so it can be used in whole Workbook?
On top of each module there is this one:
Option Explicit
Const szVaultName = "Name"
Const szVaultGUID As String = "{1234}"
Public oMFClientApp As New MFilesAPI.MFilesClientApplication
Public oObjVerFile As MFilesAPI.ObjectVersionAndProperties
Public oVault As MFilesAPI.Vault
Public oObjectSearchResults As MFilesAPI.ObjectSearchResults
Here is code for establishing connection:
Sub EstablishMFilesConnection()
Dim oMFClientApp As New MFilesAPI.MFilesClientApplication
Dim oVaultConnections As MFilesAPI.VaultConnections
'Login to the vault
Set oVaultConnections = oMFClientApp.GetVaultConnectionsWithGUID(szVaultGUID)
If oVaultConnections.Count = 0 Then
Set oVault = oMFClientApp.BindToVault(szVaultName, 0, True, True)
MsgBox "No vaults found with the GUID: " + szVaultGUID, vbExclamation
End
Else
On Error Resume Next
Set oVault = oMFClientApp.BindToVault(oVaultConnections.Item(1).Name, 0, True, True)
oVault.TestConnectionToVault
If Err.Number <> 0 Then
MsgBox "Can't connect to M-Files", vbExclamation
End
End If
On Error GoTo 0
End If
On Error GoTo 0
End Sub