Microsoft KB Archive/285396

= How to obtain Microsoft Office constants from .olb files with Visual FoxPro =

Article ID: 285396

Article Last Modified on 2/16/2005

-

APPLIES TO


 * Microsoft Visual FoxPro 6.0 Professional Edition
 * Microsoft Visual FoxPro 7.0 Professional Edition
 * Microsoft Visual FoxPro 8.0 Professional Edition
 * Microsoft Visual FoxPro 9.0 Professional Edition

-



This article was previously published under Q285396



SUMMARY
Automation of Microsoft Office products from Visual FoxPro (VFP) is a popular undertaking for VFP developers. Most often, VFP developers approach this task by converting macros from the product to be automated or by translating examples written for Microsoft Visual Basic. Regardless of the approach, the VFP developer must often convert Office constants to their literal values before these constants can be used from VFP. Depending on the amount of code, this can be a tedious undertaking.



MORE INFORMATION
The following sample code helps VFP developers translate Office constants. It provides a method to extract the built-in constants from Office object library (.olb) files, and then allows the developer to save them to a header (.h) file for inclusion in a VFP project.

For example, a VFP developer writing a program to automate Microsoft Word can use this code to extract all of the Word constants to a header file. The header file can be included in the Word automation program using the #INCLUDE statement, allowing the developer to use Word constants &quot;as is&quot; in the VFP code (for example, the developer can use wdFindContinue instead of its literal value of 1).

To use this sample code, follow these steps:
 * 1) Paste the code below in a new .prg file, and then save and run the program. A form with the title &quot;.OLB Constants Extractor&quot; appears.
 * 2) Click the ellipsis (...) button to locate an .olb file in your Office folder, and then click Extract Constants. The form extracts all the constants from the .olb file you selected and writes them to the edit box.
 * 3) You can then review the output or save the results to a header file for use in your code. To do this, click Save to .h.

Office constants are available from the following files:

The names of the .olb files vary depending on your version of Microsoft Office.

Note This sample code uses objects that are included in the Tlbinf32.dll file. This dynamic-link library (.dll) file ships with Microsoft Visual Studio 98 Service Pack 4. If this file is not present and properly registered on your computer, the sample code below will fail. ****************START CODE**************** PUBLIC oform1

oform1=NEWOBJECT(&quot;form1&quot;) oform1.SHOW RETURN

DEFINE CLASS form1 AS FORM
 * FORM CODE****************

HEIGHT = 445 WIDTH = 567 DOCREATE = .T.   AUTOCENTER = .T.    BORDERSTYLE = 1 CAPTION = &quot;.OLB Constants Extractor&quot; MAXBUTTON = .F.   MINBUTTON = .F.    NAME = &quot;Form1&quot;

ADD OBJECT txtolbfile AS TEXTBOX WITH ; HEIGHT = 27, ; LEFT = 65, ; READONLY = .T., ; TABINDEX = 2, ; TOP = 6, ; WIDTH = 458, ; NAME = &quot;txtOLBFILE&quot;

ADD OBJECT label1 AS LABEL WITH ; AUTOSIZE = .T., ; CAPTION = &quot;.\<OLB File:&quot;, ; HEIGHT = 17, ; LEFT = 4, ; TOP = 11, ; WIDTH = 55, ; TABINDEX = 1, ; NAME = &quot;Label1&quot;

ADD OBJECT cmdsave AS COMMANDBUTTON WITH ; TOP = 411, ; LEFT = 394, ; HEIGHT = 27, ; WIDTH = 84, ; CAPTION = &quot;\<Save to .h&quot;, ; ENABLED = .F., ; TABINDEX = 6, ; NAME = &quot;cmdSAVE&quot;

ADD OBJECT cmdquit AS COMMANDBUTTON WITH ; TOP = 411, ; LEFT = 480, ; HEIGHT = 27, ; WIDTH = 84, ; CAPTION = &quot;\<Quit&quot;, ; TABINDEX = 7, ; NAME = &quot;cmdQUIT&quot;

ADD OBJECT edtconstants AS EDITBOX WITH ; HEIGHT = 347, ; LEFT = 6, ; READONLY = .T., ; TABINDEX = 4, ; TOP = 52, ; WIDTH = 558, ; NAME = &quot;edtConstants&quot;

ADD OBJECT cmdgetfile AS COMMANDBUTTON WITH ; TOP = 6, ; LEFT = 533, ; HEIGHT = 27, ; WIDTH = 26, ; CAPTION = &quot;...&quot;, ; TABINDEX = 3, ; NAME = &quot;cmdGETFILE&quot;

ADD OBJECT cmdextract AS COMMANDBUTTON WITH ; TOP = 411, ; LEFT = 280, ; HEIGHT = 27, ; WIDTH = 110, ; CAPTION = &quot;\<Extract Constants&quot;, ; ENABLED = .F., ; TABINDEX = 5, ; NAME = &quot;cmdEXTRACT&quot;

PROCEDURE cmdsave.CLICK STRTOFILE(THISFORM.edtconstants.VALUE,PUTFILE([Header File], ; JUSTSTEM(THISFORM.txtolbfile.VALUE) + [.h],[.h])) ENDPROC

PROCEDURE cmdquit.CLICK THISFORM.RELEASE ENDPROC

PROCEDURE cmdgetfile.CLICK LOCAL lcOLBFile

lcOLBFile = GETFILE([OLB],[OLB File],[Open]) IF EMPTY(lcOLBFile) RETURN .F.       ENDIF IF UPPER(RIGHT(lcOLBFile,3)) # [OLB] MESSAGEBOX([Invalid File],0,[]) RETURN .F.       ENDIF

THISFORM.txtolbfile.VALUE = lcOLBFile THISFORM.cmdextract.ENABLED= .T.   ENDPROC

PROCEDURE cmdextract.CLICK WAIT WINDOW [Processing...] NOCLEAR NOWAIT LOCAL oTLB_INFO, oConstants, lcConstantsStr, Obj, member #DEFINE CRLF CHR(13) + CHR(10)

oTLB_INFO = CREATEOBJECT([tli.typelibinfo]) oTLB_INFO.ContainingFile = (THISFORM.txtolbfile.VALUE)

oConstants = oTLB_INFO.Constants

lcConstantsStr = [] FOR EACH Obj IN oTLB_INFO.Constants lcConstantsStr = lcConstantsStr + CRLF + &quot;* &quot; + Obj.Name + CRLF FOR EACH member IN Obj.Members lcConstantsStr = lcConstantsStr + [#DEFINE ] + ; member.NAME + [ ] + ; TRANSFORM(member.VALUE) + CRLF NEXT member NEXT Obj

THISFORM.edtconstants.VALUE=lcConstantsStr THISFORM.cmdsave.ENABLED= .T.       WAIT CLEAR WAIT WINDOW [Complete!] TIMEOUT 2 ENDPROC

ENDDEFINE
 * END CODE****************

