提问人:user22737370 提问时间:10/14/2023 最后编辑:Eugene Astafievuser22737370 更新时间:10/14/2023 访问量:95
我可以从我的辅助电子邮件地址发送带有宏的电子邮件吗?
Can I send emails with macro from my secondary email address?
问:
下面是我正在使用的宏,它有效。我想知道如何修改它,以便它从我的辅助邮箱发送电子邮件。提前感谢您的帮助。
Option Explicit
Public Sub Example()
Dim olApp As Object
Dim olMail As Object
Dim olRecip As Object
Dim olCCRecip As Object
Dim olAtmt As Object
Dim iRow As Long
Dim Recip As String
Dim CCRecip As String
Dim Subject As String
Dim Body As String
Dim Atmt As String
iRow = 2
Set olApp = CreateObject("Outlook.Application")
Dim Sht As Worksheet
Set Sht = ThisWorkbook.Worksheets("Email List")
Do Until IsEmpty(Sht.Cells(iRow, 1))
Recip = Sht.Cells(iRow, 1).Value
CCRecip = Sht.Cells(iRow, 2).Value
Subject = Sht.Cells(iRow, 3).Value
Body = Sht.Cells(iRow, 4).Value
Atmt = Sht.Cells(iRow, 5).Value ' Attachment Path
Set olMail = olApp.CreateItem(0)
With olMail
Set olRecip = .Recipients.Add(Recip)
.Subject = Subject
.Body = Body
Set olAtmt = .Attachments.Add(Atmt)
olRecip.Resolve
If CCRecip <> "" Then
Set olCCRecip = .Recipients.Add(CCRecip)
olCCRecip.Type = 2
olCCRecip.Resolve
End If
.Display 'If you want to automatically send mail without any notification, use .Send. If you want see mail
'before send, use .Display method.olRecip.Resolve
End With
iRow = iRow + 1
Loop
Set olApp = Nothing
End Sub
答:
0赞
kcp
10/14/2023
#1
iRow = 2
Set olApp = CreateObject("Outlook.Application")
Dim Sht As Worksheet Set Sht = ThisWorkbook.Worksheets("Email List")
Do Until IsEmpty(Sht.Cells(iRow, 1))
Recip = Sht.Cells(iRow, 1).Value
CCRecip = Sht.Cells(iRow, 2).Value
Subject = Sht.Cells(iRow, 3).Value
Body = Sht.Cells(iRow, 4).Value
Atmt = Sht.Cells(iRow, 5).Value ' Attachment Path
Set olMail = olApp.CreateItem(0)
'Get the other account by using the email or use index olMail.Session.Accounts.Item(1)
Set OutAccount = olMail.Session.Accounts.Item("[email protected]")
With olMail
Set olRecip = .Recipients.Add(Recip)
.Subject = Subject
.Body = Body
Set olAtmt = .Attachments.Add(Atmt)
olRecip.Resolve
If CCRecip <> "" Then
Set olCCRecip = .Recipients.Add(CCRecip)
olCCRecip.Type = 2
olCCRecip.Resolve
End If
'set the account to be used to send the email
Set .SendUsingAccount = OutAccount
.Display 'If you want to automatically send mail without any notification, use .Send. If you want see mail
'before send, use .Display method.olRecip.Resolve
End With
iRow = iRow + 1
Loop
Set olApp = Nothing End Sub
评论
0赞
user22737370
10/14/2023
不知道我做错了什么,它没有用。只显示我编辑的部分,因为它不会让我粘贴整个代码 将 olApp 调暗为对象 将 o1OutAccount 调暗为对象 将 iRow 调暗为长 将 OutApp 调暗为字符串 设置 olApp = CreateObject(“Outlook.Application”) 设置 o1OutAccount = OutApp.Session.Accounts.Item(“email”) End If '设置用于发送电子邮件的帐户 设置 .SendUsingAccount = o1OutAccount
0赞
kcp
10/14/2023
有错别字,我已经更新了,请再试一次
0赞
user22737370
10/14/2023
非常感谢您的帮助,仍然没有运气。在顶部,我将 Dim o1OutAccount 添加为 Object 和 Dim OutAccount As String,然后我添加您提供的内容 '使用电子邮件或使用索引 olApp.Session.Accounts.Item(1) Set OutAccount = olApp.Session.Accounts.Item(“email”) 并在末尾 '设置用于发送电子邮件的帐户 Set .SendUsingAccount = o1OutAccount ;对于 Set OutAccount = o1App ...部分。它显示一个对象错误,当我在 OutAccount 前面添加 o1 时,我收到无效的过程调用或参数错误
0赞
user22737370
10/14/2023
对不起,我希望我能发送这个,这样就更清楚了
0赞
kcp
10/14/2023
又是我,抱歉我使用了错误的对象实例,请再试一次,我已经对上面的代码进行了更改,请确保将“[email protected]”替换为您的辅助帐户的有效电子邮件或使用 olMail.Session.Accounts.Item(1) 或 olMail.Session.Accounts.Item(0)
1赞
Eugene Astafiev
10/14/2023
#2
如果在 Outlook 中配置了第二个邮箱,则可以使用 MailItem.SendUsingAccount 属性,该属性返回或设置一个 Account 对象,该对象表示要发送 MailItem 的帐户。
Dim oAccount As Outlook.account
With olMail
Set olRecip = .Recipients.Add(Recip)
.Subject = Subject
.Body = Body
Set olAtmt = .Attachments.Add(Atmt)
olRecip.Resolve
If CCRecip <> "" Then
Set olCCRecip = .Recipients.Add(CCRecip)
olCCRecip.Type = 2
olCCRecip.Resolve
End If
For Each oAccount In Application.Session.Accounts
If oAccount.AccountType = olPop3 Then
Set oMail.SendUsingAccount = oAccount
End If
Next
.Display 'If you want to automatically send mail without any notification, use .Send. If you want see mail
'before send, use .Display method.olRecip.Resolve
End With
或者可以在MSDN页面上找到的完整示例:
Sub SendUsingAccount()
Dim oAccount As Outlook.account
For Each oAccount In Application.Session.Accounts
If oAccount.AccountType = olPop3 Then
Dim oMail As Outlook.MailItem
Set oMail = Application.CreateItem(olMailItem)
oMail.Subject = "Sent using POP3 Account"
oMail.Recipients.Add ("[email protected]")
oMail.Recipients.ResolveAll
Set oMail.SendUsingAccount = oAccount
oMail.Send
End If
Next
End Sub
如果 Exchange 帐户具有代表他人发送的足够权限,则可以使用 MailItem.SentOnBehalfOfName 属性,该属性返回一个字符串,该字符串指示邮件的预期发件人的显示名称。
评论
ShowAllAccounts()