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

Posted on 2009-12-23
Medium Priority
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
  • 3
  • 2
  • 2
LVL 27

Assisted Solution

CaptainCyril earned 200 total points
ID: 26116248
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 43

Accepted Solution

pcelba earned 1800 total points
ID: 26116625
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

ID: 26130906
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,
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 43

Expert Comment

ID: 26133033
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"

USE testgen

Open in new window


Author Closing Comment

ID: 31669618
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

ID: 26135761
You are welcome.

Merry Christmas.
LVL 43

Expert Comment

ID: 26137745
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!

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
Lost Word File? Eagerly, need it back? Read ahead; this File Recovery guide is for you.
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

578 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