How to disable e-mail creation screen displaying in outlook? - excel

Below code displays e-mail creation screen in outlook each time i run it. i tried removing .display from the second function but it gives run-time error.
I am new to VBA macros ,Please advice on how to hide the e-mail creation screen for each new e-mail that is being triggered. thanks a lot in advance.
Public Sub loopCheck()
Dim NumRows As Integer
Dim eID As String
Dim eName As String
Dim eEmail As String
Dim supportGroup As String
Dim managerEmail As String
Dim acName As String
Dim x As Integer
Application.ScreenUpdating = False
NumRows = Worksheets("Data").Range("A5", Range("A5").End(xlDown)).Rows.Count ' Set numrows = number of rows of data.
Worksheets("Data").Range("A5").Select ' Select first record.
For x = 1 To NumRows ' Establish "For" loop to loop "numrows" number of times.
eID = Worksheets("Data").Range("A" & x + 4).Value
eName = Worksheets("Data").Range("B" & x + 4).Value
eEmail = Worksheets("Data").Range("C" & x + 4).Value
supportGroup = Worksheets("Data").Range("F" & x + 4).Value
managerEmail = Worksheets("Data").Range("G" & x + 4).Value
acName = Worksheets("Data").Range("I" & x + 4).Value
'Prepare table to be sent locally.
Worksheets("Data").Range("AA5").Value = eID
Worksheets("Data").Range("AB5").Value = eName
Worksheets("Data").Range("AC5").Value = eEmail
Worksheets("Data").Range("AF5").Value = supportGroup
managerEmail = managerEmail + ";" + Worksheets("Data").Range("AA1").Value
'Call Emails function.
Call Emails(eEmail, managerEmail)
ActiveCell.Offset(1, 0).Select
Next
Application.ScreenUpdating = True
End Sub
Public Sub Emails(y As String, z As String)
Dim outlook As Object
Dim newEmail As Object
Dim xInspect As Object
Dim pageEditor As Object
Dim a As String
Dim b As String
a = y
b = z
Set outlook = CreateObject("Outlook.Application")
Set newEmail = outlook.CreateItem(0)
With newEmail
.To = a
.CC = b
.BCC = ""
.Subject = "test loop"
.Body = ""
.display
Set xInspect = newEmail.GetInspector
Set pageEditor = xInspect.WordEditor
Worksheets("Data").Range("AA4:AF5").Copy
pageEditor.Application.Selection.Start = Len(.Body)
pageEditor.Application.Selection.End = pageEditor.Application.Selection.Start
pageEditor.Application.Selection.PasteAndFormat (wdFormatPlainText)
.display
.send
Set pageEditor = Nothing
Set xInspect = Nothing
End With
Set newEmail = Nothing
Set outlook = Nothing
End Sub

You have two instances of .display in your Emails sub .If first instance is suppressed it locks the editor and would not allow program to run. Second instance can be commented out and .send is enough for program working. Still first instance will display on screen . In order to totally disable e-mail creation screen display please take reference from programs at roundebruin which covers all types of possibilities of sending emails without displaying email creation screen. Method followed by you is a preferred method for embedding of images or charts in the HTML body.
your adopted code is similar to SO Question . Please refer to opening comments in Answer by Dmitry Streblechenko " --
You should also use MailItem.GetInspector instead of
Application.ActiveInspector since the message is not yet displayed.
So if you want to suppress e-mail creation screen display, please adopt other approach as suggested earlier.
Further Eugene Astafiev also mentioned while answering a question HERE
That's a known issue in Outlook. You have to call the Display method first to get the inspector visible.
Otherwise it won't work.
I think you can not suppress display of e-mail creation screen display by invoking this approach.

Related

Reply to selected Outlook emails using Excel VBA

