从userform调用宏时出错

时间:2014-05-21 22:13:14

标签: vba outlook

我有一个userform,当单击一个按钮时,可以在一个单独的模块中调用一个宏。我收到以下错误:"运行时错误' 450':错误的参数数量或无效的属性分配"

在排除故障时,我删除了参数并更改了虚拟宏,我调用的是不接受参数,但是我得到了同样的错误。

Public Sub btnSubmit_Click()
Dim Description As String
Dim Priority As String
If (checkCleared.Value = False) Then
MsgBox ("Please certify that all sensitive informationhas been removed and then submit")
Exit Sub
Else

'Description = formScreen.txtDesc.Value
'Priority = formScreen.comboPriority.Value

'Application.Run ThisOutlookSession!postScreenedEmail(Priority, Description)
Application.Run ThisOutlookSession!postScreenedEmail
End If
End Sub

在单独的模块中:

Public Sub postScreenedEmail() '(Priority As String, Description As String)
MsgBox ("postScreened")
'MsgBox ("Priority is: " & Priority & "    and Description is " & Description)
End Sub

我尝试了其他调用宏的方法,例如" Call postScreenedEmail()"但它无法看到宏。我的最终目标是从userform获取值并将它们传递给另一个宏,以便它们可以与我正在使用的API一起使用。

编辑:我可能混淆了我的术语,这是我正在使用的层次结构(不能用我的代表发布图片)。话虽如此,我试图只用Application.Run "postScreenedEmail", Priority, Description进行调用,但没有改变任何内容

-Project1(VbaProject.OTM)
  -Microsoft Outlook Objects
  | ThisOutlookSession
  -Forms
  | formScreen
  |
  -Modules
    Module1

2 个答案:

答案 0 :(得分:1)

尝试:

call postScreenedEmail

而不是:

Application.Run ThisOutlookSession!postScreenedEmail

由于您的sub是公共的,因此vba应该能够在没有模块引用的情况下找到它。

如果这样做,再次添加引用(使代码更具可读性,特别是对于其他代码,如ckuhn203在评论中指出的那样)并查看它是否中断。如果是这样,那就是问题所在。


编辑:


您确定要引用正确的模块吗?

如果我尝试:

-Project1(VbaProject.OTM)
  -Microsoft Outlook Objects
  | ThisOutlookSession
  -Modules
  | Module1
模块1中的

Sub jzz()
Debug.Print "test"
End Sub

并在ThisOutlookSession中:

Sub test()
Call Module1.jzz
End Sub

它有效。没问题。使用:

Application.Run Module1.jzz

而不是Call拖欠编译错误。

偶:

Sub test2()
Call ThisOutlookSession.test
End Sub
来自Module1的

可以正常工作。

你可以运行这么小的测试来试图获得正确的引用吗?

答案 1 :(得分:0)

试试这个... Application.Run为过程名称取一个字符串,然后用逗号分隔的参数/参数列表:

Application.Run "Procedure_Name", arg1, arg2, arg3

所以我认为这应该有效:

Application.Run "ThisOutlookSession!postScreenedEmail", Priority, Description