以编程方式添加VBA引用

时间:2017-11-07 23:25:57

标签: vba ms-office

我正在尝试更正各种Avaya应用程序版本的引用问题(由于不同用户的不同目录而引用丢失)。我们有多个版本在运行,需要它来处理所有可能的版本,然后给出错误。到目前为止,我已经创建了下面的内容,以便在开始时将它们内联定义,但它仍然会引发一个"编译错误:无效的外部过程"。我是新手编码并尝试将此信息添加到现有的VBA设置中。

'Start Declarations
Dim cvsApp As New ACSUP.cvsApplication
Dim cvsConn As New ACSCN.cvsConnection
Dim cvsSrv As New ACSUPSRV.cvsServer
Dim Rep As New ACSREP.cvsReport
Dim Info As Object, Log As Object, b As Object
Dim logged As Boolean

'Start My Code
If Dir("C:\Program Files (x86)\Avaya\CMS Supervisor R16", vbDirectory) <> 
vbNullString Then
    cvsApp.References.AddFromFile "C:\Program Files (x86)\Avaya\CMS 
Supervisor R16\ascApp.exe"
    cvsConn.References.AddFromFile "C:\Program Files (x86)\Avaya\CMS Supervisor R16\cvsCONN.dll"
    cvsSrv.References.AddFromFile "C:\Program Files (x86)\Avaya\CMS Supervisor R16\ascSRV.exe"
    Rep.References.AddFromFile "C:\Program Files (x86)\Avaya\CMS Supervisor R16\ascRep.exe"
    Catalog.References.AddFromFile "C:\Program Files (x86)\Avaya\CMS Supervisor R16\cvsctlg.dll"
    Else
    If Dir("C:\Program Files (x86)\Avaya\CMS Supervisor R17", vbDirectory) <> vbNullString Then
        cvsApp.References.AddFromFile "C:\Program Files (x86)\Avaya\CMS Supervisor R17\ascApp.exe"
        cvsConn.References.AddFromFile "C:\Program Files (x86)\Avaya\CMS Supervisor R17\cvsCONN.dll"
        cvsSrv.References.AddFromFile "C:\Program Files (x86)\Avaya\CMS Supervisor R17\ascSRV.exe"
        Rep.References.AddFromFile "C:\Program Files (x86)\Avaya\CMS Supervisor R17\ascRep.exe"
        Catalog.References.AddFromFile "C:\Program Files (x86)\Avaya\CMS Supervisor R17\cvsctlg.dll"
        Else
        If Dir("C:\Program Files (x86)\Avaya\CMS Supervisor R18", vbDirectory) <> vbNullString Then
            cvsApp.References.AddFromFile "C:\Program Files (x86)\Avaya\CMS Supervisor R18\ascApp.exe"
            cvsConn.References.AddFromFile "C:\Program Files (x86)\Avaya\CMS Supervisor R18\cvsCONN.dll"
            cvsSrv.References.AddFromFile "C:\Program Files (x86)\Avaya\CMS Supervisor R18\ascSRV.exe"
            Rep.References.AddFromFile "C:\Program Files (x86)\Avaya\CMS Supervisor R18\ascRep.exe"
            Catalog.References.AddFromFile "C:\Program Files (x86)\Avaya\CMS 
Supervisor R18\cvsctlg.dll"
            Else
                MsgBox ("ERROR: Avaya Files not found, Contact Admin")
        End If
    End If
End If

按下按钮时调用的子。

Sub Report_Run()

Call FixReference 'added this in - does it need to be?
Call Unhide
Call Clear_Report
Call Report_a
Call Hide

End Sub

编辑#2 - 这是我给出的原始代码,其中包含基于“工具”部分的参考。我们大多数人同时拥有多台服务器,因此它会调用cms服务器订单号(So 3rd server打开等)。代码前显示的设置窗口:

设置
开始日期:2017年10月15日 结束日期:10/21/2017 技能:**** 服务器订单:1 ACD:6

