Index
2010-05-27 10:29Ken Dibble : Word Automation: OLE exception error 5981 in Windows 7
2010-05-27 16:18Jarvis, Matthew : RE: Word Automation: OLE exception error 5981 in Windows 7
2010-06-02 20:17Ken Dibble : RE: Word Automation: OLE exception error 5981 in Windows 7
2010-06-02 20:59Bill Arnold : RE: Word Automation: OLE exception error 5981 in Windows 7
2010-06-03 00:03Ken Dibble : RE: Word Automation: OLE exception error 5981 in Windows 7
2010-06-03 08:27Richard Kaye : RE: Word Automation: OLE exception error 5981 in Windows 7
2010-06-03 16:01Ken Dibble : RE: Word Automation: OLE exception error 5981 in Windows 7
2010-06-03 17:30Bill Arnold : RE: Word Automation: OLE exception error 5981 in Windows 7
2010-06-04 11:49Ken Dibble : RE: Word Automation: OLE exception error 5981 in Windows 7
2010-06-04 13:14Bill Arnold : RE: Word Automation: OLE exception error 5981 in Windows 7
Back to top
Word Automation: OLE exception error 5981 in Windows 7

Author: Ken Dibble

Posted: 2010-05-27 10:29:16   Link

Hi folks,

I have some Word automation code that worked fine in Win 2000 but is not

working in Win 7. Yeah, I'm using Word 97, but I have heard that this

problem can also occur with Word 2003, so let's for the sake of argument

hold off on the jeering and see if there are any solutions, okay? :)

I can get a Word object reference without problems but in the code:

WITH oWord

.documents.add <<< VFP Error 1429, OLE Exception Error 5981 "could

not open macro storage"

Googling this gets a lot of results, which seem to fall into two categories:

Normal.dot is corrupted, or

There's a permissions issue affecting access to Normal.dot

Word had no problem seeing and using Normal.dot in manual operation, but on

the chance that it was subtly corrupted I renamed it as OldNormal.dot and

tried the automation. That should have forced creation of a new

Normal.dot--which it did--but I got the same error.

As for permissions: I'm testing this in Win 7 while running with a Domain

Administrator account. So theoretically I should have permissions to access

everything. However...

I noticed that the default location for Normal.dot in Word 97 is \Program

Files\Microsoft Office\Office". I realize that in Win 7 this is not a

writeable location by default, and I saw that Win 7 had actually put

Normal.dot into the \Virtual Store\ folder in my profile.

So I tried changing the location for Normal.dot to a folder that should be

accessible. I tried both putting it in \Users\Public\Public Documents, and

in my My Documents folder, in each case telling Word to find it there. Word

can see and use the template during manual operation, but during automation

I still get the same error.

After the error--which hangs Word--regardless of whether I forced creation

of a new Normal.dot or let it use the old one--I can see a Word temp file

in the templates location called "~$Normal.dot". The "normal" Normal.dot

file is 27 kb; this temp file is 1 kb, which suggests to me that Word got

as far as creating the temp file but not writing anything to it.

In my Googling I had found a Microsoft KB article (KB 224338) indicating

that one cause of this could be that Win 7 does not actually load the

registry "hive" for the user who installed Word. That doesn't make sense in

this context because I'm running as Domain Administrator, and that's the

account I installed Word in.

But, Word 97 doesn't natively understand NT-style security and to get it to

work on Win 2000 and later you have to explicitly grant restricted user

accounts permissions for various features, and you have to grant them

Modify rights on the \Program Files\Office folder to give them access to

Normal.dot if it's there. Again, that shouldn't be directly relevant to me

because I'm not running as a restricted user--but just in case, I

explicitly granted not just Modify, but Full Control rights to my Domain

User account to everything in the Word tree in the Registry, and to the

\Program Files\Office\ folder tree.

Again, no dice, same error.

So I don't see how this can be either a permissions or corruption problem.

But the MS KB article suggests some things to try. One of them is to start

Word manually before running the automation code. That would not be

desirable because the automation is complex and it would slow it down

considerably. But in any case, that doesn't work--same error. Because of

that I doubt the other suggestions in the article, to somehow load the

appropriate registry hive, or to do a "run as" administrator,

programmatically, would work either. In any case, I don't know how to do

those things.

So... can anyone shed any light on this? Have you had the problem with Word

2003 in Win 7?

Thanks very much in advance.

Ken Dibble

Southern Tier Independence Center, Inc.

www.stic-cil.org

_______________________________________________

Post Messages to: ProFox@leafe.com

Subscription Maintenance: http://leafe.com/mailman/listinfo/profox

OT-free version of this list: http://leafe.com/mailman/listinfo/profoxtech

Searchable Archive: http://leafe.com/archives/search/profox

This message: http://leafe.com/archives/byMID/profox/5.2.1.1.1.20100527095230.01c04710@pop3.frontiernet.net

