我想在 Workbook VBA 的几个模块中使用一段代码。基本上,这用于通过其客户端建立与文件管理系统的连接。我在很多宏中使用它,并且必须将它分别复制到每个模块。是否可以将其转换为某种公共函数或宏,以便可以在整个工作簿中使用?
在每个模块的顶部有一个:
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
下面是建立连接的代码:
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