繁体   English   中英

在Excel VBA中调用函数时无效的过程调用或参数

[英]invalid procedure call or argument when calling function in excel vba

单击后,有一个命令按钮可以触发向客户发送电子邮件的触发器。 它将首先这样调用该函数:

Private Sub Lotus2_Click()
ThisWorkbook.Send_Unformatted_Rangedata (2)
End Sub

然后在另一个工作表中有两个待调用函数,我无法调试它,因为无论何时,系统只会向我显示调用该函数的行。 问题是我知道调用函数有问题,但是我不确定函数的哪一部分出错。 很抱歉,功能部分有点繁琐,如下所示。 我非常感谢您提供的任何建议,谢谢。

********* UPDATE *******************嗨,我刚刚发现此行有问题,并且出现Run time error -2147417851 (80010105) Automation error The server threw an exception

Set noDocument = noDatabase.CreateDocument

但是我没有发现任何问题。 任何帮助将不胜感激。

Sub Send_Unformatted_Rangedata(i As Integer)
Dim noSession As Object, noDatabase As Object, noDocument As Object
Dim vaRecipient As Variant
Dim rnBody As Range
Dim Data As DataObject
Dim rngGen As Range
Dim rngApp As Range
Dim rngspc As Range

Dim stSubject As String
stSubject = "E-Mail For Approval for " + (Sheets("Summary").Cells(i, "A").Value) + "  for the Project  " + Replace(ActiveWorkbook.Name, ".xls", "")
'Const stMsg As String = "Data as part of the e-mail's body."
'Const stPrompt As String = "Please select the range:"

'This is one technique to send an e-mail to many recipients but for larger
'number of recipients it's more convenient to read the recipient-list from
'a range in the workbook.
 vaRecipient = VBA.Array(Sheets("Summary").Cells(i, "U").Value, Sheets("Summary").Cells(i, "V").Value)

On Error Resume Next
'Set rnBody = Application.InputBox(Prompt:=stPrompt, _
     Default:=Selection.Address, Type:=8)
'The user canceled the operation.
'If rnBody Is Nothing Then Exit Sub
Set rngGen = Nothing
Set rngApp = Nothing
Set rngspc = Nothing

Set rngGen = Sheets("General Overview").Range("A1:C30").SpecialCells(xlCellTypeVisible)
Set rngApp = Sheets("Application").Range("A1:E13").SpecialCells(xlCellTypeVisible)

Set rngspc = Sheets(Sheets("Summary").Cells(i, "P").Value).Range(Sheets("Summary").Cells(i, "Q").Value).SpecialCells(xlCellTypeVisible)
Set rngspc = Union(rngspc, Sheets(Sheets("Summary").Cells(i, "P").Value).Range(Sheets("Summary").Cells(i, "R").Value).SpecialCells(xlCellTypeVisible))

 On Error GoTo 0

If rngGen Is Nothing And rngApp Is Nothing And rngspc Is Nothing Then
    MsgBox "The selection is not a range or the sheet is protected. " & _
           vbNewLine & "Please correct and try again.", vbOKOnly
    Exit Sub
End If


'Instantiate Lotus Notes COM's objects.
Set noSession = CreateObject("Notes.NotesSession")
Set noDatabase = noSession.GETDATABASE("", "")

'Make sure Lotus Notes is open and available.
 If noDatabase.IsOpen = False Then noDatabase.OPENMAIL

'Create the document for the e-mail.
Set noDocument = noDatabase.CreateDocument

'Copy the selected range into memory.
rngGen.Copy
rngApp.Copy
rngspc.Copy

'Retrieve the data from then copied range.
Set Data = New DataObject
Data.GetFromClipboard

'Add data to the mainproperties of the e-mail's document.
With noDocument
  .Form = "Memo"
  .SendTo = vaRecipient
  .Subject = stSubject
  'Retrieve the data from the clipboard.
  .Body = Data.GetText & " " & stMsg
  .SaveMessageOnSend = True
End With

'Send the e-mail.
With noDocument
  .PostedDate = Now()
  .send 0, vaRecipient
End With

'Release objects from memory.
Set noDocument = Nothing
Set noDatabase = Nothing
Set noSession = Nothing


'Activate Excel for the user.
AppActivate "Microsoft Excel"

'Empty the clipboard.
Application.CutCopyMode = False

MsgBox "The e-mail has successfully been created and distributed.", vbInformation

End Sub

然后在另一张纸上有两个功能部分

如果过程位于工作表模块中,则应使用以下命令调用它们:

Sheet_Object_Name.Send_Unformatted_Rangedata (2)

第二种选择是将过程移至ThisWorkbook模块,然后将您的代码移至:

ThisWorkbook.Send_Unformatted_Rangedata (2)

应该工作正常。

另一种解决方案是将一个单独的模块添加到您的项目中(使用Insert->Module ),在其中移动过程,然后您可以使用以下简单方法从其他模块中调用这些过程:

Send_Unformatted_Rangedata (2)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM