Hello,
the document events (ex: "OnOpen"
) can be known from the ThisComponent.Events
property. The Events
property exposes an array of Property type, two per event.
The two event properties are:
Property 1
- Name: the event managing flag (ex:
"Script"
)
- Handle: (internal?)
- Value: the script to call when the event occurs (ex:
"vnd.sun.star.script:Standard.Module1.OnOpenDocument?language=Basic&location=document"
)
- State: (internal?)
Property 2
- Name: the event type flag (ex:
"EventType"
)
- Handle: (internal?)
- Value: the event name (ex:
"OnLoad"
)
- State: (internal?)
Note that apparently nothing ensures you read Property 1
before Property 2
(well, I never found any evidence that would ensure the order for reading these properties values).
Thus I have the following function to retrieve the Events information (extracted from my TheCAT extension):
Function GetEventsArray(ByRef pEvents As Object) As Variant
'converts the event properties into an easier to handle array of strings.
'Output: a 3 column array with 1 row per event:
'-- col 0: the event name (‘OnLoad’)
'-- col 1: the event process (‘vnd.sun.star.script:Standard.Module1.OnOpenDocument?language=Basic&location=document’)
'-- col 2: the complete string
Dim l_Array() As Variant
Dim l_Item As String
Dim l_Desc As Variant
Dim l_Str As String
Dim l_Type As String
Dim l_Pos1 As Long
Dim l_Pos2 As Long
Dim i As Integer
ReDim l_Array(UBound(pEvents.ElementNames), 2)
i = 0
For Each l_Item In pEvents.ElementNames
l_Desc = pEvents.getByName(l_Item)
'check the array is valid (ArrayExists() function is part of my array tools)
If Tools.ArrayExists(l_Desc) Then
l_Array(i, 0) = l_Item
'get type
l_Type = ""
If (l_Desc(0).Name = "EventType") Then
l_Type = l_Desc(0).Value
ElseIf (l_Desc(1).Name = "EventType") Then
l_Type = l_Desc(1).Value
End If
l_Array(i, 1) = l_Type
'get handler
l_Str = ""
If (l_Desc(0).Name = l_Type) Then
l_Str = l_Desc(0).Value
ElseIf (l_Desc(1).Name = l_Type) Then
l_Str = l_Desc(1).Value
End If
'if "Script" then l_Str is smthg like: "vnd.sun.star.script:Standard.Module1.OnOpenDocument?language=Basic&location=document"
l_Array(i, 2) = l_Str
i = i + 1
End If
Next l_Item
If (i > 0) Then
ReDim Preserve l_Array(i - 1, 2)
Else
Erase l_Array()
End If
GetEventsArray = l_Array()
End Function 'GetEventsArray
Call: MyArray() = GetEventsArray(ThisComponent.Events)
So, changing the event managing string should be feasible, but I never tried to add such an event manager when it doesn’t exist yet.
In the worst case, if you uncompress the document and have a look at its content.xml
file, you find:
<office:scripts>
<office:event-listeners>
<script:event-listener script:language="ooo:script" script:event-name="dom:load" xlink:href="vnd.sun.star.script:Standard.Classeur.OnDocumentOpen?language=Basic&location=document" xlink:type="simple" />
</office:event-listeners>
</office:scripts>
which means you may add the event manager (aka listener) here. I, for sure, know this can be done.
This solution pro is that you may do that silently in some batch program.
HTH,