• Set Focus Of Program ( Not MS Based) (VBA)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Set Focus Of Program ( Not MS Based) (VBA)

    Author
    Topic
    #396219

    The user has a choice to print or preview a template generated in InDesign. I am wondering how I can get the template in the Indesign Program to become the Active Window. I have tried using the ShellExecute Function, but to no avail. Any ideas would be great

    Thanks

    Viewing 3 reply threads
    Author
    Replies
    • #740835

      I know nothing about InDesign, so please forgive the following naive questions.

      Do you mean that several document windows are open within the (already open) InDesign application window, and you want a specific document window to be activated? Or do you want the (already open) InDesign application window to be activated? Or do you want to start InDesign and give it the focus?

    • #740836

      I know nothing about InDesign, so please forgive the following naive questions.

      Do you mean that several document windows are open within the (already open) InDesign application window, and you want a specific document window to be activated? Or do you want the (already open) InDesign application window to be activated? Or do you want to start InDesign and give it the focus?

    • #740843

      Is the file already open or do you need to open it first? What code do you currently have?

      • #740849

        The file is already opened and populated. I cannot get the document in the program to become the active document on screen, so the user can preview it.

        Like you know you go alt-tab to switch between programs so the program you select is the active program on screen. I want that functionality (of course the user does not click atl-tab, it happens automatically

        I hope that make sense

        • #740871

          There is no way to do this using standard VBA. It might be possible with Windows API calls, but you would need to have technical information about the class name of the InDesign application and document windows. You may be able to find such information on InDesign discussion forums. Unless there is a Lounger who happens to know this, I’m afraid that you won’t get an answer here.

          • #740873

            OK Cool

            Thanks for the help

            • #740954

              (Edited by MarkD on 07-Nov-03 06:57. Deleted attachment. See next post for revised file.)

              You can set the foreground focus to an open window, MS or otherwise, if you know either the Windows class name for the application, or the window text (ie, caption). Either of these can be used to return the window’s handle (hwnd) using FindWindow API function. You can then use SetForegroundWindow function to set the window as the system’s foreground window. You can use sub like this to get Windows class name and window caption for all open top-level windows:

              Public Sub GetClassNames()

              Dim hwnd As Long
              Dim strCaption As String
              Dim strClassName As String

              ‘ Loop through all top-level windows:
              hwnd = GetDesktopWindow()
              hwnd = GetWindow(hwnd, GW_CHILD)

              Do While hwnd 0
              strCaption = GetCaption(hwnd)
              If Len(strCaption) > 0 Then
              strClassName = apiGetClassName(hwnd)
              Debug.Print “Caption: ” & strCaption, “Class Name: ” & strClassName
              End If
              ‘ Move to the next top-level window:
              hwnd = GetWindow(hwnd, GW_HWNDNEXT)
              Loop

              End Sub

              Sample output:

              Caption: VB / VBA – Microsoft Internet Explorer Class Name: IEFrame
              Caption: Adobe Acrobat Class Name: Afx:400000:8:10011:0:340373
              Caption: Untitled – Notepad Class Name: Notepad
              Caption: Lavasoft Ad-watch v Class Name: TAWMon
              Caption: Ad-watch Class Name: TApplication
              Caption: C:Documents and SettingsMARK DMy DocumentsWork Stuff Class Name: ExploreWClass

              And lots more…. Example of sub that sets foreground focus to another application:

              Sub SetAppFocus(ByRef strClassName As String, ByRef strCaption As String)

              Dim hwnd As Long

              ‘ FindWindow API can use Window’s ClassName or window caption text
              ‘ Normally only need one, for other pass zero using vbNullString constant
              hwnd = FindWindow(strClassName, strCaption)

              If hwnd > 0 Then
              SetForegroundWindow (hwnd)
              ShowWindow hwnd, SW_MAXIMIZE
              End If

              End Sub

              As noted above you can use either class name or caption to get a window’s hwnd. For example, testing sub with Adobe Acrobat, either of these worked:

              SetAppFocus vbNullString, “Adobe Acrobat”
              SetAppFocus “Afx:400000:8:10011:0:340373”, vbNullString

              BUT as noted by HansV, if InDesign (whatever that is) uses “child” windows you may not be able to set focus to child window unless it is maximized in the “parent” MDI application window. For example, with Adobe, I opened two .PDF documents tiled within parent window. This did NOT work:

              SetAppFocus “Afx:400000:8:10011:0:340373”, “SomePDF File.pdf”

              where “SomePDF File.pdf” is caption of child window. But if this document is maximized in parent window, this works:

              SetAppFocus vbNullString, “Adobe Acrobat – [SomePDF File.pdf]”

              Edit comment: See next post for revised attachment with sample API code.

              HTH

            • #741057

              In further reply, if the template or whatever you want to display is a child window in an MDI application, you’d have to use something like this to set focus to specified child window:

              Public Sub SetAppFocusRev(ByRef strClassName As String, _
              ByRef strAppCaption As String, _
              ByRef strChildCaption As String)

              Dim hwnd As Long
              Dim hwndMDI As Long
              Dim hwndChild As Long

              ‘ FindWindow API can use Window’s ClassName or window caption text
              hwnd = FindWindow(strClassName, strAppCaption)

              If hwnd > 0 Then
              ‘ Set foreground focus to app window:
              SetForegroundWindow hwnd
              ‘ Maximize app or child window:
              If Len(strChildCaption) = 0 Then
              ShowWindow hwnd, SW_MAXIMIZE
              Else
              ‘ Get hwnd for MDI Client window:
              hwndMDI = FindWindowEx(hwnd, &O0, “MDIClient”, vbNullString)
              ‘ Get hwnd for MDI Child window:
              hwndChild = FindWindowEx(hwndMDI, &O0, vbNullString, strChildCaption)
              ‘ Maximize and set focus to MDI client & child window:
              ShowWindow hwndMDI, SW_MAXIMIZE
              ShowWindow hwndChild, SW_MAXIMIZE
              End If
              End If

              End Sub

              Example of use with Adobe Acrobat:

              SetAppFocusRev vbNullString,”Adobe Acrobat”, “SomePDF File.pdf”

              (Adobe was open, with two files open in cascaded windows.) This statement set focus to Acrobat, and maximized the MDI client window with SomePDF File.pdf displayed in maximized client window.

              Note you have to use FindWindowEx API function, not FindWindow, to get the hwnd’s for MDI Client window, then the specified child window (the MDI Client will be first “child” of application main window). Previous post used GetWindow API to list open windows, however, the preferred method (so I’m informed) is to use the EnumWindows and EnumChildWindows API’s to enumerate open windows and child windows, using a callback function. See attached (revised) text file for necessary API declarations and test subs. Some test results with Acrobat:

              ‘TestEnumChildWindows (FindWindow(vbNullString, “Adobe Acrobat”))
              ‘ Child Class = MDIClient, Title = , Hwnd = 1442726
              ‘ Child Class = Afx:400000:8:0:0:2b035f, Title = SomePDF File.pdf, Hwnd = 66768
              ‘ Child Class = Afx:400000:8:0:0:2b035f, Title = File Format.pdf, Hwnd = 459766

              ‘ Closed Adobe, reopened:

              ‘TestEnumChildWindows (FindWindow(vbNullString, “Adobe Acrobat”))
              ‘ Child Class = MDIClient, Title = , Hwnd = 721446
              ‘ Child Class = Afx:400000:8:0:0:8d901dd, Title = File Format.pdf, Hwnd = 852772
              ‘ Child Class = Afx:400000:8:0:0:8d901dd, Title = SomePDF File.pdf, Hwnd = 590556

              Note that the class name for the child document windows apparently are dynamic, so these can’t be used to get the hwnd, you have to use the window caption. If you are going to open a template file, you should know what the caption is, assuming InDesign has some standard caption format (you can modify code so that caption does not have to be an exact match, using Like, etc). You can use the EnumWindows and EnumChildWindows API’s to determine class name, etc, used by InDesign application, then test to see if this will work with the app in question.

              Attached text file can be renamed with “.bas” extension and imported into VBA project.

              HTH

            • #741058

              In further reply, if the template or whatever you want to display is a child window in an MDI application, you’d have to use something like this to set focus to specified child window:

              Public Sub SetAppFocusRev(ByRef strClassName As String, _
              ByRef strAppCaption As String, _
              ByRef strChildCaption As String)

              Dim hwnd As Long
              Dim hwndMDI As Long
              Dim hwndChild As Long

              ‘ FindWindow API can use Window’s ClassName or window caption text
              hwnd = FindWindow(strClassName, strAppCaption)

              If hwnd > 0 Then
              ‘ Set foreground focus to app window:
              SetForegroundWindow hwnd
              ‘ Maximize app or child window:
              If Len(strChildCaption) = 0 Then
              ShowWindow hwnd, SW_MAXIMIZE
              Else
              ‘ Get hwnd for MDI Client window:
              hwndMDI = FindWindowEx(hwnd, &O0, “MDIClient”, vbNullString)
              ‘ Get hwnd for MDI Child window:
              hwndChild = FindWindowEx(hwndMDI, &O0, vbNullString, strChildCaption)
              ‘ Maximize and set focus to MDI client & child window:
              ShowWindow hwndMDI, SW_MAXIMIZE
              ShowWindow hwndChild, SW_MAXIMIZE
              End If
              End If

              End Sub

              Example of use with Adobe Acrobat:

              SetAppFocusRev vbNullString,”Adobe Acrobat”, “SomePDF File.pdf”

              (Adobe was open, with two files open in cascaded windows.) This statement set focus to Acrobat, and maximized the MDI client window with SomePDF File.pdf displayed in maximized client window.

              Note you have to use FindWindowEx API function, not FindWindow, to get the hwnd’s for MDI Client window, then the specified child window (the MDI Client will be first “child” of application main window). Previous post used GetWindow API to list open windows, however, the preferred method (so I’m informed) is to use the EnumWindows and EnumChildWindows API’s to enumerate open windows and child windows, using a callback function. See attached (revised) text file for necessary API declarations and test subs. Some test results with Acrobat:

              ‘TestEnumChildWindows (FindWindow(vbNullString, “Adobe Acrobat”))
              ‘ Child Class = MDIClient, Title = , Hwnd = 1442726
              ‘ Child Class = Afx:400000:8:0:0:2b035f, Title = SomePDF File.pdf, Hwnd = 66768
              ‘ Child Class = Afx:400000:8:0:0:2b035f, Title = File Format.pdf, Hwnd = 459766

              ‘ Closed Adobe, reopened:

              ‘TestEnumChildWindows (FindWindow(vbNullString, “Adobe Acrobat”))
              ‘ Child Class = MDIClient, Title = , Hwnd = 721446
              ‘ Child Class = Afx:400000:8:0:0:8d901dd, Title = File Format.pdf, Hwnd = 852772
              ‘ Child Class = Afx:400000:8:0:0:8d901dd, Title = SomePDF File.pdf, Hwnd = 590556

              Note that the class name for the child document windows apparently are dynamic, so these can’t be used to get the hwnd, you have to use the window caption. If you are going to open a template file, you should know what the caption is, assuming InDesign has some standard caption format (you can modify code so that caption does not have to be an exact match, using Like, etc). You can use the EnumWindows and EnumChildWindows API’s to determine class name, etc, used by InDesign application, then test to see if this will work with the app in question.

              Attached text file can be renamed with “.bas” extension and imported into VBA project.

              HTH

            • #740955

              (Edited by MarkD on 07-Nov-03 06:57. Deleted attachment. See next post for revised file.)

              You can set the foreground focus to an open window, MS or otherwise, if you know either the Windows class name for the application, or the window text (ie, caption). Either of these can be used to return the window’s handle (hwnd) using FindWindow API function. You can then use SetForegroundWindow function to set the window as the system’s foreground window. You can use sub like this to get Windows class name and window caption for all open top-level windows:

              Public Sub GetClassNames()

              Dim hwnd As Long
              Dim strCaption As String
              Dim strClassName As String

              ‘ Loop through all top-level windows:
              hwnd = GetDesktopWindow()
              hwnd = GetWindow(hwnd, GW_CHILD)

              Do While hwnd 0
              strCaption = GetCaption(hwnd)
              If Len(strCaption) > 0 Then
              strClassName = apiGetClassName(hwnd)
              Debug.Print “Caption: ” & strCaption, “Class Name: ” & strClassName
              End If
              ‘ Move to the next top-level window:
              hwnd = GetWindow(hwnd, GW_HWNDNEXT)
              Loop

              End Sub

              Sample output:

              Caption: VB / VBA – Microsoft Internet Explorer Class Name: IEFrame
              Caption: Adobe Acrobat Class Name: Afx:400000:8:10011:0:340373
              Caption: Untitled – Notepad Class Name: Notepad
              Caption: Lavasoft Ad-watch v Class Name: TAWMon
              Caption: Ad-watch Class Name: TApplication
              Caption: C:Documents and SettingsMARK DMy DocumentsWork Stuff Class Name: ExploreWClass

              And lots more…. Example of sub that sets foreground focus to another application:

              Sub SetAppFocus(ByRef strClassName As String, ByRef strCaption As String)

              Dim hwnd As Long

              ‘ FindWindow API can use Window’s ClassName or window caption text
              ‘ Normally only need one, for other pass zero using vbNullString constant
              hwnd = FindWindow(strClassName, strCaption)

              If hwnd > 0 Then
              SetForegroundWindow (hwnd)
              ShowWindow hwnd, SW_MAXIMIZE
              End If

              End Sub

              As noted above you can use either class name or caption to get a window’s hwnd. For example, testing sub with Adobe Acrobat, either of these worked:

              SetAppFocus vbNullString, “Adobe Acrobat”
              SetAppFocus “Afx:400000:8:10011:0:340373”, vbNullString

              BUT as noted by HansV, if InDesign (whatever that is) uses “child” windows you may not be able to set focus to child window unless it is maximized in the “parent” MDI application window. For example, with Adobe, I opened two .PDF documents tiled within parent window. This did NOT work:

              SetAppFocus “Afx:400000:8:10011:0:340373”, “SomePDF File.pdf”

              where “SomePDF File.pdf” is caption of child window. But if this document is maximized in parent window, this works:

              SetAppFocus vbNullString, “Adobe Acrobat – [SomePDF File.pdf]”

              Edit comment: See next post for revised attachment with sample API code.

              HTH

          • #740874

            OK Cool

            Thanks for the help

          • #741830

            [indent]


            There is no way to do this using standard VBA. It might be possible with Windows API calls


            [/indent]To activate the application itself, no API calls are needed, as long as you knwo the windows caption. For example:

            AppActivate “Office Forum powered by Compuserve”
            activates my internet explorer window when I am in the Compuserve forum.
            If no windows with the caption you specify are found, a runtime error is generated you could trap.

            If you need a specific child window of the application to be activated, of course Hans is right, you either need to have its handle and use API calls to activate it, or you need to use VBA to access the application’s object model.

            If the application really has full VBA, setting a reference to it from Word’s VBA should give you full access to the intellisense options and the associated Help files.

            • #741842

              I interpreted the question to be how to activate a child window within the InDesign application window, hence my remark. You’re correct, of course, that activating an application window is relatively easy.

            • #741843

              I interpreted the question to be how to activate a child window within the InDesign application window, hence my remark. You’re correct, of course, that activating an application window is relatively easy.

          • #741831

            [indent]


            There is no way to do this using standard VBA. It might be possible with Windows API calls


            [/indent]To activate the application itself, no API calls are needed, as long as you knwo the windows caption. For example:

            AppActivate “Office Forum powered by Compuserve”
            activates my internet explorer window when I am in the Compuserve forum.
            If no windows with the caption you specify are found, a runtime error is generated you could trap.

            If you need a specific child window of the application to be activated, of course Hans is right, you either need to have its handle and use API calls to activate it, or you need to use VBA to access the application’s object model.

            If the application really has full VBA, setting a reference to it from Word’s VBA should give you full access to the intellisense options and the associated Help files.

        • #740872

          There is no way to do this using standard VBA. It might be possible with Windows API calls, but you would need to have technical information about the class name of the InDesign application and document windows. You may be able to find such information on InDesign discussion forums. Unless there is a Lounger who happens to know this, I’m afraid that you won’t get an answer here.

        • #740879

          Shimmer

          I found the InDesign Scripting Forum on the Adobe website and also discovered an InDesign VBA reference in my list of references. This tells me two things:
          1. You can control InDesign from VBA – but I don’t know how
          2. It looks like the adobe forum has people with the knowledge but you are unlikely to find anyone here that can help you.

          Best of luck with your journey.

        • #740880

          Shimmer

          I found the InDesign Scripting Forum on the Adobe website and also discovered an InDesign VBA reference in my list of references. This tells me two things:
          1. You can control InDesign from VBA – but I don’t know how
          2. It looks like the adobe forum has people with the knowledge but you are unlikely to find anyone here that can help you.

          Best of luck with your journey.

      • #740850

        The file is already opened and populated. I cannot get the document in the program to become the active document on screen, so the user can preview it.

        Like you know you go alt-tab to switch between programs so the program you select is the active program on screen. I want that functionality (of course the user does not click atl-tab, it happens automatically

        I hope that make sense

    • #740844

      Is the file already open or do you need to open it first? What code do you currently have?

    Viewing 3 reply threads
    Reply To: Set Focus Of Program ( Not MS Based) (VBA)

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

    Your information: