none
How to keep the focus on a .pdf document that is opened by clicking on a hyperlink in a text field? RRS feed

  • Question

  • Do you know how to keep the focus on a .pdf document that is opened by clicking on a hyperlink in a text field?

    I have several hyperlinks to different .pdf documents.

    Some open and keep focus when clicked.

    Others open, but the focus returns to the Access database.

    Thank you


    Nath

    Thursday, March 16, 2017 1:31 PM

Answers

  • After opening the file, you can use code like: http://www.everythingaccess.com/tutorials.asp?ID=Bring-an-external-application-window-to-the-foreground to set the focus on the document. 

    My users used to experience similar behavior that you describe, some users the files opened and remained in focus and others not, for no reason that I could ever figure out. I now use a slightly modified version of the above code in almost all my database when launching external documents.  The one thing I will add is I never use hyperlinks, they cause headaches to work with.  I simply store everything as plain text and launch them using Shell command, or the likes.


    Daniel Pineault, 2010-2016 Microsoft MVP
    Professional Support: http://www.cardaconsultants.com
    MS Access Tips and Code Samples: http://www.devhut.net


    Thursday, March 16, 2017 2:34 PM
  • Hi NaPazz,

     you can also try to use AppActivate or explorer.exe Folders

    The AppActivate statement changes the focus to the named application or window but does not affect whether it is maximized or minimized. Focus moves from the activated application window when the user takes some action to change the focus or close the window. Use the Shell function to start an application and set the window style.

    example:

    Public vPID As Variant
    Public Sub OpenApplication()
        'Launch application if not already open
        If vPID = 0 Then 'Application not already open
    101:
            vPID = Shell("C:\Windows\system32\notepad.exe", vbNormalFocus)
        Else 'Application already open so reactivate
            On Error GoTo 101
            AppActivate (vPID)
        End If
    End Sub

    Reference:

    AppActivate Statement

    explorer.exe Folders:

    Public Sub OpenExplorer()
        'Launch folder if not already open
        Dim strDirectory As String
        Dim pID As Variant, sh As Variant
        strDirectory = "C:\Users\Ryan\Documents"
        On Error GoTo 102:
        Set sh = CreateObject("shell.application")
        For Each w In sh.Windows
            If w.document.folder.self.Path = strDirectory Then 'if already open, activate it
                w.Visible = False
                w.Visible = True
                Exit Sub
            End If
        Next
        'if you get here, the folder isn't open so open it
        pID = Shell("explorer.exe " & strDirectory, vbNormalFocus)
    102:
    End Sub

    Reference:

    explorer.exe Folders

    Regards

    Deepak


    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 NaPazz Tuesday, March 28, 2017 12:42 PM
    Friday, March 17, 2017 6:03 AM
    Moderator

All replies

  • After opening the file, you can use code like: http://www.everythingaccess.com/tutorials.asp?ID=Bring-an-external-application-window-to-the-foreground to set the focus on the document. 

    My users used to experience similar behavior that you describe, some users the files opened and remained in focus and others not, for no reason that I could ever figure out. I now use a slightly modified version of the above code in almost all my database when launching external documents.  The one thing I will add is I never use hyperlinks, they cause headaches to work with.  I simply store everything as plain text and launch them using Shell command, or the likes.


    Daniel Pineault, 2010-2016 Microsoft MVP
    Professional Support: http://www.cardaconsultants.com
    MS Access Tips and Code Samples: http://www.devhut.net


    Thursday, March 16, 2017 2:34 PM
  • Hi NaPazz,

     you can also try to use AppActivate or explorer.exe Folders

    The AppActivate statement changes the focus to the named application or window but does not affect whether it is maximized or minimized. Focus moves from the activated application window when the user takes some action to change the focus or close the window. Use the Shell function to start an application and set the window style.

    example:

    Public vPID As Variant
    Public Sub OpenApplication()
        'Launch application if not already open
        If vPID = 0 Then 'Application not already open
    101:
            vPID = Shell("C:\Windows\system32\notepad.exe", vbNormalFocus)
        Else 'Application already open so reactivate
            On Error GoTo 101
            AppActivate (vPID)
        End If
    End Sub

    Reference:

    AppActivate Statement

    explorer.exe Folders:

    Public Sub OpenExplorer()
        'Launch folder if not already open
        Dim strDirectory As String
        Dim pID As Variant, sh As Variant
        strDirectory = "C:\Users\Ryan\Documents"
        On Error GoTo 102:
        Set sh = CreateObject("shell.application")
        For Each w In sh.Windows
            If w.document.folder.self.Path = strDirectory Then 'if already open, activate it
                w.Visible = False
                w.Visible = True
                Exit Sub
            End If
        Next
        'if you get here, the folder isn't open so open it
        pID = Shell("explorer.exe " & strDirectory, vbNormalFocus)
    102:
    End Sub

    Reference:

    explorer.exe Folders

    Regards

    Deepak


    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 NaPazz Tuesday, March 28, 2017 12:42 PM
    Friday, March 17, 2017 6:03 AM
    Moderator