How would I determinine the difference (programmatically) between a word and wordperfect document in a foxpro General field?

Posted on 2009-12-23
Last Modified: 2012-05-08
I work for a company that needs a automated solution to extract the general and memo fields from a large database. the general field houses WordPerfect and Microsoft word files. I need to figure out how to determine what they are at runtime. We were going to use dates, but different offices switched from WPD to DOC files at different times(we dont know when).
I can extract the word files just fine, have not written code to extract the WPD files yet though. what I need to know is if anyone knows a way to determine what kind of file is in the Gen field at program execution time (runtime). if you have code ideas, please submit, if you have any ideas, please submit!

questions? please ask.

Thanks and have a fantastic holiday!
* MS Word Extraction *

USE inspctn.dbf

GO top


IF (ISBLANK(inspctn.ciulkey) = .F.)

loForm = CREATEOBJECT("Form")

* - create the word object - 

oWord = CREATEOBJECT("Word.Application") 


loForm.addobject("oleboundcontrol1", "oleboundcontrol") 

loForm.oleboundcontrol1.autosize = .T. 

* bind general field to oleboundcontrol 

loForm.oleboundcontrol1.controlsource = "inspctn.gnarrative" 

* save data from general field to .doc file 

loForm.oleboundcontrol1.saveas("c:\TestFolder\"+ inspctn.ciulkey + "_2.doc") 

* open the word file to be merged 

oWord.Documents.Open("c:\TestFolder\"+ inspctn.ciulkey + "_2.doc") 

oWord.ActiveDocument.PageSetup.PageWidth = "8.5"

oWord.ActiveDocument.PageSetup.PageHeight = "11"

oWord.Activedocument.saveas("c:\TestFolder\"+ inspctn.ciulkey + "_2.doc")





Open in new window

Question by:Maelificus
    LVL 27

    Assisted Solution

    I don't know but maybe I can guide you with 2 ideas.

    Open the document programatically. See which applications runs and automate the saving.

    Force the document to open with one application. If it fails then try the other.
    LVL 41

    Accepted Solution

    First few bytes of each file represents its format obviously.
    Word .DOC has: D0 CF 11
    WordPerfect: FF 57 50 43

    This should be sufficient enough to recognize these two file types (see code snippet).

    But you probably need to know the file type prior to its extraction... One trick exists - create a copy of your table (or a copy of just one record) and change the General field data type to Memo by direct change in DBF heading (you have to use low level file function or hexeditor). Then you may read the memo and look for above byte sequences and other values.

    lnH = FOPEN("c:\TestFolder\"+ inspctn.ciulkey + "_2.doc")
    IF lnH < 0
      *-- Error opening file
    lcHdr = FREAD(lnH, 3)  && You may read more bytes...
    CASE ASC(lcHdr) = 0xD0 AND ASC(SUBSTR(lcHdr, 2, 1)) = 0xCF AND ASC(SUBSTR(lcHdr, 3, 1)) = 0x11
      *-- Word .DOC file
    CASE ASC(lcHdr) = 0xFF AND ASC(SUBSTR(lcHdr, 2, 1)) = 0x57 AND ASC(SUBSTR(lcHdr, 3, 1)) = 0x50 AND ASC(SUBSTR(lcHdr, 2, 1)) = 0x43
      *-- WordPerfect file
      *-- Unknown file

    Open in new window


    Author Comment

    Thanks for the responses, I forgot to mention I was going on x-mas break last week.
     I will see what I can do with your suggestions and get back with you soon!
    Thanks again,
    LVL 41

    Expert Comment

    OK, no problem. I've been testing the General field conversion into Memo and it works. The code is very simple:
    USE YourGeneralTable
    *-- Locate some record containing non empty General field
    COPY NEXT 1 FIELD SomeOLE TO testgen.dbf
    lnH = FOPEN('testgen.dbf', 2)
    = FSEEK(lnH,43)
    IF FREAD(lnH,1) = "G"
      = FSEEK(lnH,43)
      = FWRITE(lnH,"M")
      ? "Success"
    = FCLOSE(lnH)
    USE testgen

    Open in new window


    Author Closing Comment

    I awarded CaptainCyril 50 points because I sort-of did what he suggested, but I awarded the rest to Pcelba for offering a more informative answer(s).
    Pcelba, I didnt use your solution, but I used various parts of the code you suggested.
    Thank you both for your help!!!
    LVL 27

    Expert Comment

    You are welcome.

    Merry Christmas.
    LVL 41

    Expert Comment

    I like the situation when ideas are sufficient as a solution. Now I even see there is a bug in my code ( instead of ... ASC(SUBSTR(lcHdr, 2, 1)) = 0x43 ... should be ... ASC(SUBSTR(lcHdr, 4, 1)) = 0x43 ... on line 13).

    So, I am sorry.

    And Happy New Year!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Like many others, we try and discourage users from printing documents unnecessarily and instead send or share them electronically. However, this doesn't always work and documents are still printed. With this simple solution, if the user tries to …
    This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
    This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
    In a previous video Micro Tutorial here at Experts Exchange (, I explained how to get a free, one-month trial of Office …

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now