** All postings, unless explicitly stated otherwise, are the opinions of the author, and do not constitute legal or medical advice. This statement is added to the messages for those lawyers who are too stupid to see the obvious.

©2010 Ken Dibble
Back to top
RE: Word Automation: OLE exception error 5981 in Windows 7

Author: Jarvis, Matthew

Posted: 2010-05-27 16:18:05   Link

> -----Original Message-----

> From: profoxtech-bounces@leafe.com

[mailto:profoxtech-bounces@leafe.com]

> On Behalf Of Ken Dibble

> Sent: Thursday, May 27, 2010 7:29 AM

> To: profoxtech@leafe.com

> Subject: Word Automation: OLE exception error 5981 in Windows 7

>

> Hi folks,

>

> I have some Word automation code that worked fine in Win 2000 but is

not

> working in Win 7. Yeah, I'm using Word 97, but I have heard that this

> problem can also occur with Word 2003, so let's for the sake of

argument

> hold off on the jeering and see if there are any solutions, okay? :)

>

>>>pain snipped

Sounds like you've stepped into a real quagmire there....

It makes my brain hurt to try and follow along the normal.dot and

permissions and Win7 and Win NT and...... stuff....

BUT I can offer two minor, probably meaningless things to at least try.

1 - Clear temp files and such and run scandisk (or Win 7 equiv)

2 - In Task Manager, display the processes running and make sure you

don't already have an occurance of Word running. Playing with automation

often times leaves these Office apps dangling....

Sorry, that's all I gots....

Thanks,

Matthew Jarvis || Business Systems Analyst

IT Department

McKenzie-Willamette Medical Center

1460 G Street, Springfield, OR 97477 || Ph: 541-744-6092 || Fax:

541-744-6145

--------------------------------------------------------------------------

Disclaimer: This electronic message may contain information that is

Proprietary, Confidential, or legally privileged or protected. It

is intended only for the use of the individual(s) and entity named

in the message. If you are not an intended recipient of this

message, please notify the sender immediately and delete the

material from your computer. Do not deliver, distribute or copy

this message and do not disclose its contents or take any action in

reliance on the information it contains.

_______________________________________________

Post Messages to: ProFox@leafe.com

Subscription Maintenance: http://leafe.com/mailman/listinfo/profox

OT-free version of this list: http://leafe.com/mailman/listinfo/profoxtech

Searchable Archive: http://leafe.com/archives/search/profox

This message: http://leafe.com/archives/byMID/profox/69F310C05DD83C48A84BA3769CE1ECF8033FE5DC@TNTRIEXEVS02.triadhospitals.net

** All postings, unless explicitly stated otherwise, are the opinions of the author, and do not constitute legal or medical advice. This statement is added to the messages for those lawyers who are too stupid to see the obvious.

©2010 Jarvis, Matthew
Back to top
RE: Word Automation: OLE exception error 5981 in Windows 7

Author: Ken Dibble

Posted: 2010-06-02 20:17:02   Link

> > I have some Word automation code that worked fine in Win 2000 but is

>not

> > working in Win 7. Yeah, I'm using Word 97, but I have heard that this

> > problem can also occur with Word 2003, so let's for the sake of

>argument

> > hold off on the jeering and see if there are any solutions, okay? :)

> >

> >>>pain snipped

>

>1 - Clear temp files and such and run scandisk (or Win 7 equiv)

Hm..would that be right before I reformat the hard drive and reinstall

Windows? :)

I know, you told me you're grasping at straws, and I appreciate the response.

But Word 97 itself works just fine in Windows 7. It's just the automation

that's messing up.

>2 - In Task Manager, display the processes running and make sure you

>don't already have an occurance of Word running. Playing with automation

>often times leaves these Office apps dangling....

Yes, it leaves Word dangling every time it throws the OLE exception, but I

always go into Task Manager and kill it before trying again.

I'm pretty surprised that nobody else has any insight into this. If you're

doing Word automation with Office 2003 or earlier and VFP 9 in Windows 7,

would you at least please let me know what your experience has been?

Thanks.

Ken Dibble

www.stic-cil.org

_______________________________________________

Post Messages to: ProFox@leafe.com

Subscription Maintenance: http://leafe.com/mailman/listinfo/profox

OT-free version of this list: http://leafe.com/mailman/listinfo/profoxtech

Searchable Archive: http://leafe.com/archives/search/profox

This message: http://leafe.com/archives/byMID/profox/5.2.1.1.1.20100602200743.01f99a30@pop3.frontiernet.net

** All postings, unless explicitly stated otherwise, are the opinions of the author, and do not constitute legal or medical advice. This statement is added to the messages for those lawyers who are too stupid to see the obvious.

©2010 Ken Dibble
Back to top
RE: Word Automation: OLE exception error 5981 in Windows 7

Author: Bill Arnold

Posted: 2010-06-02 20:59:14   Link

Ken,

This KB related to the general topic "Could Not Open Macro Storage" might be

informative?

http://support.microsoft.com/kb/224338

