New Email window show up in front of any other window

Josh House
Josh House used Ask the Experts™
on
I have VBA that will create an email when you click the generate email button. However, I have tried to make it pop up but it is inconsistent. How do I make this window pop up in front of any window as if making it the most active window.

below is the VBA I have. Can this be edited so that the new email window comes to the forefront every time. Please help.

Sub Mail_Selection_Range_Outlook_Body()
'Don't forget to copy the function RangetoHTML in the module.
    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object

    Set rng = Nothing
    On Error Resume Next
    'Only the visible cells in the selection
    'Set rng = Selection.SpecialCells(xlCellTypeVisible)
    'You can also use a fixed range if you want
    Set rng = Sheets("Volume Template").Range("K4:L14").SpecialCells(xlCellTypeVisible)
    On Error GoTo 0

    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)

    On Error Resume Next
    With OutMail
        .To = ""
        .CC = ""
        .BCC = Range("K16").Value 'Add bccEmail once the code is fixed below
        .Subject = "UTS Volume Quote Request Order ID: " & Range("L4")
        .HTMLBody = RangetoHTML(rng)
        .Display  'or use .Send
         Application.Visible = True
         OutlApp.Visible = True
         OutMail.Visible = True
 On Error GoTo 0
    End With
    On Error GoTo 0
    
End Sub


Function RangetoHTML(rng As Range)
    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

Open in new window

Carrier-Volume-Rate-Estimate--New-.xlsm
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Managing Director/Excel VBA Developer
Distinguished Expert 2018
Commented:
Hi Josh,

When using 3rd party programs and other Windows commands that open an external default program the program window opens minimized and blinking. For example when having Outlook as your default mail app and using the Windows’ “Send to Mail Recipient,” the new email window stays on the task bar and blinks until you click it or wait for the allotted time that Windows is set to.

Solution:
A registry entry that is controlling the amount of time until the program can take focus is set too high. We need to adjust that.
1. Open “Regedit.exe” and navigate to the “HKEY_CURRENT_USER > Control Panel > Desktop” key
2. Find the REG_DWORD entry that is labeled “ForegroundLockTimeout” (default decimal value: 200000).
3. Change it’s decimal value to “0” in order to make the program’s window immediately pop up instead of flashing on the task bar.
4. Log out of Windows and then log back in.
Source is from PURYEAR IT

Other option is to include Fore Ground Window on top in VBA:
Sub Mail_Selection_Range_Outlook_Body()
'Don't forget to copy the function RangetoHTML in the module.
    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object
    Dim Wnd As Object

    Set rng = Nothing
    On Error Resume Next
    'Only the visible cells in the selection
    'Set rng = Selection.SpecialCells(xlCellTypeVisible)
    'You can also use a fixed range if you want
    Set rng = Sheets("Volume Template").Range("K4:L14").SpecialCells(xlCellTypeVisible)
    On Error GoTo 0

    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)
    Set Wnd = CreateObject("Outlook.Application")
    
    If Wnd.GetForegroundWindow() = True Then
        If Wnd.AlwaysOnTop = True Then
            Wnd.AlwaysOnTop = False
        Else
            Wnd.AlwaysOnTop = True
        End If
        Wnd.ApplyChanges 0
    End If

    On Error Resume Next
    With OutMail
        .To = ""
        .CC = ""
        .BCC = Range("K16").Value 'Add bccEmail once the code is fixed below
        .Subject = "UTS Volume Quote Request Order ID: " & Range("L4")
        .HTMLBody = RangetoHTML(rng)
        .Display  'or use .Send
         Application.Visible = True
         OutlApp.Visible = True
         OutMail.Visible = True
 On Error GoTo 0
    End With
    On Error GoTo 0
    
End Sub


Function RangetoHTML(rng As Range)
    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

Open in new window

Josh HouseDigital Sales Coordinator

Author

Commented:
thanks this has been a major help
ShumsManaging Director/Excel VBA Developer
Distinguished Expert 2018

Commented:
You're Welcome Josh! Glad I was able to help

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial