none
Can't maximize Excel using VBA RRS feed

  • Question

  • I have been trying to maximize an Excel window using VBA from iFix and I am unable to get the window to maximize.

    I am using Excel 2010 and iFix 5.5.

    Code is as follows:

    Dim MyXL as Object

    Const WM_USER = 1024

    Dim hWnd as Long

    On Error Resume Next

    hWnd = FindWindow(,"XLMAIN",0)

    If hWnd = 0 Then

    MsgBox "Excel Not Running", vbokonly

    Exit Sub

    Else

    Set MyXL= GetObject(, "Excel Application")

    MyXL.WindowsState = xlMaximized

    MyXL.Visible = True

    End if

    End Sub

    The code returns a Windows handle from FindWindow()

    The codes returns an instance of Excel from GetObject()

    however, no combination of ActiveWindow, ActiveApplication, etc. will maximize the Excel application.

    Does anyone know how to maximize this window and get it to display? Has the API changed rendering this code unusable?

    Do I need to add additional references to the VBA project?

    I am exhausted from working on this! 


    WDM

    Monday, October 23, 2017 6:06 PM

Answers

  • Hello,

    You could use ShowWindow hwnd, SW_MAXIMIZE as Sam suggested to maximize Excel windows.

    To automate Excel, make sure you have added Microsoft Excel X.0 Object Library or Microsoft.Office.Interop.Excel as references.

    Then you could use

    Set MyXL = GetObject(, "Excel.Application")
    MyXL.ActiveWindow.WindowState = xlMaximized

    It would return nothing if you use GetObject(, "Excel Application")

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by qjxv9 Tuesday, October 24, 2017 5:15 PM
    Tuesday, October 24, 2017 2:29 AM
    Moderator
  • I now believe that the problem is not with the script but with the reference to the API.

    Thanks for your help.


    WDM

    • Marked as answer by qjxv9 Tuesday, October 24, 2017 5:15 PM
    Tuesday, October 24, 2017 5:15 PM

All replies

  • The following works for me. Your question is very vague about your actual requirements; there are other ways of doing things and there might be a better way but I don't know your requirements.

    Const SW_MAXIMIZE As Long = 3
    #If Win64 Then
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
        (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
        Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As LongPtr, _
            ByVal nCmdShow As Long) As Boolean
    #Else
        Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
            (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
        Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, _
            ByVal nCmdShow As Long) As Boolean
    #End If
         
    Sub Test()
    Dim hwnd As Long
    On Error Resume Next
    hwnd = FindWindow(vbNullString, "Book1 - Excel")
    If hwnd = 0 Then
        MsgBox "Excel Not Running", vbOKOnly
        Exit Sub
    End If
    'MsgBox "Excel found", vbOKOnly
    ShowWindow hwnd, SW_MAXIMIZE
    End Sub
    



    Sam Hobbs
    SimpleSamples.Info


    Monday, October 23, 2017 7:52 PM
  • Hello,

    You could use ShowWindow hwnd, SW_MAXIMIZE as Sam suggested to maximize Excel windows.

    To automate Excel, make sure you have added Microsoft Excel X.0 Object Library or Microsoft.Office.Interop.Excel as references.

    Then you could use

    Set MyXL = GetObject(, "Excel.Application")
    MyXL.ActiveWindow.WindowState = xlMaximized

    It would return nothing if you use GetObject(, "Excel Application")

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by qjxv9 Tuesday, October 24, 2017 5:15 PM
    Tuesday, October 24, 2017 2:29 AM
    Moderator
  • I now believe that the problem is not with the script but with the reference to the API.

    Thanks for your help.


    WDM

    • Marked as answer by qjxv9 Tuesday, October 24, 2017 5:15 PM
    Tuesday, October 24, 2017 5:15 PM