Using Excel VBA, I want to reply to emails which I selected/highlighted inside the Outlook application.
There are different email messages and subject lines based on the order which I selected the email messages.
There are replies to the wrong email. It should reply to those which I highlighted in Outlook.
For example when I selected three emails there are instances that two replied correctly but the other one replied to an email which I did not highlight.
Sub SendEmail()
Dim OutlookApp As Object
Dim OutlookMail As Object
i = 1
Do While Not IsEmpty(Cells(i + 1, 4))
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutlookApp.ActiveExplorer.Selection.Item(i)
Dim OutlookConversation As Object
Set OutlookConversation = OutlookMail.GetConversation
Dim OutlookTable As Object
Set OutlookTable = OutlookConversation.GetTable
Dim OutlookAr As Variant
OutlookAr = OutlookTable.GetArray(OutlookTable.GetRowCount)
Dim OutlookReplyToThisMail As Object
Set OutlookReplyToThisMail = OutlookMail.Session.GetItemFromID(OutlookAr(UBound(OutlookAr), 0))
With OutlookReplyToThisMail.ReplyAll
.Subject = Sheet1.Cells(1 + i, 15) & "_" & .Subject
.HTMLBody = "<p style='font-family:calibri;font-size:13'>" & _
Sheet1.Cells(34, 2 + i) & "<br>" & "<br>" & _
Sheet1.Cells(35, 2 + i) & "<br>" & "<br>" & _
Sheet1.Cells(36, 2 + i) & Signature & .HTMLBody
.Display
End With
i = i + 1
Loop
End Sub
First of all, creating a new Outlook Application instance in the loop is not actually a good idea:
Do While Not IsEmpty(Cells(i + 1, 4))
Set OutlookApp = CreateObject("Outlook.Application")
Instead, consider moving the creation line above before the loop:
Set OutlookApp = CreateObject("Outlook.Application")
Do While Not IsEmpty(Cells(i + 1, 4))
In the code you are iterating over Excel cells and get corresponding selected items in Outlook.
it should only reply those which i highlighted in outlook email.
If you need to iterate over all selected items in Outlook you need to not rely on the Excel's data and have got a separate loop based on the number of selected items. For example:
Dim myOlExp As Outlook.Explorer
Dim myOlSel As Outlook.Selection
Set myOlExp = OutlookApplication.ActiveExplorer
Set myOlSel = myOlExp.Selection
For x = 1 To myOlSel.Count
If myOlSel.Item(x).Class = OlObjectClass.olMail Then
' For mail item, use the SenderName property.
Set oMail = myOlSel.Item(x)
Debug.Print oMail.SenderName
End If
Next

Send Email Automatically when Triggered

I have found a lot of examples using KuTools, but I don't want this to be interactive, I want the dates in my spreadsheet to trigger an email to be sent.
So the issue I am currently having, is that the Ranges I am trying to fetch are being returned as Nothing, hence, my program doesn't work. Hope someone can help me.
Public Sub sendEmail()
Dim dueDates As Excel.Range
Dim certs As Excel.Range
Dim remainingDays As Long
Dim triggerDate As Long
Set dueDates = ThisWorkbook.Worksheets("worksheetName").Range("D3:D12")
Set certs = ThisWorkbook.Worksheets("worksheetName").Range("C3:C12")
remainingDays = 90
triggerDate = today.AddDays(remainingDays)
Dim toWhom As Excel.Range
Dim subject As String
Dim bodyOpen As String
Dim bodyClose As String
Dim bodyFull As String
Dim mail As Object
Dim outlook As Object
Set toWhom = ThisWorkbook.Worksheets("worksheetName").Range("B16")
subject = "Cert renewal reminder"
bodyOpen = "<HTML><BODY><br>"
bodyClose = "<br></HTML></BODY>"
bodyFull = ""
On Error Resume Next
For Each cell in dueDates
If cell.Value Is Nothing Then Exit Sub (my code is breaking at this line)
If cell.Value <> "" Then
If cell.Value < triggerDate Then
remainingDays = cell.Value - today
bodyFull = bodyOpen + "Certificate " + certs.ActiveCell.row.Value + " will expire in " + remainingDays + " days.<br>Official expiration day = " + cell.Value + ".<br>Please schedule renewal soon<br>" + bodyClose
Set outlook = CreateObject("Outlook.Application")
Set mail = outlook.CreateItem(0)
If toWhom <> "" Then
With mail
.subject = subject
.To = toWhom
.HTMLBody = bodyFull
.Display
.Send
End With
On Error GoTo 0
Set mail = Nothing
End If
End If
End If
End If
Next cell
Set outlook = Nothing
End Sub
This is my first macro, never used VBA before so I may be miss-using types and such things, any advise is taken.