Dim cvsApp As New ACSUP.cvsApplication
Dim cvsConn As New ACSCN.cvsConnection
Dim cvsSrv As New ACSUPSRV.cvsServer
Dim Rep As New ACSREP.cvsReport
Dim Info As Object, Log As Object, b As Object
Dim logged As Boolean

Sub Clear_Report()

Sheets(Array("-1 (2)", "-2 (2)", "-3 (2)", "-4 (2)", "-5 (2)", "-6 (2)", "-7 (2)", _
    "-8 (2)", "-9 (2)", "-10 (2)", "-11 (2)", "-12 (2)", "-13 (2)", "-14 (2)", "-15 (2)", _
    "-16 (2)", "-17 (2)", "-18 (2)", "-19 (2)", "-20 (2)", "-21 (2)", "-22 (2)", "-23 (2)" _
    , "-24 (2)", "-25 (2)")).Select
Sheets("-1 (2)").Activate
Sheets(Array("-26 (2)", "-27 (2)", "-28 (2)", "-29 (2)", "-30 (2)", "-31 (2)", _
    "-32 (2)", "-33 (2)", "-34 (2)", "-35 (2)")).Select Replace:=False
Columns("A:AB").Select
Selection.ClearContents
Sheets("Auto").Select
End Sub

Sub Report_Run()
Call Unhide
Call Clear_Report
Call Report_a

End Sub



Public Sub Report_a()

Application.ScreenUpdating = 0


Call Move_down2


Sheets("Report Drop").Select
Range("b2:x4500").Select
Selection.ClearContents
Selection.Clear

Dim Serv As Integer
Dim Report As String
Dim Report2 As String
Dim B1 As String
Dim B2 As String
Dim B3 As String
Dim B1A As String
Dim B2A As String
Dim B3A As String
Dim ACDset As String
Dim B4 As String
Dim B4A As String
Dim Dropline As Integer

Serv = ThisWorkbook.Sheets("Auto").Cells(8, 2)
Report = ThisWorkbook.Sheets("Auto").Cells(2, 2)
'Report2 = ThisWorkbook.Sheets("Auto").Cells(51, 26)
'B1 = ThisWorkbook.Sheets("Auto").Cells(4, 2)
'B2 = ThisWorkbook.Sheets("Auto").Cells(5, 2)
'B3 = ThisWorkbook.Sheets("Auto").Cells(5, 26)
B1A = ThisWorkbook.Sheets("Auto").Cells(4, 3)
B2A = ThisWorkbook.Sheets("Auto").Cells(5, 3)
'B3A = ThisWorkbook.Sheets("Auto").Cells(8, 26)
Dropline = ThisWorkbook.Sheets("Auto").Cells(3, 2)


