Page 1 of 1

Problem with OLE and Microsoft Word

PostPosted: Fri Apr 07, 2023 12:44 pm
by driessen
Hello,

I did this test:
Code: Select all  Expand view
oWord := TOleAuto():New("Word.Application")
oWord:Quit()
oWord:= NIL

oWord := TOleAuto():New("Word.Application")
oWord:Quit()
oWord:= NIL

oWord := TOleAuto():New("Word.Application")
oWord:Quit()
oWord:= NIL

I wanted to test the speed by which Word is opened and closed by using OLE.

After the Quit-command, it lasts at least 10 to 15 seconds before Word is effectivily closed.

What can be down to make this process much quicker?

Thank you very much for any advice.

Re: Problem with OLE and Microsoft Word

PostPosted: Fri Apr 07, 2023 1:37 pm
by Natter
You can find the process in Word memory (via WMI) as "OpusApp" and delete it
oProcess:Terminate()

Re: Problem with OLE and Microsoft Word

PostPosted: Fri Apr 07, 2023 2:58 pm
by driessen
Sorry, I don't have any experience with this. Can you please provide me with an example? Thanks.

Re: Problem with OLE and Microsoft Word

PostPosted: Fri Apr 07, 2023 4:59 pm
by Natter
Something like this
Code: Select all  Expand view
procedure KillProcess()
local st
local oWmi:=WmiService(), oList, oProc
private dim:={"WINWORD.EXE", "OpusApp", {}}

  EnumChildWindows( 0, ;
      { | hCt, nLParam | L_Tsk(hCt) }, 0 ) // optional supplied value

  for st=1 to len(dim)
    oList:= oWmi:ExecQuery( "select * from Win32_Process where Name = '" + ;
                                                          dim[st,1] + "'" )

    for each oProc in oList // list of processes
        oProc:Terminate()
    next
  next
return nil

Function L_Tsk(hWn) && CALLBACK of EnumChildWindows
local fl
** hWn - handle of window 

    fl:=ascan(dim, {|xx|xx[2]=GetClassName(hWn)})
    if fl>0 // this is the desired process 
      aadd(dim[fl,3], {GetWindowProcessID(hWn), hWn})
    endif
return .T.
 

Re: Problem with OLE and Microsoft Word

PostPosted: Fri Apr 07, 2023 5:52 pm
by karinha
Code: Select all  Expand view

#include "FiveWin.ch"

static oWnd

function Main()

   local oBar, cPdf

   //cPdf := "C:\FWH\SAMPLES\MODELO.PDF"       // NO FUNCIONA
   //cPdf := "C:\FWH\SAMPLES\MATERIAL.TXT"  // FUNCIONA, IMPRIME NORMAL.
   //cPdf := "C:\FWH1905\SAMPLES\MODELO.RTF"   // FUNCIONA, IMPRIME NORMAL.
   cPdf := "C:\FWH1905\SAMPLES\DATA_DOC.doc" // FUNCIONA, IMPRIME NORMAL.

   DEFINE WINDOW oWnd TITLE "Test Word"

   DEFINE BUTTONBAR oBar _3D OF oWnd

   DEFINE BUTTON OF oBar ACTION( PrintPDF( cPdf ) )

   SET MESSAGE OF oWnd TO "Test Word" NOINSET CLOCK DATE KEYBOARD

   ACTIVATE WINDOW oWnd

return nil

function PrintPDF( cPdf )

   local oWord, oDoc, lVisible

   if Empty( cPdf ) .or. !File( cPdf )

      MsgAlert( "Invalid File Name" )

      return .f.

   elseif ( oWord := WinWordObj() ) == nil

      MsgAlert( FWString( "Word not installed" ) )

      return .f.

   endif

   oWord:DisplayAlerts := .f.

   lVisible := oWord:Visible

   // oWord:Visible := .f.

   oDoc := oWord:Documents:Open( truename( cPdf ), .f., .t. )

   // oDoc:PrintOut()  // Se que quiser direto na impressora de PDF.
   // oDoc:Close()

   // oWord:Visible := lVisible
   oWord:Visible = .T.
   oWord:WindowState = 1 // Maximize

   SETFOREGROUNDWINDOW( FINDWINDOW( "OpusApp" ) )

return .t.

// FIN
 


Code: Select all  Expand view

// http://forums.fivetechsupport.com/viewt ... ff84ac719d

#include "Fivewin.ch"

FUNCTION MAIN()

   LOCAL cDoc := CURDRIVE() + ":\" + CURDIR() + "\test.doc"
   LOCAL oWord

   If FILE( cDoc )

      oWord = TOleAuto():New( "
Word.Application" )
      oWord:Documents:Open( cDoc )
      oWord:Visible = .T.
      oWord:WindowState = 1 // Maximize

      SETFOREGROUNDWINDOW( FINDWINDOW( "
OpusApp" ) )

   ENDIF

RETURN NIL


Regards, saludos.

Re: Problem with OLE and Microsoft Word

PostPosted: Fri Apr 07, 2023 6:27 pm
by driessen
Guy,s

Thank you so much for your help.

I know what to do this weekend. Testing, testing, testing.

Happy Eastern to you all.

Re: Problem with OLE and Microsoft Word

PostPosted: Fri Apr 07, 2023 6:54 pm
by Natter
Maybe, I didn't check, you can try

oWrd:=GetActiveObject(...
....................
SendMessage(oWrd:hWnd, VM_CLOSE)