PDA

View Full Version : Raising Events in Word via Classes



mansky
07-13-2006, 01:45 PM
Hi,
I have my own FileOpen and FileOpenFile macros in a Global template loaded from the Word startup folder. I've got my own versions of these macros in order to replace Word's version with mine, so that a custom class I've created can be used to keep a running list of Word files that are currently open. The reason for the custom class is so that I can get around the limit of 9 files in the Recently Opened list of files. The upper limit seems to be 9 and cannot (as far as I know) be changed.

It seems as if my version of FileOpen now prevents the User from being able to double click on a File and open it directly. The selected file now briefly opens and then closes and Word proceeds to open a blank new document (as I've coded it to do in my version of FileOpen.) It does however show up as the first item in the list of "Recent Files", indicating that Word indeed (I think) fired a Double-Click event.

As I understand it in Word, an Application object can be declared using the keyword "WithEvents" in order to tell Word that the class that this Application object is in contains an Event that can be raised.

I've got two questions:

(1) Will ActiveDocument.Name contain the name of the file just double-clicked by the User as Word comes up (assuming Word is not up and running)? That is, does the OS pass the double-click event onto Word so that Word actually does the opening of the file?

(2) Can I Raise a second event inside the subroutine defining the first event?

I'm trying to figure out how/what event exactly is passed onto Word from the OS upon double-clicking a file, so that I can then add the appropriate event handler to my existing code.

Here's the code that's executed from my global template in the Startup folder:

Sub AutoExec()
... code snipped ...

fp = FreeFile()
FE = FileExists(FileTracker)

NF = 0
If FE Then
Open FileTracker For Input As #fp
isOpen = True
Do While Not EOF(fp)
Line Input #fp, F 'read in existing list of files
If (Len(F) > 0) Then
NF = NF + 1
Contents(NF) = F
End If
Loop

' Add list of files in FileTracker to custom _
' collection TOF (TrackOpenFiles)

For i = 1 To NF
TOF.Add (Contents(i))
Next i

'Now open the documents pointed to _
' in the TOF collection
Application.ScreenUpdating = False
For i = 1 To NF
Documents.Open Filename:=Contents(i)
With Documents(Contents(i)).ActiveWindow
.Height = 700
.Width = 800
.Top = 70
End With
Next i
Application.ScreenUpdating = True

' If FileTracker does not exist, then just do the usual, _
' open a new Document
Else
Application.ScreenUpdating = False
Documents.Add
With ActiveDocument.ActiveWindow
.Height = 700
.Width = 800
.Top = 70
End With
Application.ScreenUpdating = True
End If
Close #fp


Here's a code snippet of the new class (My_Class) and application I think I need:

Option Explicit
Public WithEvents My_Word As Application

Private Sub Class_Initialize()
Set My_Word = Application
End Sub

Private Sub My_Word_DblClick()
Debug.Print "I am in Double Click Event ..." 'debug statement
End Sub

The above snippet is in a new class module called My_Class in the same global template.

I "think" one solution is to replace the Debug.Print statement above, with a Raise statement that will fire a new event in my existing TOF class. The new event in TOF would then, in turn, invoke the Add method in TOF to add the double-clicked file to my running list of currently open Word files.

Does that make sense? What's the syntax of Raise when the object I'm trying to "fire" isn't an Err object, but rather an event in a custom class?


Any suggestions, or ideas would be greatly appreciated!


Ed

Killian
07-14-2006, 04:41 AM
Hi Ed,

I'm not entirely clear what the brief is here - but maybe I can clarify a couple of things...

No event is passed from the OS to Word. When you double-click a file in Explorer, Windows checks the file's associated application and launches it with the file path as an argument. Windows is "Message" based so the only thing the OS is waiting for a return message from the Word application WINDOW (the container for the application) to indicate it's status. All application level errors and events are handled by Word internally.

Enabling application events, "Public WithEvents My_Word As Application" will give you a list of events to choose from (AFAIK, "DblClick" isn't among them).
If you used "My_Word_WindowBeforeDoubleClick", it would fire when Word's document window was double-clicked but before any resulting action.

In the Object Model, "AcitveDocument" is part of the Application object and returns a reference to the document which has the active document window in Word. That's it. Nothing is passed from the OS.

Do you need to raise a second event from the class? It would be easier to just call a public routine/function from both locations.