Vba 使用Excel通过Lotus Notes发送电子邮件

Vba 使用Excel通过Lotus Notes发送电子邮件,vba,email,excel,Vba,Email,Excel,我正在编写通过IBM Lotus Notes发送电子邮件的宏代码,我可以发送给客户,但内容错误,我已将电子邮件内容保存在工作表“概述”中,如下所示: 但它会自动向一位客户发送一封包含错误内容的电子邮件,如“是”和“否”。我现在对此一无所知,非常感谢您的帮助 以下是全部内容: Sub Send_Unformatted_Rangedata(i As Integer) Dim noSession As Object, noDatabase As Object, noDocument As Object

我正在编写通过IBM Lotus Notes发送电子邮件的宏代码,我可以发送给客户,但内容错误,我已将电子邮件内容保存在工作表“概述”中,如下所示:

但它会自动向一位客户发送一封包含错误内容的电子邮件,如“是”和“否”。我现在对此一无所知,非常感谢您的帮助

以下是全部内容:

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.
'Change Microsoft Excel to Excel
AppActivate "Excel"

'Empty the clipboard.
Application.CutCopyMode = False

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

End Sub

Sub Send_Formatted_Range_Data(i As Integer)
Dim oWorkSpace As Object, oUIDoc As Object
Dim rnBody As Range
Dim lnRetVal As Long
Dim stTo As String
Dim stCC As String
Dim stSubject As String
Const stMsg As String = "An e-mail has been succesfully created and saved."

Dim rngGen As Range
Dim rngApp As Range
Dim rngspc As Range

stTo = Sheets("Summary").Cells(i, "U").Value
stCC = Sheets("Summary").Cells(i, "V").Value
stSubject = "E-Mail For Approval for " + (Sheets("Summary").Cells(i, "A").Value) + "  for the Project  " + Replace(ActiveWorkbook.Name, ".xls", "")

'Check if Lotus Notes is open or not.
lnRetVal = FindWindow("NOTES", vbNullString)

If lnRetVal = 0 Then
    MsgBox "Please make sure that Lotus Notes is open!", vbExclamation
    Exit Sub
End If

Application.ScreenUpdating = False

 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

rngGen.Copy
rngApp.Copy
rngspc.Copy

'Instantiate the Lotus Notes COM's objects.
Set oWorkSpace = CreateObject("Notes.NotesUIWorkspace")

On Error Resume Next

Set oUIDoc = oWorkSpace.ComposeDocument("", "mail\xldennis.nsf", "Memo")
On Error GoTo 0

Set oUIDoc = oWorkSpace.CurrentDocument

'Using LotusScript to create the e-mail.
Call oUIDoc.FieldSetText("EnterSendTo", stTo)
Call oUIDoc.FieldSetText("EnterCopyTo", stCC)
Call oUIDoc.FieldSetText("Subject", stSubject)

'If You experience any issues with the above three lines then replace it with:
'Call oUIDoc.FieldAppendText("EnterSendTo", stTo)
'Call oUIDoc.FieldAppendText("EnterCopyTo", stCC)
'Call oUIDoc.FieldAppendText("Subject", stSubject)

'The can be used if You want to add a message into the created document.
Call oUIDoc.FieldAppendText("Body", vbNewLine & stBody)

'Here the selected range is pasted into the body of the outgoing e-mail.
Call oUIDoc.GoToField("Body")
Call oUIDoc.Paste

'Save the created document.
Call oUIDoc.Save(True, False, False)
'If the e-mail also should be sent then add the following line.
'Call oUIDoc.Send(True)

'Release objects from memory.
Set oWorkSpace = Nothing
Set oUIDoc = Nothing

With Application
    .CutCopyMode = False
    .ScreenUpdating = True
End With

MsgBox stMsg, vbInformation

'Activate Lotus Notes.
 AppActivate ("Notes")
'Last edited Feb 11, 2015 by Peter Moncera

End Sub

剪贴板将被您所做的多个副本替换

要查看电子邮件并手动发送,请添加此

CreateObject(“Notes.NotesUIWorkspace”).EDITDOCUMENT True,oUIDoc
AppActivate“>”和oUIDoc.subject

下面
调用oUIDoc.Save(真、假、假)


由于不再有lotus notes,因此无法测试它是否正常工作。但这与我在上一份工作中使用的是类似的。

复制到内存中。下一份会取代上一份吗?因此,您正在执行
rngGen.copy
rngApp.copy
rngspc.copy
,但不会将所有这些都替换为
rngspc.copy
,因为这是最近要复制到内存中的。我很确定excel在未来只能有一件事clipboard@Sam谢谢,你知道如何手动发送电子邮件吗?很抱歉,我只能明天尝试,因为我的笔记本电脑无法在家连接到互联网。您好,该方法似乎不起作用,错误消息说
需要对象
它在
AppActivate
部分失败了吗?如果是这样,请将
oUIDoc.subject
替换为
stSubject
它正在运行
调用oUIDoc.Save(真、假、假)
它以前是否失败过?因为那已经是你代码的一部分了
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.
'Change Microsoft Excel to Excel
AppActivate "Excel"

'Empty the clipboard.
Application.CutCopyMode = False

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

End Sub

Sub Send_Formatted_Range_Data(i As Integer)
Dim oWorkSpace As Object, oUIDoc As Object
Dim rnBody As Range
Dim lnRetVal As Long
Dim stTo As String
Dim stCC As String
Dim stSubject As String
Const stMsg As String = "An e-mail has been succesfully created and saved."

Dim rngGen As Range
Dim rngApp As Range
Dim rngspc As Range

stTo = Sheets("Summary").Cells(i, "U").Value
stCC = Sheets("Summary").Cells(i, "V").Value
stSubject = "E-Mail For Approval for " + (Sheets("Summary").Cells(i, "A").Value) + "  for the Project  " + Replace(ActiveWorkbook.Name, ".xls", "")

'Check if Lotus Notes is open or not.
lnRetVal = FindWindow("NOTES", vbNullString)

If lnRetVal = 0 Then
    MsgBox "Please make sure that Lotus Notes is open!", vbExclamation
    Exit Sub
End If

Application.ScreenUpdating = False

 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

rngGen.Copy
rngApp.Copy
rngspc.Copy

'Instantiate the Lotus Notes COM's objects.
Set oWorkSpace = CreateObject("Notes.NotesUIWorkspace")

On Error Resume Next

Set oUIDoc = oWorkSpace.ComposeDocument("", "mail\xldennis.nsf", "Memo")
On Error GoTo 0

Set oUIDoc = oWorkSpace.CurrentDocument

'Using LotusScript to create the e-mail.
Call oUIDoc.FieldSetText("EnterSendTo", stTo)
Call oUIDoc.FieldSetText("EnterCopyTo", stCC)
Call oUIDoc.FieldSetText("Subject", stSubject)

'If You experience any issues with the above three lines then replace it with:
'Call oUIDoc.FieldAppendText("EnterSendTo", stTo)
'Call oUIDoc.FieldAppendText("EnterCopyTo", stCC)
'Call oUIDoc.FieldAppendText("Subject", stSubject)

'The can be used if You want to add a message into the created document.
Call oUIDoc.FieldAppendText("Body", vbNewLine & stBody)

'Here the selected range is pasted into the body of the outgoing e-mail.
Call oUIDoc.GoToField("Body")
Call oUIDoc.Paste

'Save the created document.
Call oUIDoc.Save(True, False, False)
'If the e-mail also should be sent then add the following line.
'Call oUIDoc.Send(True)

'Release objects from memory.
Set oWorkSpace = Nothing
Set oUIDoc = Nothing

With Application
    .CutCopyMode = False
    .ScreenUpdating = True
End With

MsgBox stMsg, vbInformation

'Activate Lotus Notes.
 AppActivate ("Notes")
'Last edited Feb 11, 2015 by Peter Moncera

End Sub