Automation can be tricky at the best of times and what starts out as a straight forward task becomes a total nightmare. You spend hours converting office macro code to VFP only to come up with an error telling you that eg. "Variable XLUNDERLINE not found".
This is where header files come in. If you look in most of the VFP books that are out they will give you examples of VFP automating Microft Office products but when you copy and paste the code it doesn't work. Unfortunitly header files are not included in the office packages BUT as it happens there is away around this problem.
Microsoft Visual Studio users can create header files using the Object viewer but what if you only have VFP?
As I'm sure you are aware, every office product ships with a .olb file which stores all the specific commands in that program. The following code extracts all the useful info out of the files and saves them as a .h file. Then all you have to do is include the file in forms or programs whenever you are automating office proucts.
****************START CODE****************
PUBLIC oform1
oform1=NEWOBJECT("form1")
oform1.SHOW
RETURN
****************FORM CODE****************
DEFINE CLASS form1 AS FORM
HEIGHT = 445
WIDTH = 567
DOCREATE = .T.
AUTOCENTER = .T.
BORDERSTYLE = 1
CAPTION = ".OLB Constants Extractor"
MAXBUTTON = .F.
MINBUTTON = .F.
NAME = "Form1"
ADD OBJECT txtolbfile AS TEXTBOX WITH ;
HEIGHT = 27, ;
LEFT = 65, ;
READONLY = .T., ;
TABINDEX = 2, ;
TOP = 6, ;
WIDTH = 458, ;
NAME = "txtOLBFILE"
ADD OBJECT label1 AS LABEL WITH ;
AUTOSIZE = .T., ;
CAPTION = ".\<OLB File:", ;
HEIGHT = 17, ;
LEFT = 4, ;
TOP = 11, ;
WIDTH = 55, ;
TABINDEX = 1, ;
NAME = "Label1"
ADD OBJECT cmdsave AS COMMANDBUTTON WITH ;
TOP = 411, ;
LEFT = 394, ;
HEIGHT = 27, ;
WIDTH = 84, ;
CAPTION = "\<Save to .h", ;
ENABLED = .F., ;
TABINDEX = 6, ;
NAME = "cmdSAVE"
ADD OBJECT cmdquit AS COMMANDBUTTON WITH ;
TOP = 411, ;
LEFT = 480, ;
HEIGHT = 27, ;
WIDTH = 84, ;
CAPTION = "\<Quit", ;
TABINDEX = 7, ;
NAME = "cmdQUIT"
ADD OBJECT edtconstants AS EDITBOX WITH ;
HEIGHT = 347, ;
LEFT = 6, ;
READONLY = .T., ;
TABINDEX = 4, ;
TOP = 52, ;
WIDTH = 558, ;
NAME = "edtConstants"
ADD OBJECT cmdgetfile AS COMMANDBUTTON WITH ;
TOP = 6, ;
LEFT = 533, ;
HEIGHT = 27, ;
WIDTH = 26, ;
CAPTION = "...", ;
TABINDEX = 3, ;
NAME = "cmdGETFILE"
ADD OBJECT cmdextract AS COMMANDBUTTON WITH ;
TOP = 411, ;
LEFT = 280, ;
HEIGHT = 27, ;
WIDTH = 110, ;
CAPTION = "\<Extract Constants", ;
ENABLED = .F., ;
TABINDEX = 5, ;
NAME = "cmdEXTRACT"
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 + "* " + 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****************
Just copy this code into a program and run it.
Hope this helps with stress levels
Chris