Wednesday, March 22, 2006

When Constants, aren’t. . .

Thanks to a comment from Rick, I decided to publish my utility for extracting the ‘constants’ from an OLB file. That’s an “Office Object Library” file and each Office application (and they can be different for different versions) has one. This utility was taken from Microsoft’s knowledgebase, commented and adjusted slightly for my needs and formatted with my 'Prg2Html' utility for posting here.

Note: This code utilizes 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 the file is not present and properly registered on your computer, the code below will fail.

So without further talk… here’s the code! I hope you find it as useful as I do.

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



*--------------------------------------------------*
* Program: bldConstant *
* Purpose: Load an OLB file and extract the *
* Constant definitions *
* Parameters: None *
* Returns: Nothing *
*--------------------------------------------------*
*-//- Note: *
* It places the resulting constant file in the *
* location specified by the user *
*--------------------------------------------------*
PUBLIC oGetConstant

oGetConstant=NEWOBJECT("GetConstant")
oGetConstant.SHOW

RETURN


*--Define the Form and its objects
DEFINE CLASS GetConstant AS FORM

HEIGHT = 550
WIDTH = 573
DOCREATE = .T.
AUTOCENTER = .T.
BORDERSTYLE = 1
CAPTION = ".OLB Constant Define Extractor"
MAXBUTTON = .F.
MINBUTTON = .F.
NAME = "GetConstant"

*--TextBox for the OLB file and path
ADD OBJECT txtOlbFileName AS ;
TEXTBOX WITH ;
HEIGHT = 27, ;
LEFT = 65, ;
READONLY = .T., ;
TABINDEX = 2, ;
TOP = 6, ;
WIDTH = 458, ;
fontname = "Courier New", ;
FontSize = 9, ;
NAME = "txtOlbFileName"

*--Label for the FileName
ADD OBJECT lblFileName AS ;
LABEL WITH ;
AUTOSIZE = .T., ;
CAPTION = ".\<OLB File:", ;
HEIGHT = 17, ;
LEFT = 4, ;
TOP = 11, ;
WIDTH = 55, ;
TABINDEX = 1, ;
NAME = "lblFileName"

*--Button to find/locate the file
ADD OBJECT cmdFindFile AS ;
COMMANDBUTTON WITH ;
TOP = 6, ;
LEFT = 533, ;
HEIGHT = 27, ;
WIDTH = 26, ;
CAPTION = "...", ;
TABINDEX = 3, ;
NAME = "cmdFindFile"

*--Editbox to hold the extracted constants
ADD OBJECT edbShowConstants AS ;
EDITBOX WITH ;
HEIGHT = 473, ;
LEFT = 6, ;
READONLY = .T., ;
TABINDEX = 4, ;
TOP = 42, ;
WIDTH = 563, ;
FONTNAME = "Courier New", ;
FONTSIZE = 9, ;
NAME = "edbShowConstants"

*--Button to Extract the Constant definitions
ADD OBJECT cmdLoadConstants AS ;
COMMANDBUTTON WITH ;
TOP = 520, ;
LEFT = 288, ;
HEIGHT = 27, ;
WIDTH = 112, ;
CAPTION = "\<Extract Constants", ;
ENABLED = .F., ;
TABINDEX = 5, ;
NAME = "cmdLoadConstants"

*--Button to save the generated file
ADD OBJECT cmdSave2Hfile AS ;
COMMANDBUTTON WITH ;
TOP = 520, ;
LEFT = 400, ;
HEIGHT = 27, ;
WIDTH = 84, ;
CAPTION = "\<Save to .h", ;
ENABLED = .F., ;
TABINDEX = 6, ;
NAME = "cmdSave2Hfile"

*--Button to Exit the Form
ADD OBJECT cmdExitForm AS ;
COMMANDBUTTON WITH ;
TOP = 520, ;
LEFT = 484, ;
HEIGHT = 27, ;
WIDTH = 84, ;
CAPTION = "\<Quit", ;
TABINDEX = 7, ;
NAME = "cmdExitForm"

*--------------------------------------*
* -- Local Procedures and Functions -- *
*--------------------------------------*
PROCEDURE cmdFindFile.CLICK
*--Find the OLB file to Process
LOCAL cOlbFileName, lRetVal
lRetVal = .F.

cOlbFileName = GETFILE("OLB","OLB File","Open")
IF !EMPTY(cOlbFileName)
IF UPPER(RIGHT(cOlbFileName,3)) = "OLB"
lRetVal = .T.
THISFORM.txtOlbFileName.VALUE = cOlbFileName
*--Now that we have a filename,
* enable the Load Button
THISFORM.cmdLoadConstants.ENABLED= .T.
ELSE
MESSAGEBOX(cOlbFileName + ;
" Is not an OLB file!",0)
ENDIF
ENDIF
RETURN (lRetVal)
ENDPROC

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

PROCEDURE cmdLoadConstants.CLICK
*--Load constants from the OLB into the editbox
WAIT WINDOW "Processing..." NOCLEAR NOWAIT
LOCAL cTypeLibInfo, oConstants, ;
cConstantStr, Obj, MEMBER
#DEFINE CRLF CHR(13) + CHR(10)

cTypeLibInfo = CREATEOBJECT("tli.typelibinfo")
cTypeLibInfo.ContainingFile = ;
(THISFORM.txtOlbFileName.VALUE)
oConstants = cTypeLibInfo.Constants

cConstantStr = ""
FOR EACH Obj IN cTypeLibInfo.Constants
cConstantStr = cConstantStr + ;
CRLF + "* " + Obj.NAME + CRLF
FOR EACH MEMBER IN Obj.Members
cConstantStr = cConstantStr + ;
"#DEFINE " + MEMBER.NAME + " " + ;
TRANSFORM(MEMBER.VALUE) + CRLF
NEXT MEMBER
NEXT Obj

THISFORM.edbShowConstants.VALUE=cConstantStr
THISFORM.cmdSave2Hfile.ENABLED= .T.
WAIT CLEAR
MESSAGEBOX("Done. . .",0,"Extract Constants")
RETURN

ENDPROC

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

PROCEDURE cmdSave2Hfile.CLICK
*--Save the Constants to the file specified by
* the user, with a ".H" extension
STRTOFILE(THISFORM.edbShowConstants.VALUE, ;
PUTFILE("Header File", ;
JUSTSTEM(THISFORM.txtOlbFileName.VALUE) + ;
".h",".h"))
RETURN

ENDPROC

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

PROCEDURE cmdExitForm.CLICK
*--Exit the form
THISFORM.RELEASE
ENDPROC

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


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


So there you have it... Over the next few days, I'll be posting what I've been working on for the next step of the career journey, as well as some VB/VFP code translations for those of you who might be interested in what they look like.

The ability to 'drive' Microsoft Office, from within other applications, like Visual FoxPro, allows you to accomplish some very professional looking reports, spreadsheets, charts and graphs. Not to mention you can choose to 'show' your work, as in let the office application be visible, or, place it behind the scenes, and just show off your finished results. If you're not working on it yet, you should at least give it a try.

As always, thanks for stopping by!!

Technorati Tags: - - -
-IceRocket Tags: - - -

3 comments:

Beth said...

It's all Greek to me. Oh, how I wish I could speak the language of the computer. =)

No_Newz said...

Oh sure, that's all I gotta do?! :P You know I am an airhead about this code stuff.
Where is part two??? Still waiting. Is blog mad like blog explosion? I don't think I could fit one more blog into my reading as it is.
Have a great weekend!
Lois Lane

Bill said...

Beth - If you only knew how much I wish (some days) that I make a living doing something else!!

Lois - Just posted the next part... Sorry about the code intrusion... just couldn't help myself!

Yep.. BlogMad is essentially hte same as BlogExplosion!

I hear ya about finding time to read... it's been in short supply for me as well!