Page 1 of 1

How to make MS WORD on top?

Posted: Thu Mar 19, 2020 11:42 am
by dutch
Dear All,

I use below code to activate MS Word but it show in background. I need to activate on top of my program.

Code: Select all

oWord := CreateObject("Word.Application")
oWord:Set("Visible",.t.)
oDoc:Open(cFile)
oWord:Set( "WindowState", 1 )  
 
How do I make on top?

Thanks in advance.

Re: How to make MS WORD on top?

Posted: Thu Mar 19, 2020 12:41 pm
by Marc Vanzegbroeck
Dutch,

You can use

Code: Select all

Setforegroundwindow(FindWindow("OpusApp",NIL))

Re: How to make MS WORD on top?

Posted: Thu Mar 19, 2020 1:54 pm
by ukoenig
Switch from TOP to NORMAL

@ 20, 80 BTNBMP oBtn1 2007 ;
SIZE 30, 30 OF oDlg NOBORDER ;
PROMPT "Top" ;
ACTION ( SetWindowPos( oDlg:hWnd, -1, 0, 0, 0, 0, 3 ),;
WinExec( "Notepad.exe" ) )

close Notepad and reopen with button 2

@ 20, 130 BTNBMP oBtn2 2007 ;
SIZE 30, 30 OF oDlg NOBORDER ;
PROMPT "Normal" ; // Notepad on TOP
ACTION ( SetWindowPos( oDlg:hWnd, -2, 0, 0, 0, 0, 3 ),;
WinExec( "Notepad.exe" ) )

regards
Uwe :D

Re: How to make MS WORD on top?

Posted: Thu Mar 19, 2020 3:49 pm
by dutch
Thanks both of you Marc and Uwe. I try and got it work now.

Re: How to make MS WORD on top?

Posted: Thu Mar 19, 2020 6:19 pm
by Otto
Hello Dutch,
would you be so kind to post some code.
Thank you in advance and best regards,
Otto

Re: How to make MS WORD on top?

Posted: Fri Mar 20, 2020 1:44 am
by dutch
Dear Otto,

I try to use oWord:Mailmerge in my program as customer requirement for make rental contract. I got this code in the forum and modify to support in the future by create temporary file for preparing data (MergeField in Word) and can add or modify in future case.
Otto wrote:Hello Dutch,
would you be so kind to post some code.
Thank you in advance and best regards,
Otto

Code: Select all

*---------------------------------*
Procedure DocMerge( cForm, cFile )
local oWord, oDoc, cOutFile, cTmpDbf, aTmpDbf, OkToWord , cName
local aDataField, aDatas, n 

CursorWait()

cOutFile := cFile // "d:\fwh1312\samples\testdoc.doc"
cTmpDbf     := MEMVAR->cFoPath+"GSTINFO.DBF" // "d:\fwh1312\samples\guest.dbf"
cFile   := MEMVAR->cFoPath+"LTRFORM\"+cForm // "d:\fwh1312\samples\Rental.doc"

aTmpDbf     := {} 
aDatas  := {} 

DbUseArea(.T.,,"EZMERGE","MRG",.T.)
do while !MRG->(eof())
    aadd( aTmpDbf, {rtrim(upper(MRG->MRG_NAME)), MRG->MRG_TYPE, MRG->MRG_LENGTH, MRG->MRG_DEC} )
    aadd( aDatas , {rtrim(upper(MRG->MRG_NAME)), rtrim(MRG->MRG_DATA)} )
    MRG->(DbSkip())
end
MRG->(DbCloseArea())

if file( cTmpDbf ) 
    ferase( cTmpDbf )
end
if !empty( aTmpDbf )
    
    DbCreate( cTmpDbf, aTmpDbf )
    
    sleep(1000) 
    
    DbUseArea(.T.,,"GSTINFO","GSI",.T.)
    GSI->(DbAppend())
    if GSI->(Rlock())
        for n := 1 to len(aDatas)
             &('GSI->'+aDatas[n][1]) := &(aDatas[n][2])
        next
    end
    GSI->(DbCommit())
    GSI->(DbCloseArea())
    
    oWord := CreateObject("Word.Application")
    oWord:Set("Visible",.f.)
    oDoc:=oWord:Get( "Documents" )
    oWord:Set("DisplayAlerts",0)
    oWord:Set("Visible",.F.)
    oWord:Set( "WindowState", 2 )  // Minimize
    
    oDoc:Open(cFile)
    oWord:ActiveDocument:MailMerge:MainDocumentType := 0 //wdFormLetters=0  // sets the mail merge main document type
    oWord:ActiveDocument:MailMerge:EditMainDocument()
    oWord:ActiveDocument:MailMerge:OpenDataSource(cTmpDbf)  //TmpDbf is the path&file name of temp database
    // oWord:ActiveDocument:MailMerge:Destination( 1 )  // 0=File, 1=Printer, 2=Email, 3=Fax
    oWord:ActiveDocument:MailMerge:Execute( .F. )
    cName:=oWord:Get("ActiveDocument")
    //about to save check to see if possible or if someone else
    //has file open
    
    
    cName:SaveAs(cOutFile)
    IF cName:Saved() == .F.
        MsgStop("Save to Word Fail")
        cOutFile := ''
    ENDIF         
    oWord:Documents:Close(.T.)
    oWord:Quit() 
    
    
    if file(cOutFile)
        ShellExecute(0, 'Open', cOutFile,,4,0)
    end
    
end
CursorArrow()
return