How can I fix an automatic email reminder issue

The code is asking me who to send this to, but the purpose of the email would be to send it to multiple people at a time. The problem in the code is ".send". I need the code to send it automatically without having to manually input each email within the code.
The first thing I tried was being specific to who I wanted the email to be send with in the code itself. It did not work.
Sub SendReminderMail()
Dim OutLookApp As Object
Dim OutLookMailItem As Object
Dim iCounter As Integer
Dim MailDest As String
Dim StrFrom As String
Dim StrTo As String
Set OutLookApp = CreateObject("Outlook.Application")
Set OutLookMailItem = OutLookApp.CreateItem(0)
With OutLookMailItem
MailDest = ""
For iCounter = 1 To WorksheetFunction.CountA(Columns(4))
If MailDest = "" And Cells(iCounter, 8).Offset(0, -1) = "Send Reminder" Then
MailDest = Cells(iCounter, 8).Value
ElseIf MailDest <> "" And Cells(iCounter, 8).Offset(0, -1) = "send Reminder" Then
MailDest = MailDest & ";" & Cells(iCounter, 8).Value
End If
Next iCounter
.BCC = MailDest
.Subject = "FYI"
.Body = "Reminder: The patient is overdue please contact the patient to reschedule. Please ignore if appointment has been made."
.Send
End With
Set OutLookMailItem = Nothing
Set OutLookApp = Nothing
End Sub
The error I get is "run-time error 462" stating the remote machine does not exist.
I refactored your code (moved variable declarations directly before initialization, changed Integer to Long, reordered the statements to group the ones related to Outlook together, and removed the superfluous Settings to Nothing).
Sub SendReminderMail()
Dim MailDest As String: MailDest = vbNullString
Dim iCounter As Long: For iCounter = 1 To WorksheetFunction.CountA(Columns(4))
If MailDest = "" And Cells(iCounter, 8).Offset(0, -1) = "Send Reminder" Then
MailDest = Cells(iCounter, 8).Value
ElseIf MailDest <> "" And Cells(iCounter, 8).Offset(0, -1) = "Send Reminder" Then
MailDest = MailDest & ";" & Cells(iCounter, 8).Value
End If
Next iCounter
Dim OutLookApp As Object: Set OutLookApp = CreateObject("Outlook.Application")
Dim OutLookMailItem As Object: Set OutLookMailItem = OutLookApp.CreateItem(0)
With OutLookMailItem
.BCC = MailDest
.Subject = "FYI"
.Body = "Reminder: The patient is overdue please contact the patient to reschedule. Please ignore if appointment has been made."
.Send
End With
End Sub
However, I don't think that these changes would correct the error.
I found in Microsoft's Knowledge Base an article regarding this issue: https://support.microsoft.com/en-us/help/189618/you-may-receive-the-run-time-error-2147023174-800706ba-error-message-o
To summarize it quickly: If you use unqualified references to refer to variables (e.g. Set a = ActiveDocument instead of Set a = WordApp.ActiveDocument) then VBA creates a hidden global variable to store which Application's ActiveDocument it refers to. Therefore if you try to quit that Application, you won't succeed and the Application will continue to run (without a visible window and without an error message) and the next time you want to refer to the ActiveDocument, it won't refer to the that Application's ActiveDocument which you started last time but the old Application's ActiveDocument, which, however was destroyed when the old Application started its process of exiting.
I examined your code and found no such unqualified reference.
My theory is that either (A) you judged some part of your code irrelevant to the problem and did not post it; and that contains such an unqualified reference; or (B) you are in the middle of developing this Sub and previous versions of the code created some dangling references.
In case (A) please post all other parts of your code that handles Outlook in any way.
In case (B) check in the Task Manager how many Outlook processes are running. Sign out from windows to make sure that all your Applications are closed, sign in, and try if the error still occurs.

Automated mailing on VBA fails on second iteration

