简体   繁体   English

Outlook 一直引用默认的 email 地址并忽略 vba 中指定的帐户

[英]Outlook keeps referring to default email address and ignoring the account specified in vba

I am using the following code to try to add another account during my automatic email creation however the vba keeps defaulting to the main email.我正在使用以下代码尝试在自动创建 email 期间添加另一个帐户,但是 vba 一直默认为主 email。 I want to use no_reply mailbox however it keeps using my firstname.lastname@company.com.我想使用 no_reply 邮箱,但它一直使用我的 firstname.lastname@company.com。 I even changed the no_reply as my default email by going into account settings in outlook and it still doesnt work.我什至通过进入 outlook 中的帐户设置将 no_reply 更改为我的默认 email ,但它仍然不起作用。 I checked while running the code if it is refering to the no_reply when it creates a new mail window, and it does at line Set OutAccount = myMail.Session.Accounts.Item(1) .我在运行代码时检查了它在创建新邮件 window 时是否引用了 no_reply,并且它在Set OutAccount = myMail.Session.Accounts.Item(1)行执行。 which shows as no_reply, However the actual email message shows my first.last@company.com.显示为 no_reply,但是实际的 email 消息显示我的 first.last@company.com。

Can some please help?有人可以帮忙吗?


Dim outlookApp As Outlook.Application
Dim myMail As Outlook.MailItem
Dim lastrow As Long
Dim i As Integer
Dim Sheet As Worksheet
Dim OutAccount As Outlook.Account

Application.ScreenUpdating = False

On Error Resume Next

lastrow = ThisWorkbook.Worksheets("Sheet1").Range("A1").End(xlDown).Row

For i = 2 To lastrow
    'If ThisWorkbook.Worksheets("Sheet2").Range("T" & i) = "No" Then
    
    Set outlookApp = New Outlook.Application
    Set myMail = outlookApp.CreateItem(olMailItem)
    'Set OutAccount = myMail.Session.Accounts.Item(1)
    source_file = ThisWorkbook.Worksheets("Sheet1").Range("E" & i).Value
    source_file2 = ThisWorkbook.Worksheets("Sheet1").Range("F" & i).Value
    
    Set Sheet = ThisWorkbook.Worksheets("Sheet1")
    
    myMail.Attachments.Add source_file
    myMail.Attachments.Add source_file2
    'Set myMail.SendUsingAccount = myMail.Session.Accounts.Item(1)
    myMail.To = ThisWorkbook.Worksheets("Sheet1").Range("D" & i).Value
    myMail.Subject = "Subject Line"
    myMail.HTMLBody = "whatever i want to write in the email"
      
    
    myMail.Display
    myMail.Send
    
    ThisWorkbook.Worksheets("Sheet1").Range("G" & i) = "Yes"
    'Else
    'End If

   
 Application.ScreenUpdating = True
 
Next i

End Sub

Adding the following line worked!添加以下行有效!

myMail.SentOnBehalfOfName = "blah@company.com"

I'd suggest iterating over all accounts configured in the profile and choose the required one.我建议遍历配置文件中配置的所有帐户并选择所需的帐户。 By using indexes you may choose a wrong account mistakenly.通过使用索引,您可能会错误地选择错误的帐户。

' Loop over the Accounts collection of the current Outlook session.
Dim accounts As Outlook.Accounts = application.Session.Accounts
Dim account As Outlook.Account
For Each account In accounts
   ' When the email address matches, return the account.
   If account.SmtpAddress = smtpAddress Then
      Return account
   End If
Next

See Send an email given the SMTP address of an account for more information.有关详细信息,请参阅给定帐户的 SMTP 地址发送 email

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

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