ACDset = ThisWorkbook.Sheets("Auto").Cells(9, 2)
Set cvsSrv = cvsApp.Servers(Serv) '(cvsApp.Servers.Count) '''''' we set a reference to the server here
Call Report_b(Report, ACDset, B1, B2, B3, B1A, B2A, B3A, B4, B4A)
ThisWorkbook.Sheets("Report Drop").Cells(Dropline, 2).PasteSpecial
logout
Application.ScreenUpdating = 1

    Sheets("Report Drop").Select
Range("A1:ab5000").Select
Selection.Copy
Sheets("-1 (2)").Select
Range("A1").Select
ActiveSheet.Paste



Application.ScreenUpdating = 1
Call Rep2
Sheets("Output").Select
End Sub

Sub Report_b(sReportName As String, ByVal ACDset As String, ByVal B1 As String, B2 As String, B3 As String, B1A As String, B2A As String, B3A As String, B4 As String, B4A As String)

On Error Resume Next
cvsSrv.Reports.ACD = ACDset ' In server03 ACD1=AD2  ACD2=KC ACD3=Core
Set Info = cvsSrv.Reports.Reports(sReportName)
If Info Is Nothing Then
If cvsSrv.Interactive Then
MsgBox "The Report " & sReportName & " was not found on ACD 1", vbCritical Or vbOKOnly, "CentreVu Supervisor"
Else
Set Log = CreateObject("ACSERR.cvslog")
Log.AutoLogWrite "The Report " & sReportName & " was not found on ACD 1"
Set Log = Nothing
End If
Else
b = cvsSrv.Reports.CreateReport(Info, Rep)
If b Then
Rep.Window.Top = 0
Rep.Window.Left = 0
Rep.Window.Width = 0
Rep.Window.Height = 0


Debug.Print Rep.SetProperty("Split/Skill(s)", B1A)
Debug.Print Rep.SetProperty("Date(s)", B2A)
Debug.Print Rep.SetProperty("Times", "00:00-23:30")
'Debug.Print Rep.SetProperty(B4, B4A)
b = Rep.ExportData("", 9, 0, True, False, True)
Rep.Quit
If Not cvsSrv.Interactive Then cvsSrv.ActiveTasks.Remove Rep.TaskID
Set Rep = Nothing
End If
End If
Set Info = Nothing
End Sub

Sub Rep2()
If ThisWorkbook.Sheets("Auto").Cells(6, 3) = "Y" Then Call Report_a
Sheets("Output").Select
'Call Macro4
Sheets("Output").Select
End Sub

Sub Hide()
'
Sheets("Report Drop").Visible = False
Sheets("-1 (2)").Visible = False
Sheets("-2 (2)").Visible = False
..(And so on)
'
End Sub

Sub Unhide()
'
Sheets("Report Drop").Select
Sheets("-1 (2)").Visible = True
..(And so on)
'
End Sub

Sub logout()
Set Log = Nothing
Set Rep = Nothing
Set cvsSrv = Nothing
Set cvsApp = Nothing
End Sub

2 个答案:

答案 0 :(得分:2)

您可以尝试替换4个早期绑定声明:

Dim cvsApp As New ACSUP.cvsApplication
Dim cvsConn As New ACSCN.cvsConnection
Dim cvsSrv As New ACSUPSRV.cvsServer
Dim Rep As New ACSREP.cvsReport

使用延迟绑定并从项目中删除Avaya CMS Supervisor引用:

Dim cvsApp As Object, cvsConn As Object, cvsSrv As Object, Rep As Object
Set cvsApp = CreateObject("ACSUP.cvsApplication")
Set cvsConn = CreateObject("ACSCN.cvsConnection")
Set cvsSrv = CreateObject("ACSUPSRV.cvsServer")
Set Rep = CreateObject("ACSREP.cvsReport")

答案 1 :(得分:0)

很抱歉,这个评论太多了,但它不是&#34;答案&#34;。 尝试运行这个小程序。运行时立即打开“立即”窗口。也许它会帮助你了解你需要做些什么来解决你的问题?

注意:如果无法识别Dim oRef As Reference

  • 添加对VBIDE的引用:ThisWorkbook.VBProject.References.AddFromGuid GUID:="{0002E157-0000-0000-C000-000000000046}", Major:=5, Minor:=3
  • 信任VBA项目对象模型(Options&gt; Trust Center&gt; Macro Settings
Public Sub getAppRef()
Dim oRef        As Reference
Dim sName       As String * 20
Dim sBroke      As String * 15
Dim sPath       As String * 150
Dim sVer        As String * 15
Dim sKind       As String * 10
Dim sGUID       As String * 45


    debug.print Now(), "Reference:"
    For Each oRef In Application.References
    'Clear Variables
        sName = ""
        sBroke = ""
        sPath = ""
        sVer = ""
        sKind = ""
        sGUID = ""
    'Populate Variables
        sName = "Name:" & oRef.Name
        sBroke = "IsBroken:" & oRef.IsBroken
        sPath = "Path:" & oRef.FullPath
        sVer = "Version:" & oRef.Major & "." & oRef.Minor
        sKind = "Kind:" & oRef.Kind
        sGUID = "GUID:" & oRef.Guid
        Debug.Print Now(), sName, sBroke, sVer, sKind, sGUID, sPath
    Next
End Sub