Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I have to make code applicable to the workbook.
Like workbook_beforeprint code. Now, after i have made the code work well, i want to save the workbook as an addin so that the code that i have written will be applicable to all the workbook that will be open after the addin is loaded. Is there any tips for that Thanks in advance. |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
On Apr 22, 1:47*pm, Subodh wrote:
I have to make code applicable to the workbook. Like workbook_beforeprint code. Now, after i have made the code work well, *i want to save the workbook as an addin so that the code that i have written will be applicable to all the workbook that will be open after the addin is loaded. Is there any tips for that Thanks in advance. You can use application level event application_workbookbeforeprint(Wb As Workbook, Cancel As Boolean).For this you have to use class mdule.Can get the details from Microsoft Site.If you need I can give the link after a while. |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
On Apr 22, 2:11*pm, Javed wrote:
On Apr 22, 1:47*pm, Subodh wrote: I have to make code applicable to the workbook. Like workbook_beforeprint code. Now, after i have made the code work well, *i want to save the workbook as an addin so that the code that i have written will be applicable to all the workbook that will be open after the addin is loaded. Is there any tips for that Thanks in advance. You can use application level event application_workbookbeforeprint(Wb As Workbook, Cancel As Boolean).For this you have to use class mdule.Can get the details from Microsoft Site.If you need I can give the link after a while. The link is http://www.tek-tips.com/faqs.cfm?fid=4195 |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
On Apr 22, 2:26*pm, Javed wrote:
On Apr 22, 2:11*pm, Javed wrote: On Apr 22, 1:47*pm, Subodh wrote: I have to make code applicable to the workbook. Like workbook_beforeprint code. Now, after i have made the code work well, *i want to save the workbook as an addin so that the code that i have written will be applicable to all the workbook that will be open after the addin is loaded. Is there any tips for that Thanks in advance. You can use application level event application_workbookbeforeprint(Wb As Workbook, Cancel As Boolean).For this you have to use class mdule.Can get the details from Microsoft Site.If you need I can give the link after a while. The link ishttp://www.tek-tips.com/faqs.cfm?fid=4195 Thanks Javed. I tried the link But it didn't worked. I am using excel 2007 and XP This is what i did Made a new microsoft excel file (Macro Enabled) Inserted a class module Pasted following code in the class module Public WithEvents xlapp as Application Public Sub xlapp_WorkbookOpen(ByVal Wb As Workbook) MsgBox " Testing Trap Events! " End Sub Named the class module as ClsAppEvents (Don't know if it was needed as i think these names don't matter) Again, I made a new module file (standard) renamed it as TrapAppEvents and typed the following code in it Public xlApplication As New ClsAppEvents Public Sub TrapApplicationEvents() Set xlApplication.xlapp = Application End Sub Saved it and kept it open Now,I expected that if i open new excel files then the message should trigger.. But that was not the case. Do i need some more thing to do? Anything in the Thisworkbook Object Or, Did i missed something. Plz Help. |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I'm not a master of event programming but a couple of considerations:
(1) I'm not sure if you'd be better off declaring 'as new'. Unless I missed something I'd declare it: Public xlApplication As ClsAppEvent i.e., without the NEW in it and then create the object in the code. As far as I know it's not the best practice to declare as New as otherwise you've got no way of testing if the variable has been instantiated although for this particular purpose the NEW could be good enough. (2) I think that your problem could be in firing this code of yours: Public Sub TrapApplicationEvents() Set xlApplication.xlapp = Application End Sub i.e., this code must be run before all the event trapping can happen - if this code hasn't run you don't have your xlApplication variable created yet and because of that it obviously can't trigger any events. So, I think that you don't run the code in the beginning. To test it - just run the code separately - in VBE locate this code and run it (F5) - it would create the xlApplication variable and from then on it should trap the events BUT bear in mind that this variable will cease to exist if you edit the code or close the file so what you need to do is to make sure that this code runs before you want to start trapping the events - so, perhaps you could put it in the Workbook_Open event of the xla file something like this: Private Sub Workbook_Open() Set xlApplication.xlapp = Application End Sub This would be then in the ThisWorkbook module of the xla file - this way whenever the xla opens it creates your variable for event trapping and it should be fine now. |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
On Apr 24, 2:06*pm, AB wrote:
I'm not a master of event programming but a couple of considerations: (1) I'm not sure if you'd be better off declaring 'as new'. Unless I missed something I'd declare it: Public xlApplication As ClsAppEvent i.e., without the NEW in it and then create the object in the code. As far as I know it's not the best practice to declare as New as otherwise you've got no way of testing if the variable has been instantiated although for this particular purpose the NEW could be good enough. (2) I think that your problem could be in firing this code of yours: Public Sub TrapApplicationEvents() * * *Set xlApplication.xlapp = Application End Sub i.e., this code must be run before all the event trapping can happen - if this code hasn't run you don't have your xlApplication variable created yet and because of that it obviously can't trigger any events. So, I think that you don't run the code in the beginning. To test it - just run the code separately - in VBE locate this code and run it (F5) - it would create the xlApplication variable and from then on it should trap the events BUT bear in mind that this variable will cease to exist if you edit the code or close the file so what you need to do is to make sure that this code runs before you want to start trapping the events - so, perhaps you could put it in the Workbook_Open event of the xla file something like this: Private Sub Workbook_Open() * * *Set xlApplication.xlapp = Application End Sub This would be then in the ThisWorkbook module of the xla file - this way whenever the xla opens it creates your variable for event trapping and it should be fine now. I am sorry I tried the second one also. ie. WIthout the new keyword But it didn't work Can't find out what the mistake was or anything i missed out. |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
how can we embed the code to the excel workbook from addin | Excel Programming | |||
can we copy the code from the addin to excel workbook | Excel Programming | |||
Get Workbook Object properties without opening Workbook? | Excel Programming | |||
Changing control properties/code through code | Excel Programming | |||
Locking VBA code via Project properties but cannot save the workbook over itself | Excel Programming |