• Class names for Excel’s windows

    Author
    Topic
    #357558

    Hi,

    Does someone know where I can find more information on the class names for Excel’s windows? e.g. I want to use API calls, like

    handle = FindWindow(“ThunderDFrame”, UserForm1.Caption)

    here, “ThunderDFrame” is the class name for Excel’s UserForms. But what about sheets and charts??? Especially, if an embedded chart is in a chart window, is there a class name for this?

    Viewing 0 reply threads
    Author
    Replies
    • #531395

      Hans,

      The following are some XL window class names I have come across.

      XLMAIN, the main window with caption Microsoft Excel – Book1 or whatever
      XLDESK, the background window when no workbook is open, or all are minimised.
      EXCEL7, A worksheet sheet window, and as far as i know also a Chart window
      I think there is an EXCEL8, but not sure what it is
      EXCELE, is I think the one you refer to as an embedded chart in it’s own (temporary) window.
      EXCEL<, is I think the formula bar. and as you pointed out
      ThunderDFrame is the UserForm Window

      Hope there is enough there to keep you going for a while, though I suspect I am missing some.

      Andrew

      • #531424

        Andrew,

        I printed that one for future reference. Thanks!

      • #531464

        Thanks Andrew,

        your list is really helpful for me. Maybe I can add what I have:
        Excel Status Bar : EXCEL4 (that ‘s different from your class name, so I have to try that out)
        Excel Userform for Excel 97: ThunderXFrame
        Excel dialog sheet: bosa_sdm_xl9 (in Excel 2000), bosa_sdm_xl8 (in Excel 97) and bosa_sdm_xl (in Excel 5 and 95)

        I wrote a macro using API calls to have a UserForm always on top and now I want to do the same with a chart (don’t know if this will work).

        Many Thanks again.

        • #531470

          Hi Hans,

          I have come across some interesting window class names for both Excel and Word. I put together some API calls and a bit of code with a useform which is supposed to show the window under the cursor.

          It can bes used in Excel or Word (or I suppose any flavour of VBA or VB) and I attach a zip file for anybody interested. It contains a bas module and and a userform which can be imported into VBA (xl or word). Actually if you interested in Excel windows, it is better to run it from a running version of word, as you can work all Excel dialogs and get a name for them from the Word userform, and vice versa if you are interested in Word windows.

          It seems to work, though I have tried it in all circumstances – it a rough and ready method.

          BTW the userform is modeless.

          Andrew

          • #531472

            Wow, Andrew, this is wonderful.

            Although, there is still one thing… My code below seems to work for a userform, but not for an embedded chart. Any ideas??

            Option Explicit
            Declare Function FindWindow Lib “user32” Alias “FindWindowA” _
            (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

            Declare Function SetWindowPos Lib “user32” (ByVal hwnd As Long, _
            ByVal hwndinsertafter As Long, ByVal x As Long, _
            ByVal y As Long, ByVal cx As Long, ByVal cy As Long, _
            ByVal wflags As Long) As Long
            Sub set_on_top(hdl As Variant, bTop As Boolean)
            Const SWP_NOSIZE = &H1
            Const SWP_NOMOVE = &H2
            Const SWP_NOACTIVATE = &H10
            Const SWP_SHOWWINDOW = &H40
            Const HWND_TOPMOST = -1
            Const HWND_NOTOPMOST = -2
            Dim WinFlags As Variant
            Dim PosFlag As Variant
            Dim return_code As Long
            WinFlags = SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW Or SWP_NOACTIVATE
            Select Case bTop
            Case True
            PosFlag = HWND_TOPMOST
            Case False
            PosFlag = HWND_NOTOPMOST
            End Select
            return_code = SetWindowPos(hdl, PosFlag, 0, 0, 0, 0, WinFlags)
            End Sub

            Sub test()
            Dim handle As Long
            handle = FindWindow(“EXCELE”, “[alwaysontop.xls]Sheet1 Chart1”)
            Call set_on_top(handle, True)
            End Sub

            If I replace EXCELE by ThunderDFrame and the second argument by UserForm1.Caption, the userform stays always on top. However, it does not work for an embedded chart. Maybe, I misspelled the window caption, but I don’t think so.
            Of course, if I add a button to the UserForm to unload it, then the userform is gone, but i don’t see a way to make disappear the chart if I don’t want to see it anymore. Maybe, that’s the reason that it doesn’t work.

            • #531475

              Hi Hans,

              I was thinking about this EXCELE window , and think that it just an ordinary which is borderless Window (if you check the class name it is EXCELE with or without the window frame). If a user selects to view it as window, then the border is drawn but as soon as the window looses focus, that event resets the window properties, and that might be why it does not respond to your code as you might expect. All this is pure speculation, and I don’t really know.

              Anyway, I will try your code, if for no other reason than it looks useful.

              Thanks

              Andrew

            • #531477

              Yes, I agree!
              Thanks for the code and the comments.

            • #531484

              Andrew,

              Actually, the code definitely works if you add a userform to the project and put in the following:

              Private Sub UserForm_Activate()
                Dim handle As Long
                handle = FindWindow("ThunderDFrame", UserForm1.Caption)
                Call set_on_top(handle, True)
              End Sub
              

              Then the userform stays on top, no matter what other application gets the focus. For sheets and charts or embedded charts, I don’t think it works.

            • #531495

              Hans,

              Thanks for that. I have incorporated your code into the Window Class Name viewer , and also adapted it show the wndow caption and handle (hWnd), as they might be useful in debugging. I am attaching the upgraded (?) project and maybe you can see if I implemented your code correctly. (I tried the Form Activate befor I saw your code, but don’t think it worked, but I will try your actual code).

              Andrew

            • #531502

              Andrew,

              If you just put the few lines of code in the userform activate event and change the Userform1.caption argument with the frmShowClass.Caption like below

              Private Sub UserForm_Activate()
              Dim handle As Long
              handle = FindWindow(“ThunderDFrame”, frmShowClass.Caption)
              Call set_on_top(handle, True)
              End Sub

              I added my Excel workbook with your code but so that the frmShowClass userform stays on top. This makes it a wonderful tool to move over other applications and find out the class names.

            • #531504

              Hans,

              Thanks for that. I think the last version I posted operates the same, stays on top i.e, except I put the call to your set_on_top code in the the code itself, rather than the UserForm. I don’t suppose it matters really.

              I’m trying to figure out it will behave if theer are 2 instances running at the same time, say one from Word and one from Excel. I suppose really if there are more than 1, then each should have a different caption. This is purely academic, as one instance is enough.

              Can you think of any other useful info it might report on ?

              Andrew

            • #531507

              Hans,

              I just noticed that you left in the line Call set_on_top(FindWindow(“ThunderDFrame”, “Window Class Name”), True) in the main code, which with your userform event is redundant.

              Andrew

            • #531513

              Hi all,

              Thanks for bringing me over to this Hans, still interested just ran out of time! Maybe I am doing this wrong, but the code below places the userform on top regardless of what the class name is.

              Private Sub UserForm_Activate()
              Dim handle As Long
              handle = FindWindow("XLMAIN", UserForm1.Caption)
              Call set_on_top(handle, True)
              End Sub
              

              I have used EXCELE, ThunderDFrame, etc with the same results

            • #531515

              Hans,

              As I suspected, the UserForm Activate does not kick in response to the updating of label captions, so it seems that unless a call to the set_on_top sub is made by the main code, it will not have any effect. Sorry about that, but it seems that changing content of a user form does not actually activate the form. However placing a call to your sub in the main body works fine.

              I have made a slight alteration that adds the application name to the userform caption, just in case you have a version running from Excel, and want to check the XL dialog box class names. You will need to run the code from an application other than xl, as when xl displays most of it’s dialogs, macro execution freezes. This happens for Print Preview (EXCELB, by the way) etc.

              Latest SR attached

            • #531546

              Andrew,

              Leaving the Call to set_on_top in the main code was not on purpose. I just copied your code and added the call to the form_activate. When I leave out these two calls in your code and leave the call to set_on_top in the form_activate event, it works fine. You are right that if the caption changes, you should call the set_on_top routine again with the new caption as argument; in that case your choice is more appropriate. It all depends on what you want to do with this. I used this set_on_top routine in many VB programmes where it was located in the form_load event. I think it is more suited for stand-alone VB programmes, than for putting it in an Excel VBA macro. Anyhow, for finding out the class names, it is a nice tool.

          • #595372

            Absolutely great thread! I’ve come back to it several times.

            For the sake of completeness or discussion or whatever, loading your ShowClass userfor in Word 97 produces this in the error log:

            Line 9: The property name ShowModal in frmShowClass is invalid.

            • #595378

              Kevin,

              As Office 97 does not support modeless userforms, this error will occur. To be of any real use it does require to be modeless.

              I have compiled a VB6 version and attach a zipped file.

              Andrew

            • #595567

              Hey man, Thanks!

    Viewing 0 reply threads
    Reply To: Class names for Excel’s windows

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: