问题
My ".send" is not working however my ".display" is? I keep getting an object defined error. and when I debug it highlights the ".send". Any suggestions?
Sub SendEmails()
Dim olApp As Outlook.Application
Dim olMail As Outlook.MailItem
For i = 8 To 10
Set olApp = New Outlook.Application
Set olMail = olApp.CreateItem(olMailItem)
With olMail
.BCC = Cells(i, 1).Value
.Subject = Cells(i, 2).Value
.Body = "Hello " & Cells(i, 3).Value & "," & vbNewLine & vbNewLine & Cells(i, 4).Value & vbNewLine & vbNewLine & Cells(i, 5).Value & vbNewLine & Cells(i, 6).Value
.Attachments.Add "H:\Shared\Public\Focus 6\Private Equity\Send Emails\2017-01-18\EY - Capitalizing on Opportunities (PE O&G).pdf"
.Send
End With
Next
Set olMail = Nothing
Set olApp = Nothing
End Sub
回答1:
As @Nathan_Sav mentioned, you can probably play around with your Outlook security features. As an alternative, you can experiment with the .SendKeys
function...though please note that this is not a very reliable method, especially if you are going to need other people/other machines to try and use this macro.
But if you replace
.Send
With
.SendKeys "%S"
It might work for you.
回答2:
.Display
will give you a view of the email. If this step works, then .Send
will work efficiently. When using .Send
you will receive a message box acknowledging that a 3rd party program is trying to access outlook (If everything is configured correctly). To fix this if it is not configured, open the workbook and then open the VBE (Alt+F11). From there goto Tools-->References and uncheck any that are marked "Missing". If possible, look for the appropriate Outlook Object library and check it.
Sub SendEmails()
Dim olApp As Outlook.Application
Dim olMail As Outlook.MailItem
For i = 8 To 10
Set olApp = New Outlook.Application
Set olMail = olApp.CreateItem(olMailItem)
With olMail
.BCC = Cells(i, 1).Value
.Subject = Cells(i, 2).Value
.Body = "Hello " & Cells(i, 3).Value & "," & vbNewLine & vbNewLine & Cells(i, 4).Value & vbNewLine & vbNewLine & Cells(i, 5).Value & vbNewLine & Cells(i, 6).Value
.Attachments.Add "H:\Shared\Public\Focus 6\Private Equity\Send Emails\2017-01-18\EY - Capitalizing on Opportunities (PE O&G).pdf"
.Display
End With
Next
Set olMail = Nothing
Set olApp = Nothing
End Sub
回答3:
Most organizational emails have a mandatory document/email labelling policy where you have to choose between options like public, restricted, confidential, etc. If your outlook is setup so that you must choose one of these options before sending the email, this might be the reason for getting an error. There aren't many solutions that I know of for this issue, other than deactivating the plugin that requires you to label emails before sending them. But maybe someone else may have other suggestions.
来源:https://stackoverflow.com/questions/41724452/send-not-working-vba-but-display-is