Forum Discussion
Sending Outlook mail using Excel VBA
Wayne-
Sorry I think I misunderstood your issue. I haven't fiddled around with Outlook VBA in a while, but maybe try changing this line:
Set objInsp = objMail.GetInspector.CurrentItem
If that doesn't work try these two modifications:
Add this to your Dim Statements:
Dim outApp As Outlook.Application
Then change this code as well:
Set objInsp = outApp.ActiveExplorer.CurrentItem
If neither of these will work. Please let me know and I can do additional testing to try and replicate your issue.
Sorry about the delay. I work on a Mac but this code has to be tested on a Windows machine.
Anyway, the .CurrentItem attempt returned a "type Mismatch" error. My tester reports that .CurrentView was on the list of suggestions but .CurrentItem was not.
The second attempt, the Dim statement and the objInspector line returned an error "object variable or with block variable not set"
- Matt MickleMay 25, 2018Bronze Contributor
Wayne-
No problem. I'll run some additional tests. However, before I do, I wanted to ask a few questions
I see MS Word VBA references in your code which makes me wonder:
1. If possible would you like to copy data from Excel directly to the email?
2. Are you trying to copy a table from word to the email?
3. Is word being used as some type of intermediary? If so what is the purpose?
The only reason I ask about these items is because there is a way to copy directly from excel and maintain formatting, but if you are trying to copy the table from word then the code will be different of course....
- DeletedMay 26, 2018
Nuts. I had a more detailed answer but I put a link in it and that got it removed as spam. I've asked for it to be restored.
Anyway,
1. Yes
2. Only as a way to get the Excel selection into an email with format retained.
3. Yes, an intermediate editor step ONLY, so that the Excel formatting is not lost. Apparently Outlook VB cannot do that.
[update] We're getting closer. We used SendKeys to tab forward into the body while we were setting the .Subject of the objMail. (Using .Body to add text causes a loss of formatting.) With this change, the macro works all the time EXCEPT when the last created email is open. I find this bizarre but by trial and error we discovered that closing or sending the most-recent email in Outlook allows the macro to succeed even if older Outlook mail windows remain open.
I tried a kludge to open and then close a dummy email within the macro, before creating the desired email. This sadly didn't work. There's something crucial about actually looking at the email in Outlook and then closing it.
- Matt MickleMay 26, 2018Bronze Contributor
Typically SendKeys should be used as a last alternative as it can behave unexpectedly. Ironically, the code that you need to use was also created by Ron De Bruin. It turns a range into HTML. THat way you can just put the HTML in the email rather than having to use MS Word as an intermediary.
Here is the original website link: https://www.rondebruin.nl/win/s1/outlook/bmail2.htm
Here is his code that I have modified to work for my needs. I believe this should do what you want. If it doesn't work I can look at another project where I did something similar. :
Sub CreateAnHTMLEmail() 'Alter Send Email Sub Procedure Details in order 'To Modify Subject, Recipients...etc... 'The range put in this Sub is....the one that will be 'put into the email... i.e. This puts A1:G21 in the email Call SendEmail(Sheets("Sheet1").Range("A1:G21")) End Sub Sub SendEmail(MyHTMLRng As Range) Dim OutApp As Object Dim OutMail As Object Dim OutlookOpened As Boolean OutlookOpened = False On Error Resume Next Set OutApp = GetObject(, "Outlook.Application") If OutApp Is Nothing Then Set OutApp = CreateObject("Outlook.Application") OutlookOpened = True End If Set OutMail = OutApp.CreateItem(0) On Error Resume Next With OutMail .To = "name@abc.com" .cc = "fox@test.com; otherperson@test.com" .Subject = "My Subject Line" .htmlbody = RangetoHTML(MyHTMLRng) .Display 'allows email to be displayed and checked before manually sending it '.Send 'This sends the email End With On Error GoTo 0 Set OutMail = Nothing If OutlookOpened Then OutApp.Quit Set OutApp = Nothing End Sub Function RangetoHTML(rng As Range) ' Changed by Ron de Bruin 28-Oct-2006 ' Working in Office 2000-2010 Dim fso As Object Dim ts As Object Dim TempFile As String Dim TempWB As Workbook TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm" 'Copy the range and create a new workbook to past the data in rng.Copy Set TempWB = Workbooks.Add(1) With TempWB.Sheets(1) .Cells(1).PasteSpecial Paste:=8 .Cells(1).PasteSpecial xlPasteValues, , False, False .Cells(1).PasteSpecial xlPasteFormats, , False, False .Cells(1).Select Application.CutCopyMode = False On Error Resume Next .DrawingObjects.Visible = True .DrawingObjects.Delete On Error GoTo 0 End With 'Publish the sheet to a htm file With TempWB.PublishObjects.Add( _ SourceType:=xlSourceRange, _ Filename:=TempFile, _ Sheet:=TempWB.Sheets(1).Name, _ Source:=TempWB.Sheets(1).UsedRange.Address, _ HtmlType:=xlHtmlStatic) .Publish (True) End With 'Read all data from the htm file into RangetoHTML Set fso = CreateObject("Scripting.FileSystemObject") Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2) RangetoHTML = ts.ReadAll ts.Close RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _ "align=left x:publishsource=") 'Close TempWB TempWB.Close savechanges:=False 'Delete the htm file we used in this function Kill TempFile Set ts = Nothing Set fso = Nothing Set TempWB = Nothing End Function
- DeletedMay 25, 2018
Matt Mickle wrote:1. If possible would you like to copy data from Excel directly to the email?
Exactly. Trying to paste in formatted Excel cells. Every Outlook example I could find creates an attachment to excerpt the Excel selection, and attaches that to the email in Outlook. I need to put the formatted Excel selection directly into the email. I found a description of the VBA limitation with Outlook that prohibits this, and that's where using the WordEditor code comes in. As noted earlier this does work but only in a limited set of circumstances (no other open Outlook doc).
2. Are you trying to copy a table from word to the email?
Ultimately, no, I have no need for Word except to enable pasting in formatted Excel content.
3. Is word being used as some type of intermediary? If so what is the purpose?
Yes, intermediary ONLY, to enable pasting formatted Excel content for the ultimate destination - an Outlook email.
FWIW, I found some code here...
https://www.datanumen.com/blogs/quickly-close-open-items-except-current-one-outlook/
...that works to close any other open Outlook windows and ultimately puts all the text in the right place. However my tester notes that here customers signature is lost as well as the email formatting. I believe the Excel information retains its format but I can check.