How to compose email in office 365 outlook using VBA?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to compose email in office 365 outlook using VBA?



I am able to compose email in Desktop Microsoft outlook using the below mentioned code. But this code is not working for Microsoft office 365 web outlook. Please provide me Code/Suggestion for composing mail in Microsoft office 365 web outlook.


Sub Mail_small_Text_Outlook()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

strbody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"

On Error Resume Next
With OutMail
.To = "ron@debruin.nl"
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = strbody
'You can add a file like this
'.Attachments.Add ("C:test.txt")
.Send 'or use .Display
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
End Sub




1 Answer
1



Based on my research, I'm afraid you can't. Using VBA like yours requires desktop Outlook to be installed. There might be a way to send mails via PowerShell. Just like this as below:


$olFolderDrafts = 16
$ol = New-Object -comObject Outlook.Application
$ns = $ol.GetNameSpace("MAPI")

# call the save method yo dave the email in the drafts folder
$mail = $ol.CreateItem(0)
$null = $Mail.Recipients.Add("XXX@YYY.ZZZ")
$Mail.Subject = "PS1 Script TestMail"
$Mail.Body = " Test Mail "
$Mail.save()
#get it back from drafts and update the body
$drafts = $ns.GetDefaultFolder($olFolderDrafts)
$draft = $drafts.Items | where $_.subject -eq 'PS1 Script TestMail'
$draft.body += "`n foo bar"
$draft.save()



Hope that give you new idea for this.



Thanks,



Simon






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS