Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-01-2017, 09:16 AM
ilcaa72 ilcaa72 is offline Turn off an Initialized Class Windows 7 64bit Turn off an Initialized Class Office 2013
Novice
Turn off an Initialized Class
 
Join Date: Jan 2014
Posts: 27
ilcaa72 is on a distinguished road
Default Turn off an Initialized Class

hello, i have an this sub that initializes a Class event that runs code on any change of the selection. I wanted to assign a Macro button to "turn off" the event.



Whats the proper VBA to 'de-initialize' the event? thanks

Code:
Sub LoadMonitor()
    Set selMonitor = New Class1
    MsgBox "Selection Change On"
End Sub


'stored in Class Module
Option Explicit
Private WithEvents mWordApp As Word.Application

Private Sub Class_Initialize()
    Set mWordApp = Word.Application
End Sub

Private Sub mWordApp_WindowSelectionChange(ByVal Sel As Selection)
'code here for selection change
Reply With Quote
  #2  
Old 05-01-2017, 04:03 PM
macropod's Avatar
macropod macropod is offline Turn off an Initialized Class Windows 7 64bit Turn off an Initialized Class Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by ilcaa72 View Post
hello, i have an this sub that initializes a Class event that runs code on any change of the selection. I wanted to assign a Macro button to "turn off" the event.

Whats the proper VBA to 'de-initialize' the event?
There is no facility to do that. Either put your event code into a template that applies only to documents based on that template - so it doesn't affect documents based on other templates - or add some error trapping to ensure the event exits unless the document or selection conforms to certain specifications.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 05-01-2017, 06:43 PM
ilcaa72 ilcaa72 is offline Turn off an Initialized Class Windows 7 64bit Turn off an Initialized Class Office 2013
Novice
Turn off an Initialized Class
 
Join Date: Jan 2014
Posts: 27
ilcaa72 is on a distinguished road
Default

Thanks Paul, I Initialize this Selection event with a macro button, and based on what I select the code provides me various options to execute on certain sentences. The issue isnt about not having it run in other docs, because I can control when it is turned on. The issue is once it is turn on
Code:
Private Sub mWordApp_WindowSelectionChange(ByVal Sel As Selection)
I would like to turn it off so when i select it doesnt execute the code anymore while i am still in the document.

Right now i have to go into VBE and press the stop button on the debugger. I would like to add another button in my toolbar which I was hoping would "unload" or "terminate" the selection Event.

Am i stuck with going into VBE to turn this Event WindowSelectionChange manually off everytime i dont want it effecting my selection?

thanks again
Reply With Quote
  #4  
Old 05-01-2017, 07:13 PM
macropod's Avatar
macropod macropod is offline Turn off an Initialized Class Windows 7 64bit Turn off an Initialized Class Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

OK, but you could have some additional code to conditionally enable/disable the WindowSelectionChange event. For example, in your normal code module:
Code:
Public bMon As Boolean

Sub LoadMonitor()
    Set selMonitor = New Class1
    Call EnableMonitor
End Sub

Sub EnableMonitor()
    bMon = True
    MsgBox "Selection Change On"
End Sub

Sub DisableLoadMonitor()
    bMon = False
    MsgBox "Selection Change Off"
End Sub
This both initiates and enables the WindowSelectionChange event and allows you to conditionally enable/disable it.

Then all you need do is add a single line to the WindowSelectionChange sub:
Code:
Private Sub mWordApp_WindowSelectionChange(ByVal Sel As Selection)
If bMon = False Then Exit Sub
'code here for selection change
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Turn off an Initialized Class I would like a function to add sequential numbers to a class number/name Highlander01 Excel 5 01-22-2016 02:29 PM
Is it possible to bind macros to keys not in the KeyCodeConstants class? AlexR Word VBA 4 04-08-2013 10:15 AM
Evernote--Class Notes markg2 Outlook 0 05-10-2012 05:50 PM
Looking for easy and quick way to draw on class notes "x-rays - image" Lacrosseboss18 PowerPoint 0 02-04-2011 01:21 PM
Want to search for "class", replacing with document property YetAnotherAuthor Word 0 10-30-2009 09:43 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:20 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft