Word doc opens in "back"

hiwelcome

Registered User.
Local time
Today, 02:57
Joined
Aug 14, 2015
Messages
47
Hello,

Using this code I found elsewhere to open up a word document using a button on a form:


Private Sub ButtonAK_Click()
Dim APDKey As String
Dim App As Object

APDKey = "C:\Desktop\AK\2014.doc"

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

Else

Set App = CreateObject(Class:="Word.Application")
App.Visible = True


App.Documents.Open FileName:=APDKey

End If
End Sub


The word document does open, but my small problem is it opens in the background. The document is a info guide so I would like word to open in front of the form for ease of reading (have priority or however it is phrased). Is there code to do this?
 
This should do it:
Code:
App.Application.Activate
 
#If Win64 Then
Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal hwnd As LongPtr) As Long

Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
#Else
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

Private Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
#End If



Private Sub ButtonAK_Click()
Dim APDKey As String
Dim App As Object
Dim strPath As String
#If Win64 Then
Dim hwnd As LongPtr
#Else
Dim hwnd As Long
#End If

APDKey = "C:\Desktop\AK\2014.doc"

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

Else

Set App = CreateObject(Class:="Word.Application")
App.Visible = True


App.Documents.Open FileName:=APDKey

hwnd = FindWindow("OpusApp", vbNullString)
If hwnd > 0 Then
SetForegroundWindow (hwnd)
End If

End If
End Sub
 
You're welcome, good luck. :)
 

Users who are viewing this thread

Back
Top Bottom