通过excel vba发送邮件时在Outlook中选择帐户 [英] choose account in outlook when send mail via excel vba

查看:723
本文介绍了通过excel vba发送邮件时在Outlook中选择帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Excel中的VBA中的Outlook中的特定帐户发送邮件,我被代码卡住了,我遍历了论坛,但仍然无法正常工作

I want to send mails from a specific account in outlook from VBA in excel and Im stuck with my code, i went over and over the forums but it still doesnt work

如果有人可以帮助我,我会向您展示我的代码,这将非常非常好

I show you my code if anyone could help me it would be very very nice

Sub SendMail()

Dim objOutlook As Object
Dim objMail As Object
Dim ws As Worksheet

Set objOutlook = CreateObject("Outlook.Application")
Set ws = ActiveSheet
Dim signature As String
Dim LstRow As Long
LstRow = ws.Cells(Rows.Count, 1).End(xlUp).Row

Dim oAccount As Outlook.Account

For Each oAccount In Outlook.Application.Session.Accounts

If oAccount = "mymail@server.com" Then

For Each cell In ws.Range("A4:A" & LstRow)

Set objMail = objOutlook.CreateItem(0)
signature = objMail.Body
    With objMail
        .To = cell.Value
        .Subject = cell.Offset(0, 1).Value
        .Body = cell.Offset(0, 2).Value & vbNewLine & signature
        .Attachments.Add cell.Offset(0, 3).Value
        .DeferredDeliveryTime = "15/03/2018 10:00:00 PM"
        .SendUsingAccount = oAccount
        .send
    End With

    Set objMail = Nothing
Next cell
Else
End If

Next
Set ws = Nothing
Set objOutlook = Nothing

End Sub

推荐答案

解决方案只是将Set放在.SendUsingAccount前面

The solution is just to put Set in front of .SendUsingAccount

 Set objMail = objOutlook.CreateItem(0)
signature = objMail.Body
   With objMail
    .To = cell.Value
    .Subject = cell.Offset(0, 1).Value
    .Body = cell.Offset(0, 2).Value & vbNewLine & signature
    .Attachments.Add cell.Offset(0, 3).Value
    .DeferredDeliveryTime = "15/03/2018 10:00:00 PM" 'need to comment here to run better
   Set .SendUsingAccount = oAccount
    .send
End With

还要感谢Maddy,我在deferredDeliveryTime之后发表了评论,它通过了oAccount顺利进行

And also Thanks to Maddy i commented after the deferredDeliveryTime and it went well through the oAccount

这篇关于通过excel vba发送邮件时在Outlook中选择帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