Bill

> -----Original Message-----

> From: profoxtech-bounces@leafe.com

> [mailto:profoxtech-bounces@leafe.com] On Behalf Of Ken Dibble

> Sent: Wednesday, June 02, 2010 8:17 PM

> To: profoxtech@leafe.com

> Subject: RE: Word Automation: OLE exception error 5981 in Windows 7

>

>

>

> > > I have some Word automation code that worked fine in Win

> 2000 but is

> >not

> > > working in Win 7. Yeah, I'm using Word 97, but I have

> heard that this

> > > problem can also occur with Word 2003, so let's for the sake of

> >argument

> > > hold off on the jeering and see if there are any

> solutions, okay? :)

> > >

> > >>>pain snipped

> >

> >1 - Clear temp files and such and run scandisk (or Win 7 equiv)

>

> Hm..would that be right before I reformat the hard drive and

> reinstall

> Windows? :)

> I know, you told me you're grasping at straws, and I

> appreciate the response.

>

> But Word 97 itself works just fine in Windows 7. It's just

> the automation

> that's messing up.

>

> >2 - In Task Manager, display the processes running and make sure you

> >don't already have an occurance of Word running. Playing

> with automation

> >often times leaves these Office apps dangling....

>

> Yes, it leaves Word dangling every time it throws the OLE

> exception, but I

> always go into Task Manager and kill it before trying again.

>

> I'm pretty surprised that nobody else has any insight into

> this. If you're

> doing Word automation with Office 2003 or earlier and VFP 9

> in Windows 7,

> would you at least please let me know what your experience has been?

>

> Thanks.

>

> Ken Dibble

> www.stic-cil.org

>

>

[excessive quoting removed by server]

_______________________________________________

Post Messages to: ProFox@leafe.com

Subscription Maintenance: http://leafe.com/mailman/listinfo/profox

OT-free version of this list: http://leafe.com/mailman/listinfo/profoxtech

Searchable Archive: http://leafe.com/archives/search/profox

This message: http://leafe.com/archives/byMID/profox/6C103504DB5A46C98DD071AE4661A2B0@bills

** All postings, unless explicitly stated otherwise, are the opinions of the author, and do not constitute legal or medical advice. This statement is added to the messages for those lawyers who are too stupid to see the obvious.

©2010 Bill Arnold
Back to top
RE: Word Automation: OLE exception error 5981 in Windows 7

Author: Ken Dibble

Posted: 2010-06-03 00:03:01   Link

>This KB related to the general topic "Could Not Open Macro Storage" might be

>informative?

>

>http://support.microsoft.com/kb/224338

Thanks Bill. I had already seen that early on. I did try some of the

suggestions in that article, and they didn't work. I don't know how to

"programmatically" load a different registry hive or start word as a

different user. However, since I'm already doing everything with the same

Domain Administrator account I was running when I installed Office, and

beyond that I've explicitly granted every possible action permission to MS

Office, and "Full Control" permission for the \Program Files\Microsoft

Office\... folder to my account, I can't see how it can possibly be a

permissions issue.

I've also investigated and discarded the possibility that the Normal.dot

file is corrupt, as is suggested in other Google results on the same error.

So what else can there be?

Thanks.

Ken Dibble

www.stic-cil.org

_______________________________________________

Post Messages to: ProFox@leafe.com

Subscription Maintenance: http://leafe.com/mailman/listinfo/profox

OT-free version of this list: http://leafe.com/mailman/listinfo/profoxtech

Searchable Archive: http://leafe.com/archives/search/profox

This message: http://leafe.com/archives/byMID/profox/5.2.1.1.1.20100602235639.01f9a918@pop3.frontiernet.net

** All postings, unless explicitly stated otherwise, are the opinions of the author, and do not constitute legal or medical advice. This statement is added to the messages for those lawyers who are too stupid to see the obvious.

©2010 Ken Dibble
Back to top
RE: Word Automation: OLE exception error 5981 in Windows 7

Author: Richard Kaye

Posted: 2010-06-03 08:27:46   Link

What happens if you use a specific document reference in the Add call?

rk

-----Original Message-----

From: profoxtech-bounces@leafe.com [mailto:profoxtech-bounces@leafe.com] On Behalf Of Ken Dibble

Sent: Thursday, June 03, 2010 12:03 AM

To: profoxtech@leafe.com

Subject: RE: Word Automation: OLE exception error 5981 in Windows 7

Thanks Bill. I had already seen that early on. I did try some of the

suggestions in that article, and they didn't work. I don't know how to

"programmatically" load a different registry hive or start word as a

different user. However, since I'm already doing everything with the same

Domain Administrator account I was running when I installed Office, and

beyond that I've explicitly granted every possible action permission to MS

Office, and "Full Control" permission for the \Program Files\Microsoft

Office\... folder to my account, I can't see how it can possibly be a

permissions issue.

I've also investigated and discarded the possibility that the Normal.dot

