Bring Word to the front when opened in ACCESS

jpl458

Well-known member
Local time
Today, 07:13
Joined
Mar 30, 2012
Messages
1,218
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
 
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:

 
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:
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

Back
Top Bottom