FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Recognizing a Word-document
Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM
Recognizing a Word-document
Posted: Fri Mar 11, 2022 09:21 AM

Hello,

Is there a way to find out what format a document it is made in, which i need to open in Word from my FWH-application.
Is it an RTF-document, is it DOC, is it DOCX?
I want to know it, without taking the filename extension into considuration.
Thank you very much.

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: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: Recognizing a Word-document
Posted: Fri Mar 11, 2022 03:17 PM

Hmm, why not use the extension? That seems to be the simplest way.

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 6983
Joined: Fri Oct 07, 2005 07:07 PM
Re: Recognizing a Word-document
Posted: Fri Mar 11, 2022 07:35 PM

Michel, what about opening with memoread?

In a DOC file, you will find {\rtf1\ were as a docx is a zip file.

Best regards,
Otto

Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Recognizing a Word-document
Posted: Sun Mar 13, 2022 10:40 PM

Existing FWH function MemoryBufferType is extended for next release.

function MemoryBufferType( cBuf )

   local cType, n, a
   local cPunct   := Chr( 9 ) + Chr( 10 ) + Chr( 12 ) + Chr( 13 ) + Chr( 26 ) + Chr( 141 )
   local lExact   := Set( _SET_EXACT, .f. )
   local lBinary  := .f.
   local aTypes   := { ;
         { "IMG.BMP",   "BM",                                  .t. }, ;
         { "IMG.PNG",   Chr( 0x89 ) + "PNG",                   .f. }, ;
         { "IMG.ICO",   Chr( 0 ) + Chr( 0 ) + Chr( 1 ),        .f. }, ;
         { "IMG.JPG",   Chr( 255 ) + Chr( 216 ) + Chr( 255 ),  .f. }, ;
         { "IMG.GIF",   "GIF8",                                .t. }, ;
         { "IMG.TIF",   Chr(73) + Chr(73) + Chr(42),           .t. }, ;
         { "IMG.TIF",   Chr(77) + Chr(77) + Chr(42),           .t. }, ;
         { "IMG.EMF",   Chr( 1 ) + Chr( 0 ) + Chr( 0 ) + Chr( 0 ),   .f., }, ;
         { "IMG.WMF",   HEXTOSTR( "D7CDC69A00" ),              .f. }, ;
         { "DOC.XML",   "<?xml ",                              .f. }, ;
         { "DOC.RTF",   "{\rtf",                               .f. }, ;
         { "DOC.GTF",   "GTF" + Chr( 5 ),                      .f. }, ;
         { "DOC.PDF",   "%PDF-",                               .f. }, ;
         { "DOC.DOC",   HEXTOSTR( "D0CF11E0A1B11AE1" ),        .f. }, ;
         { "DOC.DCX",   HEXTOSTR( "504B030414" ),              .f. }  }

   for each a in aTypes
      if cBuf = a[ 2 ]
         if a[ 3 ]
            if IsBinaryData( SubStr( cBuf, 3, 15 ) )
               cType    := a[ 1 ]
            endif
         else
            cType    := a[ 1 ]
         endif
         EXIT
      endif
   next
   Set( _SET_EXACT, lExact )
   if ! Empty( cType )
      return cType
   endif

   lBinary     := IsBinaryData( cBuf )
   if lBinary
      if FreeImageIsLoaded() .and. ( n := IfNil( FITypeFromMemory( cBuf ), -1 ) ) >= 0
         return "IMG." + cValToChar( n )
      endif
      cType := "BIN.HEX"
   else
      if IsUTF8( cBuf )
         return "TXT.UTF8"
      else
         cType    := "TXT.ANSI"
      endif
   endif

return cType

This function can recognize if the given text is an image type or a doc type ( RTF, GTF, PDF, DOC, DCX (for DOCX), etc

Regards



G. N. Rao.

Hyderabad, India

Continue the discussion