VBA code to minimize a window

simora

Board Regular
Joined
May 7, 2005
Messages
199
How do I minimize an Outlook window while sending an email from Excel using VBA.
The macro is in Excel. Can you use Sendkeys on multiple times in your code. I tried using SendKeys ("% { } N") To minimize the active window Tring to simulate ( alt + spacebar and N)

Didn't work.
I'm using another instance of Sendkeys to do something else later.

Any ideas?
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hi:

I tried that but what it does is to minimize the Excel window rather than the Outlook window, even though I know that I'm currently in the outlook window. I know this because I used sendkeys to influence the window.

When I finally tried SendKeys ("%{ }N") this actually minimizes the window in outlook, however, now, it wont let me use other sendkeys to send the email.

I'm trying to minimize the Outlok window before I send it because there's about a 20 second delay while Excel & Outlook decides the final output to send.

Thanks
 
Upvote 0
Try this:

Put the code below in a VBA code module.

1- Check subroutine 'window_action' - make sure the Outlook window caption
is specified here. It doesn't have to be the entire caption, but enough to
uniquely identify the Outlook window.
2- From your event that you wish to minimize the external (Outlook) application,
call subroutine 'window_action'.

This should minimize the Outlook window... Let me know how you make out.

Option Explicit
Public psAppNameContains As String
Public pbfound As Boolean
Public thehwnd As Long
Public Declare Function GetWindowText& Lib "user32" Alias "GetWindowTextA" (ByVal hwnd _
As Long, ByVal lpString As String, ByVal cch As Long)
Public Declare Function ShowWindow& Lib "user32" (ByVal hwnd As Long, ByVal ncmdshow As Long)
Public Declare Function EnumWindows Lib "user32" _
(ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Const SW_MINIMIZE = 6

Public Sub window_action()
AppActivateByStringPart ("Outlook")
If thehwnd > 0 Then
Dim showw As Long
showw = ShowWindow(thehwnd, SW_MINIMIZE)
Else
MsgBox ("Outlook window not found")
End If
End Sub

Public Function AppActivateByStringPart(StringPart As String) As Boolean
Dim lRet As Long
psAppNameContains = StringPart
lRet = EnumWindows(AddressOf CheckForInstance, 0)
AppActivateByStringPart = pbfound
pbfound = False
End Function

Public Function CheckForInstance(ByVal lhwnd As Long, ByVal _
lParam As Long) As Long
Dim sTitle As String
Dim lRet As Long
Dim iNew As Integer
If Trim(psAppNameContains = "") Then
CheckForInstance = False
Exit Function
End If
sTitle = Space(255)
lRet = GetWindowText(lhwnd, sTitle, 255)
sTitle = StripNull(sTitle)
If InStr(sTitle, psAppNameContains) > 0 Then
CheckForInstance = False
pbfound = True
thehwnd = lhwnd
Else
CheckForInstance = True
End If
End Function

Public Function StripNull(ByVal InString As String) As String
Dim iNull As Integer
If Len(InString) > 0 Then
iNull = InStr(InString, vbNullChar)
Select Case iNull
Case 0
StripNull = InString
Case 1
StripNull = ""
Case Else
StripNull = Left$(InString, iNull - 1)
End Select
End If
End Function
 
Upvote 0
Hi,

There is a shorter way:
Rich (BB code):
' Minimize an Outlook window
Sub MinimizeOutlookWindow()
  On Error Resume Next
  With GetObject(, "Outlook.Application")
    .ActiveWindow.WindowState = 1   ' olMinimized = 1
  End With 
End Sub

Regards,
Vladimir
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,617
Messages
6,120,541
Members
448,970
Latest member
kennimack

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