FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Problem with connection to Word using OLE
Posts: 8515
Joined: Tue Dec 20, 2005 07:36 PM
Re: Problem with connection to Word using OLE
Posted: Mon Jun 09, 2025 02:22 PM
// Using Microsoft Office Word ActiveX

/*
#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
*/

/*
#include "FiveWin.ch"

FUNCTION Main()

   LOCAL oWnd, oActiveX, oWord

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

   IF File( "C:\TMP\ADHEMAR.TXT" )

      oWord:Documents:Open( "C:\TMP\ADHEMAR.TXT" )

   ELSE

      ? "File not found"

      RETURN NIL

   ENDIF

   oWord:Visible := .F.
   oWord:WindowState := 0

   oWord:PrintOut()

   oWord:Quit()
   oWord:End()

RETURN NIL
*/

#include "FiveWin.ch"

STATIC oWnd

FUNCTION Main()

   LOCAL oBar, cPdf

   cPdf := "C:\TMP\DATA_DOC.doc"

   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. .NOT. 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()  // Direct in Printer.
   // oDoc:Close()

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

   SETFOREGROUNDWINDOW( FINDWINDOW( "OpusApp" ) )

RETURN( .T. )

// FIN
Regards, saludos.
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Problem with connection to Word using OLE
Posted: Mon Jun 09, 2025 02:59 PM
driessen wrote: I know that. But if I want to do something in a document, I need to chose one of the documents. To d that, I need oDoc1:Activate() .OR. oDoc2:Activate()/
No, you don't. You only need to use the correct variable (ie. oDoc1:DoSomething(), oDoc2:DoSomething()).
Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM
Re: Problem with connection to Word using OLE
Posted: Mon Jun 09, 2025 03:32 PM

Isn't it very strange that my problem only occurs if I build my application with Harbour?

If I build my application with xHarbour Builder, I don't have any problems.

Regards,

Michel D.
Genk (Belgium)


_____________________________________________________________________________________________

I use : FiveWin for (x)Harbour v. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773

Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Problem with connection to Word using OLE
Posted: Mon Jun 09, 2025 03:49 PM
Yes, it is. And what is worse, it cannot be easily replicated, right? :-(
Posts: 8515
Joined: Tue Dec 20, 2005 07:36 PM
Re: Problem with connection to Word using OLE
Posted: Mon Jun 09, 2025 05:05 PM
Mister driessen, please test this example and show a complete example of how you are doing it, is that ok?
// C:\FWH\SAMPLES\WORD4.PRG

#include "Fivewin.ch"

FUNCTION Main()

   LOCAL cFile := CurDrive() + ":\" + CurDir() + "\test.doc"
   LOCAL oWord, oDocument, oText, cText, oRang

   TRY

      oWord := CreateObject( "Word.Application" )

   CATCH

      MsgAlert( FWString( "Word not installed" ) )

      RETURN NIL

   END

   oWord:Visible := .F.

   oDocument := oWord:documents:open( cFile )

   oDocument:Activate() // new

   oRang := oDocument:Content // new

   oRang:Select() // new

   oText := oWord:selection()

   cText := oText:Text

   oDocument:close() // close .doc

   oWord:Quit() // End

   MsgStop( cText )

RETURN NIL

// FIN / END
Regards, saludos.
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341

Continue the discussion