file is corrupt, as is suggested in other Google results on the same error.

So what else can there be?

Thanks.

Ken Dibble

www.stic-cil.org

_______________________________________________

Post Messages to: ProFox@leafe.com

Subscription Maintenance: http://leafe.com/mailman/listinfo/profox

OT-free version of this list: http://leafe.com/mailman/listinfo/profoxtech

Searchable Archive: http://leafe.com/archives/search/profox

This message: http://leafe.com/archives/byMID/profox/DF1EEF11E586A64FB54A97F22A8BD04406842F0922@ACKBWDDQH1.artfact.local

** All postings, unless explicitly stated otherwise, are the opinions of the author, and do not constitute legal or medical advice. This statement is added to the messages for those lawyers who are too stupid to see the obvious.

©2010 Richard Kaye
Back to top
RE: Word Automation: OLE exception error 5981 in Windows 7

Author: Ken Dibble

Posted: 2010-06-03 16:01:18   Link

>What happens if you use a specific document reference in the Add call?

Interesting you should ask.

This old code I'm using would create the new document and get a file handle

for it with FCREATE(), then it would leave the file open to "lock" it to

"avoid surprises". This worked fine in Windows 2000 with VFP 9 SP 1.

Here are my test results with VFP 9 SP 1 in Windows 7:

* In my tests there are no spaces in path or file names, BTW.

cFile = "C:\SomePath\SomeFileThatDoesNotExist.doc"

Test 1:

nHandle = FCREATE(cFile) && 0 byte file created with specified path\name

WITH oMyWord

.documents.Add(cFile) && "Word could not open the document" ;

No temp file created

ENDWITH

Test 2:

* No FCREATE(), no file created

WITH oMyWord

.documents.Add(cFile) && "The document name or path is not valid" ;

No temp file created

ENDWITH

Test 3:

nHandle = FCREATE(cFile) && 0 byte file created with specified path\name

FCLOSE(nHandle)

WITH oMyWord

.documents.Add(cFile) && "Could not open macro storage" ;

4 kb temp Word file ("~$" prepended) with specified path\name

ENDWITH

Test 4:

* Original code that worked in Windows 2000 (Yes, the original call was not

a function call; no parentheses)

nHandle = FCREATE(cFile) && 0 byte file created with specified path\name

WITH oMyWord

.documents.Add && "Could not open macro storage" ;

No temp file created

ENDWITH

Test 5:

* No FCREATE(), no file name && No file created

WITH oMyWord

.documents.Add && "Could not open macro storage" ;

No temp file created

ENDWITH

Test 6:

nHandle = FCREATE(cFile) && 0 byte file created with specified path\name

FCLOSE(nHandle)

WITH oMyWord

.documents.Add && "Could not open macro storage" ;

No temp file created

ENDWITH

In each of these tests, Word created a 1 kb temp file called ~$Normal.dot

in the properly writeable location I've specified in the Word settings for

template files:

C:\Users\kend\My Documents\Office Templates\

Then the error occurs and Word hangs and must be killed in the Task Manager.

After each test I killed Word and deleted any orphaned temp files as well

as any 0 byte word files, and I shut down and restarted VFP.

So, Word at least knows where the template file should be. I assume that

Word actually opens a temp copy of Normal.dot in memory but then cannot do

anything with it before the error occurs.

Also, in the test where the file was initially created, then closed, and

then documents.add(cFile) was called, an actual temporary Word file was

created before the error occurred. There are additional parameters for

documents.Add(), including a template file name, a logical variable for

whether to use a "new template", and an integer variable for, I believe,

"visible". However, when I try:

documents.add(cFile,cTemplatePathandName,.F.,0)

I get a Word (not VFP) "syntax error".

Any suggestions?

Thanks very much for your help, Richard.

Ken Dibble

www.stic-cil.org

_______________________________________________

Post Messages to: ProFox@leafe.com

Subscription Maintenance: http://leafe.com/mailman/listinfo/profox

OT-free version of this list: http://leafe.com/mailman/listinfo/profoxtech

Searchable Archive: http://leafe.com/archives/search/profox

This message: http://leafe.com/archives/byMID/profox/5.2.1.1.1.20100603143533.01c13e70@pop3.frontiernet.net

** All postings, unless explicitly stated otherwise, are the opinions of the author, and do not constitute legal or medical advice. This statement is added to the messages for those lawyers who are too stupid to see the obvious.

©2010 Ken Dibble
Back to top
RE: Word Automation: OLE exception error 5981 in Windows 7

Author: Bill Arnold

Posted: 2010-06-03 17:30:45   Link

Ken,

I realize you've said it worked before using FCREATE, but just wondering if

you ran any tests with an existing DOC file instead of creating a 0 byte

new/empty file each time - just to see if it has any effect.

Bill

_______________________________________________

Post Messages to: ProFox@leafe.com

Subscription Maintenance: http://leafe.com/mailman/listinfo/profox

