Analysis offers different callbacks that are executed with certain events. Callback 'Workbook_SAP_Initialize' is always executed. The other callbacks listed below must be registered with the API method 'SAPExecuteCommand' before being used for the first time. To deregister the callbacks, you can also use 'SAPExecuteCommand'.
Every callback can be registered only once in a workbook. If you register one of the callbacks for a second time, the first registration is overwritten.
Callback 'Workbook_SAP_Initialize' has to be defined in the 'ThisWorkbook' section of the VBA editor. The other callbacks should be defined in a module. If they are defined in the 'ThisWorkbook' or a sheet section, they have to be fully referenced during registration. In the 'ThisWorkbook' section, for example, you have to use ThisWorkbook.<MacroName> as a reference.
Use this callback to define the initialization logic of the workbook. This callback is similar to the 'Workbook_Open' event offered by Microsoft Excel, but it is executed after the Microsoft Excel event and after the Analysis standard initialization. If you open a workbook in Microsoft Excel and then activate the Analysis Add-In, the Excel event is executed when opening the workbook, and the Analysis standard initialization and the callback are executed after the activation of Analysis.
The callback has to be defined as a subroutine without input parameters.
' defined in ThisWorkbook Public Sub Workbook_SAP_Initialize() ' register callbacks Call Application.Run("SAPExecuteCommand", "RegisterCallback", "AfterRedisplay", "Callback_AfterRedisplay") Call Application.Run("SAPExecuteCommand", "RegisterCallback", "BeforePlanDataSave", "Callback_BeforePlanDataSave") Call Application.Run("SAPExecuteCommand", "RegisterCallback", "BeforePlanDataReset", "Callback_BeforePlanDataReset") End Sub
The 'AfterRedisplay', 'BeforPlanDataSave' and 'BeforePlanDataReset' callbacks are registered during workbook initialization and can therefore be used in the VBA project.
Use this callback to define the logic that should take place every time the workbook is redisplayed. For example, you can automatically adapt the formatting in the workbook after every redisplay.
The callback has to be defined as subroutine without input parameters. The callback is alled only when 'Pause Refresh' on workbook level is false or the user explicitly requested a 'Refresh All'. It is also called only when the active workbook has been changed.
' defined in a module Public Sub Callback_AfterRedisplay() ThisWorkbook.Worksheets("Sheet1").Cells(1, 1).Value = "Last redisplay: " ThisWorkbook.Worksheets("Sheet1").Cells(1, 2).Value = Now() End Sub
With this callback, the text "Last redisplay: " is displayed in cell A1 on sheet 1, and in cell A2, the date and time of the last redisplay are displayed. The information is updated after every redisplay.
With the following code, you can get the crosstabs and data sources that have been changed since the last user action and that therefore now have been updated in the sheets.
An array is returned like for 'SAPListOf' with technical name, name and data source.
lResult= Application.Run("SAPGetProperty", "CHANGEDDATASOURCES")An array is returned like for 'SAPListOf' with technical name and name.
Use this callback to define the logic that should be executed every time a user saves plan data. For example, you can execute a planning function (SAPExecutePlanningFunction).
The callback has to be defined as a function returning a Boolean value without input parameters. If the function returns false, the save will not be executed.
' defined in a module Public Function Callback_BeforePlanDataSave() As Boolean Dim lResult As Integer lResult = Application.Run("SAPExecutePlanningFunction", "PF_1") If lResult <> 1 Then ' planning function execution failed, cancel save Call MsgBox("Planning Function (PF_1) execution failed. Data will not be saved.", vbCritical, "Error") Callback_BeforePlanDataSave = False Else Callback_BeforePlanDataSave = True End If End Function
Every time a user selects the Save button to save planning data, planning function PF_1 is executed. If the planning function can be executed, the planning data is saved. If the execution fails, a message box with the text "Planning Function (PF_1) execution failed. Data will not be saved." is displayed.
Use this callback to define the logic that should be executed every time a user resets plan data to the previously saved state. For example, you define that the user is asked if the data really should be reset.
The callback has to be defined as a function returning a Boolean value without input parameters. If the function returns 'false', the reset will not be executed.
' defined in a module Public Function Callback_BeforePlanDataReset() As Boolean Dim lAnswer As VbMsgBoxResult lAnswer = MsgBox("Do you really want to reset planning data?", vbYesNo, "Reset") If lAnswer = vbYes Then Callback_BeforePlanDataReset = True Else Callback_BeforePlanDataReset = False End If End Function
Every time a user enters planning data and selects
, a message box with the text "Do you really want to reset planning data?" is displayed. If the user selects 'Yes', the entered data is reset to the previous saved state. If the user selects 'No', the reset is not executed.