Application will minimize, but will not maximize when called/ RRS feed

  • Question

  • Hello:

    I was able to work on a Sub to minimize my application. However, when I try to maximize, it won't do it.  Here is my code to minimize:

    Module sheetView
        Public WB As Excel.Workbook = CType(Globals.ThisWorkbook.Application.ActiveWorkbook, Excel.Workbook)
       Sub minimizeWindow()
            With WB
                .Application.WindowState = XlWindowState.xlMinimized
            End With
        End Sub

    This works perfectly, but when I want to maximize, it wont. For example, on my maximize code, is suppose to maximize excel and select a particular sheet. The sheet is selected but the application is not maximized.

        Sub maximizeWindow()
            With WB
                .Application.WindowState = XlWindowState.xlMaximized
            End With
        End Sub
    End Module
    Thursday, August 1, 2013 4:27 PM


  • After some work, I was able to get it to work.

    Public Class Form1
        'Declare functions and constants
        Private Declare Function ShowWindow Lib "user32" (ByVal handle As IntPtr, ByVal nCmdShow As Integer) As Integer
        Private Const SW_SHOWMAXIMIZED As Integer = 3
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'Specify part of the window title you want. Get its window handle:
            Dim hwnd As String = findPartialTitle("Notepad")
            'Send hwnd to showwindow function
            ShowWindow(hwnd, SW_SHOWMAXIMIZED)
        End Sub
        'FUNCTION: Find handle by partial window title
        Private Function findPartialTitle(ByVal partialTitle As String) As IntPtr
            For Each p As Process In Process.GetProcesses()
                If p.MainWindowTitle.IndexOf(partialTitle, 0, StringComparison.CurrentCultureIgnoreCase) > -1 Then
                    Return p.MainWindowHandle
                End If
            Return IntPtr.Zero
        End Function
    End Class

    • Marked as answer by JMazorra Friday, August 2, 2013 2:16 AM
    Friday, August 2, 2013 2:15 AM