OT-free version of this list: http://leafe.com/mailman/listinfo/profoxtech

Searchable Archive: http://leafe.com/archives/search/profox

This message: http://leafe.com/archives/byMID/profox/03FD834A9BB7410CBBAC3FBE0439B254@bills

** All postings, unless explicitly stated otherwise, are the opinions of the author, and do not constitute legal or medical advice. This statement is added to the messages for those lawyers who are too stupid to see the obvious.

©2010 Bill Arnold
Back to top
RE: Word Automation: OLE exception error 5981 in Windows 7

Author: Ken Dibble

Posted: 2010-06-04 11:49:40   Link

>Ken,

>

>I realize you've said it worked before using FCREATE, but just wondering if

>you ran any tests with an existing DOC file instead of creating a 0 byte

>new/empty file each time - just to see if it has any effect.

>

>Bill

If I create and save an "empty" Word document (containing a single blank

space) and shut down Word, I get a 19 kb .DOC file. (Testing with a larger

file that contains text produces identical results, BTW.)

Then:

Test 1:

* No FCREATE(), no file name && No file created

WITH oMyWord

.documents.Add && "Could not open macro storage" ;

No temp file created

ENDWITH

Test 2:

* No FCREATE()

WITH oMyWord

.documents.Add(cFile) && "Could not open macro storage" ;

4 kb Word temp file created

ENDWITH

Test 3:

nHandle = FOPEN(cFile) && Pre-created Word file successfully opened

WITH oMyWord

.documents.Add && "Could not open macro storage" ;

No temp file created

ENDWITH

Test 4:

nHandle = FOPEN(cFile) && Pre-created Word file successfully opened

WITH oMyWord

.documents.Add(cFile) && "Could not open macro storage" ;

4 kb temp file created

ENDWITH

Again, in all cases, the temp Normal.dot file is created in the appropriate

location, and Word hangs at the error and must be killed in the Task Manager.

And I've just tested the original code with Windows XP SP 3. It fails at

the same point with a Word error "This is not a valid file name." As I

said, this was working fine in Windows 2000. Curiouser and Curiouser.

Any further thoughts?

Thanks!

Ken Dibble

www.stic-cil.org

_______________________________________________

Post Messages to: ProFox@leafe.com

Subscription Maintenance: http://leafe.com/mailman/listinfo/profox

OT-free version of this list: http://leafe.com/mailman/listinfo/profoxtech

Searchable Archive: http://leafe.com/archives/search/profox

This message: http://leafe.com/archives/byMID/profox/5.2.1.1.1.20100604111020.01bf79e8@pop3.frontiernet.net

** All postings, unless explicitly stated otherwise, are the opinions of the author, and do not constitute legal or medical advice. This statement is added to the messages for those lawyers who are too stupid to see the obvious.

©2010 Ken Dibble
Back to top
RE: Word Automation: OLE exception error 5981 in Windows 7

Author: Bill Arnold

Posted: 2010-06-04 13:14:37   Link

Ken,

I've kept a distance from Word automation after hearing of compatibility

headaches, but I couldn't resist in a few cases. Just took at look and I see

that in these cases I'm creating a new doc, and none involves a reference to

an existing file (new/empty or prev existing) for the operation.

FWIW, two examples follow below.

Bill

A small example

IF ISNULL(THISFORM.oWord)

THISFORM.oWord=CREATEOBJECT("Word.Application")

ENDIF

oDoc=THISFORM.oWord.Documents.Add()

THISFORM.simplECHART1.editCopy()

THISFORM.oWord.Selection.PasteSpecial(.F., .F., 0, .F., 3)

oDoc.PrintOut

oDoc.Close(0)

RELEASE oDoc

oDoc = .null.

A larger example, a prg that generates HTML doc for table descriptions using

Word

**********************************************************************

* Program/Method....: TABLEDOC.PRG

* Purpose....: The DBC Documentation Tool

* Uses WORD to generate an HTML doc file

* Uses the FIELDS table to obtain field descriptions

**********************************************************************

* based on: http://www.inquiry.com/techtips/vfox_pro/10min/10min0600.asp

* This tool creates a Microsoft Word file that documents a VFP database.

*!* When you run the code below, you are prompted to choose a DBC file

to

*!* document. After you choose a DBC file, it creates a Word file with

the

*!* same name but with the .doc extension. This file contains a Word

table

*!* for every VFP table in the database. The output looks like the

following:

PARAMETERS whattodoc

PUBLIC tzipname,tsgname,tzipfname

public targetf,tzipname,caliasb,ccurdir

targetf = "c:\vls\bldlib\cl\rec_formats"

tzipfname = "c:\vls\bldlib\cl\samplib\recformats.zip"

caliasb = ALIAS()

ccurdir = CURDIR()

