PDA

View Full Version : Event manager



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"

SamT
07-17-2013, 07:41 AM
This line in the sub implies that you have many douments you use to find the name of the document you wish to select a word in. That each of those Docs has a first table with the desired Doc.Name in the first cell.mySpecName = ActiveDocument.Tables(1).Cell(1, 1).Range.TextI don't know enough about your system to comment further.

This code, developed from your example, has several msgboxes. The first will return the Name of the active document as VBA sees it. Check the name in the MsgBox to see if it has an extension. The Code below does not use the extension as part of the name. Modify if needed.

The following code is not the best way to handle your situation, but without more knowledge, it's the best I can do to get you started fixing the problem.

As written, the code below will exit after you close one of the next two msgboxes. Which depend on whether or not the code should run on the document "Reference List"
Option Explicit

Private Sub EventSource_WindowSelectionChange(ByVal Sel As Selection)
'Dim RS As Range 'Redundant. Removed by Samt
'Dim RE As Range
Dim mySpecName As String
Dim mySpec As Document
Dim ValRs As Long
Dim ValRe As Long
Dim DotPos As Long

'Moved to top. Always Exit sub if more than one line selected
If Sel.Rows.Count > 1 Then Exit Sub

MsgBox ("Verifying DocName Ext or lack of: " & ActiveDocument.Name) 'Verify name is DocName + "docx"

'New Comments + commented out original Code follows:
'Comments:
'Exit sub only if Doc.Name is "Reference List.docx"
'AND IF Selection is more than one line
'Code:
' If ActiveDocument.Name = "Reference List.docx" Then
' If Sel.Rows.Count > 1 Then Exit Sub

'Suggested code Follows:
'Comments:
'Select one line of following code. Leaving both lines will always exit sub
'Note lack of Name extension. See MsgBox results above

'IF Doc.Name NOT "Reference List"
MsgBox "Quitting Event Handler if ActiveDocument.Name IS NOT ""Reference List"""
If ActiveDocument.Name <> "Reference List" Then Exit Sub
'If DocName IS "Reference List"
MsgBox "Quitting Event Handler if ActiveDocument.Name IS ""Reference List"""
If ActiveDocument.Name = "Reference List" Then Exit Sub


ValRs = Val(Sel.Rows(1).Cells(3).Range) 'Previously set RS to Range
ValRe = Val(Sel.Rows(1).Cells(4).Range)
'ValRs = Val(RS) 'Returns only leading numbers from a string.
'ValRe = Val(RE)

mySpecName = ActiveDocument.Tables(1).Cell(1, 1).Range.Text
DotPos = InStr(mySpecName, ".")
mySpecName = Left(mySpecName, DotPos + 3)

Set mySpec = Documents(mySpecName)
mySpec.Range(ValRs, ValRe).Select 'Selects Doc.Characters ValRs to ValRe
MsgBox ("Selected Characters are: " & Selection)

End Sub
A better way would be to move all the working bits of code to a standard module (Sub SelectInMe)in the document whose word you want to select (Doc1) and in the Event Handler, get the Selections Document name and rangeSelDocName = Sel.Document.Name
SelRng = Sel.RangePerform rudimentary checks to exit the selection handler, then pass the SelDocName and SelRng to Doc1.SelectInMe

I really hope this helps you out.

jimlamb1944
07-17-2013, 12:19 PM
SamT - Thank you so much for looking at my posting. I am not sure that I communicated my situation well enough. The main program analyzes a Word document that I have written that has many numbers in it that are figure reference numbers (for patent writing). The document being analyzed, let's call it "AnalyzedDoc", will have a .docx extension but any name. When the VBA program finishes analyzing the "AnalyzedDoc", a new document is created called "ReferenceList.docx". A table in "ReferenceList.docx" has had the name "AnalyzedDoc" stored in cell (1,1) of Table (1) of ReferenceList.docx. "ReferenceList.docx" is left on screen as the active document. Another table in "ReferenceList.docx" stores the reference numbers and their range start and end values (RS, RE).
The event handler is supposed to detect a mouse selection in a row of a third table of the ReferenceList.docx that stores and shows rows each including one reference number and the associated range of the reference number within the AnalyzedDoc. The AnalyzedDoc is then activated having the reference number selected that is stored in the selected row of the ReferenceList.docx. The first line of the event handler (If ActiveDocument.Name = "Reference List.docx" Then) is a check to make sure that the user has not changed the active document to something else. The second line makes sure the user did not select more than one row. If I put a breakpoint at the first line and run the main program, I never have the occurrence of VBA bringing up the event handler to show the break occurring (I.e., the first line is never executed). So my conclusion is that I do not have the Event Handler properly set up, somewhere in the declarations of the main program, the sub program, or in the setup of the event handler. I am sure that my code in the Event Handler is amateurish, and perhaps something in the code after the first line causes the Event Handler to never be activated, which is what you may be telling me.

SamT
07-23-2013, 07:41 AM
SamT - Thank you so much for looking at my posting.

I am not sure that I communicated my situation well enough.

The main program analyzes a Word document that I have written that has many numbers in it that are figure reference numbers (for patent writing).

The document being analyzed, let's call it "AnalyzedDoc", will have a .docx extension but any name. When the VBA program finishes analyzing the "AnalyzedDoc", a new document is created called "ReferenceList.docx".

A table in "ReferenceList.docx" has had the name "AnalyzedDoc" stored in cell (1,1) of Table (1) of ReferenceList.docx.

"ReferenceList.docx" is left on screen as the active document. Another table in "ReferenceList.docx" stores the reference numbers and their range start and end values (RS, RE).

The event handler is supposed to detect a mouse selection in a row of a third table of the ReferenceList.docx that stores and shows rows each including one reference number and the associated range of the reference number within the AnalyzedDoc.

The AnalyzedDoc is then activated having the reference number selected that is stored in the selected row of the ReferenceList.docx.

The first line of the event handler (If ActiveDocument.Name = "Reference List.docx" Then) is a check to make sure that the user has not changed the active document to something else. The second line makes sure the user did not select more than one row.

If I put a breakpoint at the first line and run the main program, I never have the occurrence of VBA bringing up the event handler to show the break occurring (I.e., the first line is never executed).

So my conclusion is that I do not have the Event Handler properly set up, somewhere in the declarations of the main program, the sub program, or in the setup of the event handler. I am sure that my code in the Event Handler is amateurish, and perhaps something in the code after the first line causes the Event Handler to never be activated, which is what you may be telling me. You said
a new document is created called "ReferenceList.docx". But I think you mean that "ReferenceList already exists on the hard drive and you are just opening it.

In any case, the document that holds the code, ("Main.docm") must remain open at all times and the event handling code must be in a class module in "Main.docm."

I don't really have any time to work on it right now, so I am really hoping this will do you. If not, well, you know where to find me.