Results 1 to 5 of 5

VBA code to minimize a window

This is a discussion on VBA code to minimize a window within the Excel Questions forums, part of the Question Forums category; How do I minimize an Outlook window while sending an email from Excel using VBA . The macro is in ...

  1. #1
    Board Regular
    Join Date
    May 2005
    Posts
    195

    Default VBA code to minimize a window

    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?

  2. #2
    MrExcel MVP Joe Was's Avatar
    Join Date
    Feb 2002
    Location
    Central Florida, USA
    Posts
    7,539

    Default Re: VBA code to minimize a window

    ActiveWindow.WindowState = xlMinimized
    JSW: Try and try again: "The way of the Coder!"

  3. #3
    Board Regular
    Join Date
    May 2005
    Posts
    195

    Default Re: VBA code to minimize a window

    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

  4. #4
    Board Regular
    Join Date
    Apr 2006
    Location
    Northeastern PA, USA
    Posts
    175

    Default VBA code to minimize a window

    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

  5. #5
    ZVI
    ZVI is offline
    MrExcel MVP ZVI's Avatar
    Join Date
    Apr 2008
    Location
    Sevastopol
    Posts
    2,712

    Default Re: VBA code to minimize a window

    Hi,

    There is a shorter way:
    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 by ZVI; Sep 13th, 2008 at 09:07 PM.

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


DMCA.com