Bring Word to the front when opened in ACCESS (1 Viewer)

jpl458

Well-known member
Local time
Yesterday, 23:57
Joined
Mar 30, 2012
Messages
1,038
I have a button with the following code that opens a specific Word document, but the Word doc opens behind ACCESS and I need it to be in front.

Code:
Dim LWordDoc As String
   Dim oApp As Object

   'Path to the word document
   LWordDoc = "c:\Users\jplor\OneDrive\Desktop\RoboData\Templates\Template1.docx"

   If Dir(LWordDoc) = "" Then
      MsgBox "Document not found."

   Else
      'Create an instance of MS Word
      Set oApp = CreateObject(Class:="Word.Application")
      oApp.Visible = True

      'Open the Document
      oApp.Documents.Open FileName:=LWordDoc
   End If

Is there a way to bring Word to the front altering this code?

Thanks
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 01:57
Joined
Feb 28, 2001
Messages
27,186
Since you are opening another totally new window (i.e. it is not an Access window even though Access opened indirectly), you have no control over it unless you know its window handle. This isn't the easiest "read" but the link shows you the steps to find your external window and move it to the front (top) of the display stack:

 

Eugene-LS

Registered User.
Local time
Today, 09:57
Joined
Dec 7, 2018
Messages
481
Is there a way to bring Word to the front altering this code?
With MS Word you can use .Activate method.

Code:
Private Sub OpenAndActivateWord()
'MS Word - Opening a document and activating application window
'---------------------------------------------------------------------------------------------------
Dim sDocPath As String
Dim objWordApp As Object

On Error GoTo OpenAndActivateWord_Err

'Path to the word document
    sDocPath = "c:\Users\jplor\OneDrive\Desktop\RoboData\Templates\Template1.docx"
    'sDocPath = "d:\Temp\007.docx"

    If Dir(sDocPath) = "" Then
        MsgBox "Document not found.", vbExclamation, "No File!"
        GoTo OpenAndActivateWord_End
    Else
        'Create an instance of MS Word
        Set objWordApp = CreateObject("Word.Application")
        objWordApp.Visible = True
        'Open the Document
        objWordApp.Documents.Open sDocPath
    End If

'Аctivating application window:
    objWordApp.Activate

OpenAndActivateWord_End:
    On Error Resume Next
    Set objWordApp = Nothing
    Err.Clear
    Exit Sub

OpenAndActivateWord_Err:
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in Sub : " & _
           "OpenAndActivateWord", vbCritical, "Error!"
    'Debug.Print "OpenAndActivateWord_Line: " & Erl & "."
    Err.Clear
    Resume OpenAndActivateWord_End
End Sub
 
Last edited:

jpl458

Well-known member
Local time
Yesterday, 23:57
Joined
Mar 30, 2012
Messages
1,038
Since you are opening another totally new window (i.e. it is not an Access window even though Access opened indirectly), you have no control over it unless you know its window handle. This isn't the easiest "read" but the link shows you the steps to find your external window and move it to the front (top) of the display stack:

Just read through it twice, and during the second trip, I started thinking that it wasn't worth the effort, right now, at my current experience level. Maybe later. Just clicking the edge of the word doc seems a whole lot simpler, if not as elegant. An awful lot of work to replace a click. I think that this could be a feature that the folks in Redmond should consider, considering that there is system entropy, where things move from the simple to the complex, involving multiple Office application working together as a unit. Seems it should be easier to do that. Just musing on a Sunday after breakfast . Saving the info for future reference. Plus, you saved me time because now I can work on more pressing needs.

Thanks again. Appreciate it.
 

Users who are viewing this thread

Top Bottom