Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bring MS word to front

Status
Not open for further replies.

AlastairP

Technical User
Feb 8, 2011
286
AU
I am trying to get msword 2007 to come to the front after creating by

oWord = CreateObject("word.application")

I have tried a bunch of different methods including:

faq184-4262

What ever method is used, MS word still requires clicking in the task bar to bring to front.


here is the code

Code:
PARAMETERS lcWname

oGUID = CreateObject("scriptlet.typelib")
cGUID = substr( oGUID.GUID, 2, 36 )	

oWord = CreateObject("word.application")

With oWord
   .Documents.add
   .Selection.TypeText(ThisForm.Edit1.Value)
   .ActiveDocument.SaveAs(pnDefaultPath + "DOCS\TEMPLATES\" + cGUID + ".doc")
   .Visible = .T.

		DECLARE INTEGER GetDesktopWindow IN Win32API
		DECLARE INTEGER GetWindow IN Win32API ;
		    INTEGER hwnd ,;
		    INTEGER dflag
		DECLARE INTEGER GetWindowText IN Win32API ;
		    INTEGER hwnd ,;
		    STRING @lptstr ,;
		    INTEGER cbmax

		LOCAL lnhwnd ,;
		    lctitle_bar ,;
		    lntext_len

		lnhwnd = GetDesktopWindow()
		lnhwnd = GetWindow(lnhwnd,5)
		lctitle_bar    = []

		DO WHILE !EMPTY(lnhwnd)
		    lctitle_bar = SPACE(200) + CHR(0)
		    lntext_len = GetWindowText(lnhwnd, @lctitle_bar, 200)
		    lctitle_bar = UPPER(LEFT(lctitle_bar, lntext_len))
		    IF !EMPTY(lctitle_bar)
		    	INSERT INTO cTemp (wName) VALUES (lctitle_bar)
			    ENDIF
		    lnhwnd    = GetWindow(lnhwnd,2)
		ENDDO

		SELECT cTemp
		LOCATE FOR cTemp.wNAme = cGUID
		IF FOUND()
			SELECT cTemp
			lcWname=cTemp.wNAme
			DO front WITH lcWname
		ENDIF
EndWith


Function Front

Code:
PARAMETERS lcWname

DECLARE Long FindWindow In Win32API String, String

nHWND = FindWindow(Null,lcWname)

If nHWND >0
    =ForceForegroundWindow(nHWND)
ENDIF

FUNCTION ForceForegroundWindow(lnHWND)

    LOCAL nForeThread, nAppThread
    
    =decl()
    
    nForeThread = GetWindowThreadProcessId(GetForegroundWindow(), 0)
    nAppThread = GetCurrentThreadId()

    IF nForeThread != nAppThread
        AttachThreadInput(nForeThread, nAppThread, .T.)
        BringWindowToTop(lnHWND)
        ShowWindow(lnHWND,3)
        AttachThreadInput(nForeThread, nAppThread, .F.)
    ELSE
        BringWindowToTop(lnHWND)
        ShowWindow(lnHWND,3)
    ENDIF
    
ENDFUNC

FUNCTION Decl
*!*        DECLARE INTEGER SetForegroundWindow IN user32 INTEGER hwnd  
    DECLARE Long BringWindowToTop In Win32API Long

    DECLARE Long ShowWindow In Win32API Long, Long

    DECLARE INTEGER GetCurrentThreadId;
        IN kernel32
     
    DECLARE INTEGER GetWindowThreadProcessId IN user32;
        INTEGER   hWnd,;
        INTEGER @ lpdwProcId  
     
    DECLARE INTEGER GetCurrentThreadId;
        IN kernel32  
        
    DECLARE INTEGER AttachThreadInput IN user32 ;
        INTEGER idAttach, ;
        INTEGER idAttachTo, ;
        INTEGER fAttach

    DECLARE INTEGER GetForegroundWindow IN user32  
    
ENDFUNC

Any ideas?
 
I have solved the problem as below. This varient used the window handle and works.

Code:
PARAMETERS lcWname

oGUID = CreateObject("scriptlet.typelib")
cGUID = substr( oGUID.GUID, 2, 36 )	

oWord = CreateObject("word.application")

With oWord
   .Documents.add
   .Selection.TypeText(ThisForm.Edit1.Value)
   .ActiveDocument.SaveAs(pnDefaultPath + "DOCS\TEMPLATES\" + cGUID + ".doc")
   .Visible = .T.
		DECLARE INTEGER GetDesktopWindow IN Win32API
		DECLARE INTEGER GetWindow IN Win32API ;
		    INTEGER hwnd ,;
		    INTEGER dflag
		DECLARE INTEGER GetWindowText IN Win32API ;
		    INTEGER hwnd ,;
		    STRING @lptstr ,;
		    INTEGER cbmax

		LOCAL lnhwnd ,;
		    lctitle_bar ,;
		    lntext_len

		lnhwnd = GetDesktopWindow()
		lnhwnd = GetWindow(lnhwnd,5)
		lctitle_bar    = []

		DO WHILE !EMPTY(lnhwnd)
		    lctitle_bar = SPACE(200) + CHR(0)
		    lntext_len = GetWindowText(lnhwnd, @lctitle_bar, 200)
		    lctitle_bar = UPPER(LEFT(lctitle_bar, lntext_len))
		    IF !EMPTY(lctitle_bar)
		    	INSERT INTO cTemp (wName,whandle) VALUES (lctitle_bar,lnhwnd)
			    ENDIF
		    lnhwnd    = GetWindow(lnhwnd,2)
		ENDDO

		SELECT cTemp
		LOCATE FOR cTemp.wNAme = cGUID
		IF FOUND()
			SELECT cTemp
			lcWname=cTemp.wHandle
			DECLARE INTEGER BringWindowToTop IN Win32API INTEGER hWnd
   			 BringWindowToTop(lcWname)  
		ENDIF
EndWith

no need for the second function

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top