I modified VBA code to send to X mail address X attachment. On the first iteration the code works perfectly, the mail is sent as is with the correct file. On the second iteration the code stops when attaching it's file (on objMail.Attachments.Add archivoFuente line) showing up this screen:
My code gets the main data from the first sheet which contains the mail address in the second column and the filename it's standardized from the name of the user and the file extension on the third column. With that done the filename it's attached to the directory, then I add body, subject and other things for the user to see on the mail, finally the mail is sent and I empty the strings variables to start the text iteration but then I encounter the error even with the file on the folder and with the same filename as standardized.
Sub bulkMail()
Dim outlookApp As Outlook.Application
Dim objMail As Outlook.MailItem
Dim archivoFuente, toMail, ccMail As String
Dim i, j As Integer
Dim fila As Long
fila = Cells(Rows.Count, 2).End(xlUp).Row
'On Error Resume Next
Set outlookApp = New Outlook.Application
Set objMail = outlookApp.CreateItem(olMailItem)
For i = 2 To fila
toMail = Cells(i, 2) ' & ";"
archivoFuente = "C:\Users\..." & Cells(i, 3)
objMail.Attachments.Add archivoFuente ' on the second iteration, stops here
ThisWorkbook.Save
'archivoFuente = ThisWorkbook.FullName
'objMail.Attachments.Add archivoFuente
objMail.To = toMail
objMail.Subject = "TEST"
objMail.Body = "LOREM," & vbNewLine & "IPSUM." & vbNewLine & "BYE."
objMail.Send
toMail = ""
archivoFuente = ""
Next i
MsgBox "DONE!"
End Sub
Appreciate any help!
Simple fix: move Set objMail = outlookApp.CreateItem(olMailItem) inside the loop.
You need a new mail item for each row. The code as is fails because you're trying to attach a file to an email you just sent (on the first iteration).
Other things you could fix:
Dim archivoFuente, toMail, ccMail As String - only ccMail is a String. You are looking for Dim archivoFuente As String, toMail As String, ccMail As String.
Dim i, j As Integer - should be Dim i As Long, j As Long. Excel has more rows than Integer can handle.

Paste cells into Outlook then convert table to text in Excel

I'm trying to build a macro that grabs a selection of cells from an Excel spreadsheet, pastes the cells into a new outlook email, then changes the format of the cells.
Specifically I want to convert the table to text, then change the font to Arial size 10.
The code below does the above, but I haven't been able to figure out how to convert the table to text, then change the text font.
Can anyone help?
Sub Email_test()
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Set rng = Nothing
Set rng = Sheets("Master").Range("A1:B99").SpecialCells(xlCellTypeVisible)
If rng Is Nothing Then
MsgBox "The selection is not a range or the sheet is protected. " & _
vbNewLine & "Please correct and try again.", vbOKOnly
Exit Sub
End If
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = "User#company.com"
.CC = ""
.BCC = ""
.Subject = "Cells as text "
.HTMLbody = RangetoHTML(rng)
' In place of the following statement, you can use ".Display" to
' display the e-mail message.
.Display
End With
End Sub
This will work for you, instead of HTMLbody use body also removed your range to html function
Sub Email_test()
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Set rng = Nothing
Set rng = Sheets("Master").Range("A1:B99").SpecialCells(xlCellTypeVisible)
If rng Is Nothing Then
MsgBox "The selection is not a range or the sheet is protected. " & _
vbNewLine & "Please correct and try again.", vbOKOnly
Exit Sub
End If
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Dim v As Variant: v = rng.Value
Dim tempStr As String: tempStr = ""
For i = LBound(v, 1) To UBound(v, 1)
For j = LBound(v, 2) To UBound(v, 2)
If j = 2 Then
tempStr = tempStr & v(i, j) & vbCrLf
Else
tempStr = tempStr & v(i, j) & " "
End If
Next j
Next i
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = "User#company.com"
.CC = ""
.BCC = ""
.Subject = "Cells as text "
.body = tempStr
' In place of the following statement, you can use ".Display" to
' display the e-mail message.
.Display
End With
End Sub
Please mark as an answer if you are satisfied with reply
The Outlook object model provides three main ways for working item bodies:
Body - a string representing the clear-text body of the Outlook item.
HTMLBody - a string representing the HTML body of the specified item.
Word editor - the Microsoft Word Document Object Model of the message being displayed. The WordEditor property of the Inspector class returns an instance of the Document class from the Word object model which you can use to set up the message body.
You can read more about all these ways in the Chapter 17: Working with Item Bodies. It us up to you which way is to choose.

Resources