jimlamb1944
07-16-2013, 08:41 PM
I had an event manager running under Windows XP/ Word 2003 or Word 2007 in a large VBA macro at one time for many months. Then some change was made by me in the code or an upgrade was made from Word 2003 to Word 2007 or from an earlier version of VBA to VBA 6.5. At that point the macro ran fine except for the event manager aspect. The program analyzes a Word document for certain numbers that appear in the document, then creates a new Word document and shows the results of the analysis in Word tables.
The event manager is a small amount of code that is supposed to detect a click of the mouse when the cursor is in a field of the table of the new document ("Reference List.docx") and highlight a corresponding point in the original document ("mySpec") in the main body of code. I have not been able to get the Event Manager to respond to the event and run the code (it never gets to the first line of the event handler code). I am looking for any clue as to why. This was the only event handler I wrote. I know that before I got it working, I had to study event handling a lot and play with variations. I used an article called "Taking Control of Microsoft Work through Events" Bill Coan 2003 as my reference. I have re-studied the article and tried variations, but to no avail. My ignorance on classes and defining them is great.
Here is the architecture for the event manager as I last left it several months ago:
MAIN PROGRAM - FIRST SEVERAL LINES:
'MODIFIED ACCORDING TO "TAKE CONTROL OF ...." by Coan
Dim objEventHandler As clsEventHandler
Dim NotElementNoStr(), NotElementNoStart(), NotElementNoEnd(), _
ElementNoStr(), ElementNoStart(), ElementNoEnd(), ElementNoPrecedingStr(), _
ElementNoGroupStr(), ElementNoGroupStart(), ElementNoGroupEnd(), ElementNoGroupPrecedingStr()
Dim NotElementNoSize, ElementNoSize, ElementNoGroupSize As Integer
Dim mySpecWindow As Window, myListWindow As Window, _
mySpec As Document, myList As Document, myRange As Range, LastAbstractStrRange As Range
NotElementNoSize = 0: ElementNoSize = 0: ElementNoGroupSize = 0
'Make the next line active, the one thereafter inactive to activate SeeFoundRange
'SeeFoundRange = MsgBox("Want to see each found Range?", 4)
SeeFoundRange = 1
Set mySpec = ActiveDocument
.......
NEAR THE END OF THE MAIN PROGRAM:
.......
Set mySpecWindow = mySpec.Windows(1)
mySpecWindow.WindowState = wdWindowStateMaximize
ScreenWidth = mySpecWindow.Width
ScreenHeight = mySpecWindow.Height
ScreenLeft = mySpecWindow.Left
ScreenTop = mySpecWindow.Top
Documents.Add.SaveAs "Reference List"
Set myList = Documents("Reference List.docx")
AFTER THE END OF THE MAIN PROGRAM
Sub Create_Event_Handler()
'create a new event handler object and assign it to objEventHandler
Set objEventHandler = New clsEventHandler
'Notify the current instance of Word to check for
'application event procedures
'inside the event handler object
Set objEventHandler.EventSource = Word.Application
End Sub
THE EVENT HANDLER
The body of the event handler code in the project window is as follows:
Public WithEvents EventSource As Word.Application
____________________________________________
Private Sub EventSource_WindowSelectionChange(ByVal Sel As Selection)
If ActiveDocument.Name = "Reference List.docx" Then
If Sel.Rows.Count > 1 Then GoTo 10
Set RS = Sel.Rows(1).Cells(3).Range
Set RE = Sel.Rows(1).Cells(4).Range
ValRS = Val(RS)
ValRE = Val(RE)
Set mySpecName = ActiveDocument.Tables(1).Cell(1, 1).Range
DotPos = InStr(mySpecName, ".")
mySpecName = Left(mySpecName, DotPos + 3)
Set mySpec = Documents(mySpecName)
mySpec.Range(ValRS, ValRE).Select
End If
10 End Sub
The definitions in the two drop down windows at the top of the window are:
"EventSource" and "WindowSelectionChange"
The event manager is a small amount of code that is supposed to detect a click of the mouse when the cursor is in a field of the table of the new document ("Reference List.docx") and highlight a corresponding point in the original document ("mySpec") in the main body of code. I have not been able to get the Event Manager to respond to the event and run the code (it never gets to the first line of the event handler code). I am looking for any clue as to why. This was the only event handler I wrote. I know that before I got it working, I had to study event handling a lot and play with variations. I used an article called "Taking Control of Microsoft Work through Events" Bill Coan 2003 as my reference. I have re-studied the article and tried variations, but to no avail. My ignorance on classes and defining them is great.
Here is the architecture for the event manager as I last left it several months ago:
MAIN PROGRAM - FIRST SEVERAL LINES:
'MODIFIED ACCORDING TO "TAKE CONTROL OF ...." by Coan
Dim objEventHandler As clsEventHandler
Dim NotElementNoStr(), NotElementNoStart(), NotElementNoEnd(), _
ElementNoStr(), ElementNoStart(), ElementNoEnd(), ElementNoPrecedingStr(), _
ElementNoGroupStr(), ElementNoGroupStart(), ElementNoGroupEnd(), ElementNoGroupPrecedingStr()
Dim NotElementNoSize, ElementNoSize, ElementNoGroupSize As Integer
Dim mySpecWindow As Window, myListWindow As Window, _
mySpec As Document, myList As Document, myRange As Range, LastAbstractStrRange As Range
NotElementNoSize = 0: ElementNoSize = 0: ElementNoGroupSize = 0
'Make the next line active, the one thereafter inactive to activate SeeFoundRange
'SeeFoundRange = MsgBox("Want to see each found Range?", 4)
SeeFoundRange = 1
Set mySpec = ActiveDocument
.......
NEAR THE END OF THE MAIN PROGRAM:
.......
Set mySpecWindow = mySpec.Windows(1)
mySpecWindow.WindowState = wdWindowStateMaximize
ScreenWidth = mySpecWindow.Width
ScreenHeight = mySpecWindow.Height
ScreenLeft = mySpecWindow.Left
ScreenTop = mySpecWindow.Top
Documents.Add.SaveAs "Reference List"
Set myList = Documents("Reference List.docx")
AFTER THE END OF THE MAIN PROGRAM
Sub Create_Event_Handler()
'create a new event handler object and assign it to objEventHandler
Set objEventHandler = New clsEventHandler
'Notify the current instance of Word to check for
'application event procedures
'inside the event handler object
Set objEventHandler.EventSource = Word.Application
End Sub
THE EVENT HANDLER
The body of the event handler code in the project window is as follows:
Public WithEvents EventSource As Word.Application
____________________________________________
Private Sub EventSource_WindowSelectionChange(ByVal Sel As Selection)
If ActiveDocument.Name = "Reference List.docx" Then
If Sel.Rows.Count > 1 Then GoTo 10
Set RS = Sel.Rows(1).Cells(3).Range
Set RE = Sel.Rows(1).Cells(4).Range
ValRS = Val(RS)
ValRE = Val(RE)
Set mySpecName = ActiveDocument.Tables(1).Cell(1, 1).Range
DotPos = InStr(mySpecName, ".")
mySpecName = Left(mySpecName, DotPos + 3)
Set mySpec = Documents(mySpecName)
mySpec.Range(ValRS, ValRE).Select
End If
10 End Sub
The definitions in the two drop down windows at the top of the window are:
"EventSource" and "WindowSelectionChange"