Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Adding a macro to a module in Excel
Message
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00935206
Message ID:
00935325
Views:
19
clear
set safe off
oE = GETOBJECT(,'excel.application')

** create a table with the module names in it
** search for sub routines in the memo of each module macCode
? "build table of modules"
create table mac (mnum n(5) , mname c(40), mcode m(4))
sele mac
WITH oe.ActiveWorkBook.VbProject
    For i = 1 TO 	.VBComponents.count
	    mmname =   	.VBComponents[i].name
    	go bott
    	insert blank
    	go bott
    	repl mname with mmname
    	repl mnum with i

		macName =	.VBComponents[i].name
		macCode = 	.VBComponents[macName].codemodule.lines(1,100000)
		repl mcode with macCode
    EndFor
EndWith
go top
*brow

myMac = ""
** will be the name of your module
? "look for myCacroCode"
myMac = "MyMacroCode"
LOCATE FOR lower(myMac) ==  alltrim(lower(mName))

if found()
	myMacroNum = mNum
	myMacroName = ALLTRIM(mName)
	*brow title str(mNum)

? "delete"
	**removing a module
	with oe.ActiveWorkbook.VBProject
	   	mRemove = .vbcomponents(myMacroName)
		.vbcomponents.Remove(mRemove)
	EndWith
endif

? "add it"
** go on and add it now
myAdd = oe.ActiveWorkbook.VBProject.VBComponents.Add(1)
myAdd.Name = myMac

? "create th macro"
CREATE table mCode (cLine c (100) )
sele mCode

for i = 1 to  50
go bott
insert blank
endFor

go top
** sub begins with
repl cline with "sub hi()"
skip

*Msg = "Do you want to continue ?"    ' Define message.
*Style = vbYesNo + vbCritical + vbDefaultButton2    ' Define buttons.
*Title = "MsgBox Demonstration"    ' Define title.
*Help = "DEMO.HLP"    ' Define Help file.
*Ctxt = 1000    ' Define topic
*MsgBox(Msg, Style, Title, Help, Ctxt)

** sub code
repl cline with "msgBox" + chr(34) + "hi" + chr(34)
skip

** sub ends with
repl cline with "end sub "
*skip

copy to tt.txt type sdf


? "add the code"
oe.ActiveWorkbook.VBProject.VBComponents ;
    (MyMac) ;
     .CodeModule.AddFromFile("C:\page0\tt.TXT")

oe.visible = .t.
oe.Application.Run["hi"]
Previous
Reply
Map
View

Click here to load this message in the networking platform