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