Transfer Excel VBA Generated emails to a folder in outlook

QMAN223

New Member
Joined
Nov 24, 2021
Messages
36
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a requirement where I need to transfer the emails my Macro creates to a Draft folder in Outlook.

Is this possible?

Here is the Macro code that I use to generate the emails...

VBA Code:
Sub jec()
 For Each sh In ThisWorkbook.Sheets
   With CreateObject("Outlook.Application").CreateItem(0)
    .To = Join(Application.Transpose(sh.ListObjects(1).DataBodyRange.Columns(1)), ";")
    .CC = Join(Application.Transpose(sh.ListObjects(1).DataBodyRange.Columns(2)), ";")
    .Subject = "Training Report  - " & Format(DateAdd("d", 1 - Weekday(Now), Now), "dd-MM-yyyy")
    .Body = "Dear All" & vbCrLf & vbCrLf & "Please find attached the Weekly Training report." & vbCrLf & vbCrLf & "Kind Regards,"
    '.Attachments.Add
    .display '.send
   End With
 Next
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hi familiar code :)

Change .display to .Save
That will probably do the trick for you
 
Upvote 0
Solution
But why display first when saving at once is possible? ;)
 
Upvote 0
Depends on what the user wants to do. I'm just pointing out that you are not limited to use one or the other.
 
Upvote 0
Ahh yeah ofcourse, lets see
 
Upvote 0
Hi familiar code :)

Change .display to .Save
That will probably do the trick for you
AHAHAHA, I was going to message you before but I rather make it a new Post to help someone else out if they wanted to do something similar.
 
Upvote 0
Hi familiar code :)

Change .display to .Save
That will probably do the trick for you
Code works like a charm! Thanks JEC! What if I wanted to save emails to a particular folder in Outlook?
 
Upvote 0
Yes, this code moves the just saved draft mail to a subfolder of the inboxmail, in this case the folder "test".
The 6 in .GetDefaultfolder(6) means your normal inbox folder. ;)

VBA Code:
Sub jec()
 For Each sh In ThisWorkbook.Sheets
   Set olApp = CreateObject("outlook.application")
   With olApp.CreateItem(0)
    .To = Join(Application.Transpose(sh.ListObjects(1).DataBodyRange.Columns(1)), ";")
    .CC = Join(Application.Transpose(sh.ListObjects(1).DataBodyRange.Columns(2)), ";")
    .Subject = "Training Report  - " & Format(DateAdd("d", 1 - Weekday(Now), Now), "dd-MM-yyyy")
    .Body = "Dear All" & vbCrLf & vbCrLf & "Please find attached the Weekly Training report." & vbCrLf & vbCrLf & "Kind Regards,"
    .Save
    .Move olApp.GetNamespace("MAPI").Getdefaultfolder(6).Folders("test")
   End With
 Next
End Sub
 
Upvote 0
Yes, this code moves the just saved draft mail to a subfolder of the inboxmail, in this case the folder "test".
The 6 in .GetDefaultfolder(6) means your normal inbox folder. ;)

VBA Code:
Sub jec()
 For Each sh In ThisWorkbook.Sheets
   Set olApp = CreateObject("outlook.application")
   With olApp.CreateItem(0)
    .To = Join(Application.Transpose(sh.ListObjects(1).DataBodyRange.Columns(1)), ";")
    .CC = Join(Application.Transpose(sh.ListObjects(1).DataBodyRange.Columns(2)), ";")
    .Subject = "Training Report  - " & Format(DateAdd("d", 1 - Weekday(Now), Now), "dd-MM-yyyy")
    .Body = "Dear All" & vbCrLf & vbCrLf & "Please find attached the Weekly Training report." & vbCrLf & vbCrLf & "Kind Regards,"
    .Save
    .Move olApp.GetNamespace("MAPI").Getdefaultfolder(6).Folders("test")
   End With
 Next
End Sub
Oh I see. Cheers!

Is the '.GetNamespace("MAPI")' necessary for the code? What does that do?
 
Upvote 0

Forum statistics

Threads
1,214,407
Messages
6,119,332
Members
448,888
Latest member
Arle8907

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top