wpsvba怎么发送邮件

博主:小盒子小盒子 11-19 3165

wpsvba可以支持用户进行发送邮件,vba插件是wps中强大的功能,需要提前将vba插件安装到WPS中才能使用。我们使用一些代码编程就能够实现快速发送邮件功能。

wpsvba怎么发送邮件:

1、首先将WPS中创建一个邮件地址,不同的名称也可以,在程序中用对应名称即可。

2、收件人地址从B2开始,抄送人地址从D2开始,密送收件人地址从F2开始。

3、G2是主题、H2写正文。A列、G列、E列可以填上对应的收件人名字,非必填的。

4、发送电子邮件代码

Public Sub 发送电子邮件()

Dim OutlookApp As Outlook.Application

Dim newMail As Outlook.mailitem

Dim myAttachments As Outlook.Attachments

Dim n As Integer, i As Integer

Dim ws As Worksheet

On Error Resume Next

Set ws = Worksheets("邮件地址")

n = ws.Range("B65536").End(xlUp).Row

address = Worksheets("邮件地址").Cells(2, 2)

For i = 2 To n

If i < n Then

address = address & ";" & Worksheets("邮件地址").Cells(i + 1, 2)

End If

Next i

'CC

n = ws.Range("D65536").End(xlUp).Row

addressCC = Worksheets("邮件地址").Cells(2, 4)

For i = 2 To n

If i < n Then

addressCC = addressCC & ";" & Worksheets("邮件地址").Cells(i + 1, 4)

End If

Next i

'BCC

n = ws.Range("F65536").End(xlUp).Row

addressBCC = Worksheets("邮件地址").Cells(2, 6)

For i = 2 To n

If i < n Then

addressBCC = addressBCC & ";" & Worksheets("邮件地址").Cells(i + 1, 6)

End If

Next i

Set OutlookApp = New Outlook.Application

wbStr = ThisWorkbook.FullName '指定要发送发工作簿名称字符串

Set newMail = OutlookApp.CreateItem(olMailItem) '创建新邮件

With newMail

.Subject = Worksheets("邮件地址").Cells(2, 7) '设置邮件主题

.Body = Worksheets("邮件地址").Cells(2, 8) '设置邮件正文

'发送附件

Set myAttachments = newMail.Attachments

myAttachments.Add wbStr, olByValue, 1, "工作簿"

.To = address '收件人

.CC = addressCC '抄送

.BCC = addressBCC '密送

.Send '开始发送

End With

End Sub

5、最后添加加载菜单中按钮,链接到编写的程序模块;

然后我们在点击一下按钮运行程序,将邮件发送给相关收件人;

以后在需要发邮件,只需要点这个按钮即可。

未命名_副本.gif+0

发布于:2024-11-19,除非注明,否则均为我的鸡盒原创文章,转载请注明出处。