q_ans = MESSAGEBOX("This procedure"+CHR(13)+CHR(13)+ ;

"1. Requires MS Word"+CHR(13)+ ;

"2. Can be restarted any time"+CHR(13)+ ;

"3. Creates record format HTML files for the input table"+CHR(13)+ ;

"4. Places HTML files -> C:\VLS\BLDLIB\CL\REC_FORMATS"+CHR(13)+ ;

"5. And ZIPPED -> C:\VLS\BLDLIB\CL\SAMPLIB\RECFORMATS.ZIP"+CHR(13)+ ;

"Proceed?",4,m_product)

IF q_ans <> 6

RETURN

ENDIF

=MESSAGEBOX("IMPORT the saved HTML file into the RoboHelp topic, ;

and then in Robohelp select TOOLS-> OPTIONS and select USE DEFAULT ;

EDITOR instead of AUTO SELECT so RoboHelp can work with this WORD ;

created file",0,m_product)

IF EMPTY(whattodoc)

whattodoc = ' '

ENDIF

DO case

CASE ATC('allprofs',whattodoc) <> 0

DO doc_all_profs_tables

CASE ATC('dbc',whattodoc) <> 0

DO docdbc WITH whattodoc

CASE ATC('dbf',whattodoc) <> 0

DO docdbf WITH whattodoc

OTHERWISE

DO docdbf WITH whattodoc

endcase

CD (ccurdir)

IF !EMPTY(caliasb)

SELECT (caliasb)

endif

release targetf,tzipname,caliasb,ccurdir

RETURN

*

---------------------------------------------------------------------------

* Document all DBFs in PROFS ... creating an Word generred HTML file

* with the structure and field descriptions for the DBF

*

---------------------------------------------------------------------------

procedure doc_all_profs_tables

LOCAL clnames

cd (z_profs) && get into source files PROFS folder

dimension clnames(500)

prof_files = adir(clnames,"*.dbf")

IF prof_files = 0

=MESSAGEBOX("No DBFs found in PROFS",0,m_product)

RETURN

ENDIF

* =asort(clnames,aelement(clnames,1,1),0,1) && sort into

descending order

* m_iver = clnames(1) && e.g. ZC010028.EXE

FOR n = 1 TO prof_files

DO docdbf WITH clnames(n,1)

IF z_esc

EXIT

endif

ENDFOR

* The HTML files are in the target folder

DO zipup && move result files into samplib ZIP file

STORE .f. TO z_esc

RELEASE clnames

RETURN

*

---------------------------------------------------------------------------

* Document a DBF ... create a word doc with the structure of the DBF

PROCEDURE docdbf

PARAMETERS cdbfname

#DEFINE wdToggle 9999998

#DEFINE wdCharacter 1

#DEFINE wdStory 6

#DEFINE wdMove 0

#DEFINE wdExtend 1

LOCAL lcDBCFile, loWord, loSelection, loDoc

LOCAL loTable, lnTable, lnTableCount

LOCAL ARRAY laTables[1], laFields[1]

lcDBFFile = cdbfname

IF !DIRECTORY(targetf)

MD (targetf)

ENDIF

IF EMPTY(lcdbFfile)

lcDBFFile = GETFILE("DBF", ;

"Select a DBF to Document", ;

"Select", ;

0,;

"Select a DBF to Document")

endif

IF EMPTY(lcDBFFile)

MESSAGEBOX("No Database Chosen")

RETURN

ENDIF

IF !FILE(lcdbffile)

=MESSAGEBOX(lcdbffile+" doesn't exist",0,m_product)

STORE .t. TO z_esc

RETURN

endif

*-- Fill an array with the names of

*-- the tables in the chosen DBC and

*-- then sort it by name

*!* OPEN DATABASE (lcDBCFile)

*!* lnTableCount = ADBOBJECTS(laTables,"TABLE")

*!* =ASORT(laTables,1)

loWord = CREATEOBJECT("Word.Application")

IF varTYPE(loWord) <> 'O'

=MESSAGEBOX("Unable to create an instance of Microsoft Word",0,m_product)

STORE .t. TO z_esc

RETURN

endif

loWord.Visible = .T.

loDoc = loWord.Documents.Add()

loSelection = loWord.Selection

loSelection.TypeText("Documentation for "+TRIM(lcDBFFile))

loSelection.TypeText(CHR(13))

loSelection.TypeText(CHR(13))

*!* FOR lnTable = 1 TO lnTableCount

*-- create a small header

loSelection.TypeText(CHR(13))

loSelection.TypeText("Table: ")

*-- Make the name of the table bold

*-- by toggling Bold on, typing the text,

*-- and then toggling Bold off

loSelection.Font.Bold = wdToggle

* loSelection.TypeText(laTables[lnTable])

* loSelection.TypeText(DBF())

loSelection.TypeText(cdbfname)

loSelection.Font.Bold = wdToggle

loSelection.TypeText(CHR(13))

loSelection.TypeText("Field Listing:")

loSelection.TypeText(CHR(13))

loSelection.TypeText(CHR(13))

loSelection.MoveLeft(wdCharacter,1)

*-- Open the table about to be

*-- documented and get its structural

*-- information

SELECT 0

USE (lcDBFFile) again

lnFieldCount = AFIELDS(laFields)

=ASORT(lafields)

*-- Create a Word table with the number of rows

*-- equal to the number of fields in the

*-- VFP table + 1. The extra row is for the

*-- column headers

* loTable = loDoc.Tables.Add(loSelection.Range,lnFieldCount+8,4)

loTable = loDoc.Tables.Add(loSelection.Range,lnFieldCount+1,5)

&& add 1 field for DESC

loTable.Cell(1,1).Range.Text = "Name"

loTable.Cell(1,2).Range.Text = "Description"

c2width = lotable.cell(1,2).width

lotable.cell(1,2).width = c2width * 2.2

loTable.Cell(1,3).Range.Text = "Type"

loTable.Cell(1,4).Range.Text = "Length"

loTable.Cell(1,5).Range.Text = "Dec"

* Fill in the rows of the Word table with field information

FOR lnField = 1 TO lnFieldCount

loTable.Cell(lnField+1,1).Range.Text = laFields[lnField,1]

loTable.Cell(lnField+1,2).Range.Text = getdesc(laFields[lnField,1])

&& get description

x3 = lotable.cell(lnField+1,2).width

lotable.cell(lnField+1,2).width = c2width * 2.2 && widen

desc column

loTable.Cell(lnField+1,3).Range.Text = laFields[lnField,2]

loTable.Cell(lnField+1,4).Range.Text =

ALLTRIM(STR(laFields[lnField,3]))

loTable.Cell(lnField+1,5).Range.Text =

ALLTRIM(STR(laFields[lnField,4]))

ENDFOR

* Add lines at the end of the table showing # fields, # records and today's

datetime

*!*

*!* lnfield = lnfield + 4

*!* lotable.cell(lnfield+1,1).range.text =

ALLTRIM(STR(FCOUNT(ALIAS())))+" fields"

*!* lnfield = lnfield + 1

*!* lotable.cell(lnfield+1,1).range.text =

ALLTRIM(STR(reccount(ALIAS())))+" records"

*!* lnfield = lnfield + 1

*!* lotable.cell(lnfield+1,1).range.text = 'As of '+TTOC(DATETIME())

*-- Jump to end of document

* The table is closed now. Add more stuff below the table

loSelection.MoveEnd(wdStory)

loSelection.Start = loSelection.End

loSelection.TypeText(CHR(13))

loSelection.TypeText(CHR(13))

loSelection.TypeText(ALLTRIM(STR(FCOUNT(ALIAS())))+" fields")

loSelection.TypeText(CHR(13))

loSelection.TypeText(ALLTRIM(STR(reccount(ALIAS())))+" records")

loSelection.TypeText(CHR(13))

loSelection.TypeText('As of '+TTOC(DATETIME()))

* Name .DOC file the same name as the DBF name, with an .HTM extension

* e.g. c:\vls\profs\html\comgrab.htm

lcsavedfile = ADDBS(targetf)+JUSTstem(lcdbffile)+'.htm'

* make sure file to be saved doesn't exist

IF FILE(lcsavedfile)

SET SAFETY OFF

ERASE (lcsavedfile)

SET SAFETY ON

ENDIF

* for SAVEAS doc, see http://msdn.microsoft.com/en-us/library/aa662158.aspx

loDoc.SaveAs(lcSavedFile,8) && for WORD 2000 -

can't use filtered html for some reason, but 8 (heavy html) works

* I had word 2002 on the gateway

machine, which works with 10 (smaller0

* loDoc.SaveAs(lcSavedFile,10) && 10 is filtered HTML, see

http://www.xefteri.com/articles/show.cfm?id=22

lodoc.Close

loWord.Quit()

lodoc = .null.

loword = .null.

USE

RETURN

* ----------------------------------------------------------------------

FUNCTION getdesc

PARAMETERS fieldn

LOCAL caliasb

caliasb = ALIAS()

IF !USED('theflds')

DO connect WITH 'fields'

ENDIF

IF z_esc

STORE .f. TO z_esc

RETURN '??? FIELDS table not available'

ENDIF

SELECT theflds

LOCATE FOR UPPER(TRIM(name)) == UPPER(TRIM(fieldn)) .and. !EMPTY(desc)

* .and. UPPER(TRIM(

LOCATE FOR UPPER(TRIM(name)) == UPPER(TRIM(fieldn)) .and. !EMPTY(desc)

IF FOUND()

SELECT (caliasb)

RETURN TRIM(theflds.desc)

ELSE

SELECT (caliasb)

RETURN ' ... '

ENDIF

SELECT (caliasb)

RETURN '???'

* -----------------------------------------------

PROCEDURE zipup

* Change below code to use right folders

* put result in BLDLIB\CL\SAMPLIB\xxxxxx.zip

CD (targetf) && get into target folder holding

CSV files

IF FILE(tzipfname)

SET safety OFF

ERASE (tzipfname)

SET SAFETY ON

ENDIF

* ZIP targetf "c:\vls\bldlib\cl\samplib\sampcsvs\*.csv"

* and

* move

* into tzipname "c:\vls\bldlib\cl\samplib\sampcsvs.zip"

go_lmutils.zip_using_fll(targetf,tzipfname) && zip

* Erase the CSV's and the work folder

CD (targetf)

SET safety OFF

ERASE *.htm

SET SAFETY ON

CD "c:\vls\bldlib\cl"

RD (targetf)

return

*

---------------------------------------------------------------------------

* Document a DBC ... create a word doc with the structure of

* each table in the DBC

*

----------------------------------------------------------------------------

PROCEDURE docdbc

PARAMETERS cdbcname

#DEFINE wdToggle 9999998

#DEFINE wdCharacter 1

#DEFINE wdStory 6

#DEFINE wdMove 0

#DEFINE wdExtend 1

LOCAL lcDBCFile, loWord, loSelection, loDoc

LOCAL loTable, lnTable, lnTableCount

LOCAL ARRAY laTables[1], laFields[1]

lcDBCFile = cdbcname

IF EMPTY(lcdbcfile)

lcDBCFile = GETFILE("DBC", ;

"Select a Database to Document", ;

"Select", ;

0,;

"Select a Database to Document")

endif

IF EMPTY(lcDBCFile)

MESSAGEBOX("No Database Chosen")

RETURN

ENDIF

*-- Fill an array with the names of

*-- the tables in the chosen DBC and

*-- then sort it by name

OPEN DATABASE (lcDBCFile)

lnTableCount = ADBOBJECTS(laTables,"TABLE")

=ASORT(laTables,1)

loWord = CREATEOBJECT("Word.Application")

loWord.Visible = .T.

loDoc = loWord.Documents.Add()

loSelection = loWord.Selection

loSelection.TypeText("Documentation for "+lcDBCFile)

loSelection.TypeText(CHR(13))

loSelection.TypeText(CHR(13))

FOR lnTable = 1 TO lnTableCount

*-- create a small header

loSelection.TypeText(CHR(13))

loSelection.TypeText("Table: ")

*-- Make the name of the table bold

*-- by toggling Bold on, typing the text,

*-- and then toggling Bold off

loSelection.Font.Bold = wdToggle

loSelection.TypeText(laTables[lnTable])

loSelection.Font.Bold = wdToggle

loSelection.TypeText(CHR(13))

loSelection.TypeText("Field Listing:")

loSelection.TypeText(CHR(13))

loSelection.TypeText(CHR(13))

loSelection.MoveLeft(wdCharacter,1)

*-- Open the table about to be

*-- documented and get its structural

*-- information

USE (laTables[lnTable])

lnFieldCount = AFIELDS(laFields)

*-- Create a Word table with the number of rows

*-- equal to the number of fields in the

*-- VFP table + 1. The extra row is for the

*-- column headers

loTable = loDoc.Tables.Add(loSelection.Range,lnFieldCount+1,4)

loTable.Cell(1,1).Range.Text = "Name"

loTable.Cell(1,2).Range.Text = "Type"

loTable.Cell(1,3).Range.Text = "Length"

loTable.Cell(1,4).Range.Text = "Dec"

*-- Fill in the rows of the Word table with

*-- the field information

FOR lnField = 1 TO lnFieldCount

loTable.Cell(lnField+1,1).Range.Text = ;

laFields[lnField,1]

loTable.Cell(lnField+1,2).Range.Text = ;

laFields[lnField,2]

loTable.Cell(lnField+1,3).Range.Text = ;

ALLTRIM(STR(laFields[lnField,3]))

loTable.Cell(lnField+1,4).Range.Text = ;

ALLTRIM(STR(laFields[lnField,4]))

ENDFOR

*-- Jump to end of document

loSelection.MoveEnd(wdStory)

loSelection.Start = loSelection.End

ENDFOR

*-- Name .DOC file the same as the DBC name

*-- Remove .DBC from end of DBC name

lcSavedFile = SUBSTRC(lcDBCFile,1,LENC(lcDBCFile)-4)

loDoc.SaveAs(lcSavedFile,10)

loWord.Quit()

USE

CLOSE DATABASE

return

_______________________________________________

Post Messages to: ProFox@leafe.com

Subscription Maintenance: http://leafe.com/mailman/listinfo/profox

OT-free version of this list: http://leafe.com/mailman/listinfo/profoxtech

Searchable Archive: http://leafe.com/archives/search/profox

This message: http://leafe.com/archives/byMID/profox/D2199EFE190E406EAAAA1F4ACF4EB136@bills

** All postings, unless explicitly stated otherwise, are the opinions of the author, and do not constitute legal or medical advice. This statement is added to the messages for those lawyers who are too stupid to see the obvious.

©2010 Bill Arnold