Focus a new Document and bring it in the foreground
Hello!
Before you look at the code some details:
I have a local addin (autostart). I use this for a form. The form collects templates form different places. I want to select a template and add a new document. Without problems I can add text to the new document by vba. At the end I close the form with "end".
My problem: the new docment is not in the foreground.
To fix my problem I started (without without an addin) by this way:
------------------
Option Explicit
Private Declare Function WindowFromAccessibleObject Lib "oleacc.dll" ( _
ByVal IAcessible As Object, ByRef hWnd As Long) As Long
Private Declare Function GetForegroundWindow Lib "user32.dll" ( _
) As Long
Private Declare Function AttachThreadInput Lib "user32" ( _
ByVal idAttach As Integer, _
ByVal idAttachTo As Integer, _
ByVal fAttach As Integer) As Integer
Private Declare Function GetWindowThreadProcessId Lib "user32.dll" ( _
ByVal hWnd As Long, _
lpdwProcessId As Long) As Long
Private Declare Function SetForegroundWindow Lib "user32.dll" ( _
ByVal hWnd As Long) As Long
Private Declare Function BringWindowToTop Lib "user32" ( _
ByVal hWnd As Long) As Long
Public Declare Function GetActiveWindow Lib "user32" () As Long
Sub test()
Dim docnew As Document
Dim idThreadForeground
Debug.Print GetForegroundWindow
Debug.Print GetActiveWindow
idThreadForeground = GetWindowThreadProcessId(GetForegroundWindow, 0)
Application.WindowState = wdWindowStateMinimize
Set docnew = Documents.Add
docnew.Activate
debug.Print GetForegroundWindow
Debug.Print GetActiveWindow
Application.WindowState = wdWindowStateMaximize
End Sub
-------------------
but without success!
I can't identify the new document.
Need Your help!!!
Fabian
|