Using the VBA API in SAP BusinessObjects
Analysis Office 1.1
Tobias Kaufmann
SAP Customer Solution Adoption
Legal disclaimer


This presentation is not subject to your license agreement or any other agreement
with SAP. SAP has no obligation to pursue any course of business outlined in this
presentation or to develop or release any functionality mentioned in this
presentation. This presentation and SAP's strategy and possible future
developments are subject to change and may be changed by SAP at any time for
any reason without notice. This document is provided without a warranty of any
kind, either express or implied, including but not limited to, the implied warranties of
merchantability, fitness for a particular purpose, or non-infringement. SAP assumes
no responsibility for errors or omissions in this document, except if such damages
were caused by SAP intentionally or grossly negligent.




© 2011 SAP AG. All rights reserved.                                                    2
Agenda
API              Samples
 Introduction    Enable Analysis Office Add-In
                  Refresh
                  Drilldown with Button
                  Set Filter
                  Dynamic Grid
Introduction
Documentation


Use the online help in Analysis Office as reference for the API




© 2011 SAP AG. All rights reserved.                               4
Introduction
API – What for?


Building sophisticated BI workbooks
Using formulas
 Call formulas within cell
 Get and show information like filter values, meta data, and data
 Set filter component
Using macros
 Used in VBA editor (e.g. behind some developer items like buttons, check box etc.)
 Execute planning functions/sequences, set dimensions in grid, set filter, read cell context
  etc.
 Typically using application.run (“”….)




© 2011 SAP AG. All rights reserved.                                                             5
Agenda
API              Samples
 Introduction    Enable Analysis Office Add-In
                  Refresh
                  Drilldown with Button
                  Set Filter
                  Dynamic Grid
Enable Analysis Office Add-In


Scenario
 Ensure that the Analysis Office Add-In is loaded
 Before the workbook macros are executed




© 2011 SAP AG. All rights reserved.                  7
Enable Analysis Office Add-In


Implement Workbook_Open in ThisWorkbook
  Loop all COMAddIns
  Set Connect to True for Analysis Office



Option Explicit
Private Sub Workbook_Open()
     Call EnableAnalysisOffice
End Sub
Private Sub EnableAnalysisOffice()
     Dim addin As COMAddIn
     For Each addin In Application.COMAddIns
            If addin.progID = "SBOP.AdvancedAnalysis.Addin.1" Then
                   If addin.Connect = False Then addin.Connect = True
            End If
     Next
End Sub


© 2011 SAP AG. All rights reserved.                                     8
Agenda
API              Samples
 Introduction    Enable Analysis Office Add-In
                  Refresh
                  Drilldown with Button
                  Set Filter
                  Dynamic Grid
Refresh


Scenario
 Ensure data is refreshed if needed
 Before calling own functions or SAP function
 Use error handling for refresh




© 2011 SAP AG. All rights reserved.              10
Refresh


Use SAPExecuteCommand with Refresh
  Refresh on error handling

Public Function MyGetData() As String
     Dim lCellContent As String
     On Error GoTo refresh
     lCellContent = Application.Run("SAPGetData", "DS_1", "4J97S26KX1BYBQ4GGQJNZGD9T", "0D_PH1=DS20")
     MyGetData = lCellContent
     Exit Function
refresh:
     Dim lResult As Long
     MsgBox "Refresh“
     lResult = Application.Run("SAPSetRefreshBehaviour", "Off")
     lResult = Application.Run("SAPExecuteCommand", "Refresh")
     lResult = Application.Run("SAPSetRefreshBehaviour", "On“)
     lCellContent = Application.Run("SAPGetData", "DS_1", "4J97S26KX1BYBQ4GGQJNZGD9T", "0D_PH1=DS20“)
     MyGetData = lCellContent
End Function


© 2011 SAP AG. All rights reserved.                                                                11
Refresh


Tip
  In case you are implementing your own formula (public function)
  Which is refreshing the data
  You should change the refresh behavior before and after this call

  Or use the Refresh Workbook on Opening option

Dim lResult as long

lResult = Application.Run("SAPSetRefreshBehaviour", "Off")

lResult = Application.Run("SAPExecuteCommand", "Refresh")

lResult = Application.Run("SAPSetRefreshBehaviour", "On")




© 2011 SAP AG. All rights reserved.                                    12
Agenda
API              Samples
 Introduction    Enable Analysis Office Add-In
                  Refresh
                  Drilldown with Button
                  Set Filter
                  Dynamic Grid
Drilldown with Button
Define Button with some actions


Scenario
 Read current cell information for current dimension
 Place new dimension “year” before the current dimension




© 2011 SAP AG. All rights reserved.                         14
Drilldown with Button
Step 1: Insert Button and Create Macro


Insert Button
 Use “developer” tab
  (in case you don’t see it,
  you might need to activate it in your Excel settings)




Create macro (that executes on click)



© 2011 SAP AG. All rights reserved.                       15
Drilldown with Button
Step 2: Get Cell Context


Use SAPGetCellInfo
     IResult = Application.Run("SAPGetCellInfo", ActiveCell, "DIMENSION")
     Reads context of cell that the cursor is placed on (ActiveCell)
     Requests dimension information
     lResult(1) will contain the data source alias
     Iresult(2) will contain the dimension key


Option Explicit

Sub Button1_Click()

      Dim lResult

      On Error GoTo leave

      lResult = Application.Run("SAPGetCellInfo", ActiveCell, "DIMENSION")

...




© 2011 SAP AG. All rights reserved.                                          16
Drilldown with Button
Step 3: Define drilldown


Use SAPMoveDimension
  OkCode = Application.Run("SAPMoveDimension", IResult(1), "0CALYEAR", "BEFORE",
   IResult(2))
  Inserts dimension “0CALYEAR” in currently chosen data source before (“BEFORE”) the
   dimension that the cursor is placed on

Option Explicit

Sub Button1_Click()

     Dim lResult
     Dim OkCode

     On Error GoTo leave

     lResult = Application.Run("SAPGetCellInfo", ActiveCell, "DIMENSION")

     OkCode = Application.Run("SAPMoveDimension", lResult(1), "0CALYEAR", "BEFORE", lResult(2))

leave:

End Sub



© 2011 SAP AG. All rights reserved.                                                               17
Drilldown with Button
Result




© 2011 SAP AG. All rights reserved.   18
Drilldown with Button


Tip
  Improve error handling by combining…
    1.     Enable Analysis Office Add-In (see other slide)
    2.     Refresh
    3.     Drilldown with Button
Option Explicit
Sub Button1_Click()
    Dim lResult
    On Error GoTo leave
    lResult = Application.Run("SAPGetCellInfo", ActiveCell, "DIMENSION")
    lResult = Application.Run("SAPMoveDimension", lResult(1), "0CALYEAR", "BEFORE", lResult(2))
    Exit Sub
leave:
    lResult = Application.Run("SAPSetRefreshBehaviour", "Off")
    lResult = Application.Run("SAPExecuteCommand", "Refresh")
    lResult = Application.Run("SAPSetRefreshBehaviour", "On“)
    lResult = Application.Run("SAPGetCellInfo", ActiveCell, "DIMENSION")
    If IsError(lResult) = True Then
        MsgBox "No dimension selected."
    Else
        lResult = Application.Run("SAPMoveDimension", lResult(1), "0CALYEAR", "BEFORE", lResult(2))
    End If
End Sub



© 2011 SAP AG. All rights reserved.                                                               19
Agenda
API              Samples
 Introduction    Enable Analysis Office Add-In
                  Refresh
                  Drilldown with Button
                  Set Filter
                  Dynamic Grid
Set Filter


Scenario
 Use function SAPSetFilter to set a filter
 Use parameter Member Format for selecting single or multiple values
 Use combo boxes for choosing parameter and values




© 2011 SAP AG. All rights reserved.                                     21
Set Filter


Insert Combobox



Format Control




Define Input range (values in dropdown)
and Cell Link (selected index)




© 2011 SAP AG. All rights reserved.       22
Set Filter


Use second sheet for values
(Input range)



And selected index
(Cell link)



Transform selected index into
value for SAPSetFilter
using formula INDEX




© 2011 SAP AG. All rights reserved.   23
Set Filter


Assign macro to control

Option Explicit

Sub DropDown2_Change()
    Dim selectedType As String
    Dim selectedValue As String
    Dim dimension As String
    Dim formulaAlias As String
    Dim r

    selectedType = Worksheets("Sheet2").Range("C1").Value
    selectedValue = Worksheets("Sheet2").Range("C7").Value
    dimension = "0D_PH2"
    formulaAlias = "DS_1"
    r = Application.Run("SAPSetFilter", formulaAlias, dimension, selectedValue, selectedType)
End Sub




© 2011 SAP AG. All rights reserved.                                                             24
Agenda
API              Samples
 Introduction    Enable Analysis Office Add-In
                  Refresh
                  Drilldown with Button
                  Set Filter
                  Dynamic Grid
Dynamic Grid


Scenario
 Use event Workbook_SheetChange to react on changes



                                                 Press Delete Key


                                           Type dimension
                                           and press return




                                      F4

© 2011 SAP AG. All rights reserved.                                 26
Summary
Download


All samples are available via the following link

https://sapmats-de.sap-
ag.de/download/download.cgi?id=SZMBCX9HYNLGEPDM4GWXZ865CWSHX4
YXWNN8BILFYW38Y7HMHX

If the link is invalid, please contact tobias.kaufmann@sap.com




© 2011 SAP AG. All rights reserved.                              28
Thank You!


Contact information:

Tobias Kaufmann
RIG Expert
tobias.kaufmann@sap.com

Bo analusis macros

  • 1.
    Using the VBAAPI in SAP BusinessObjects Analysis Office 1.1 Tobias Kaufmann SAP Customer Solution Adoption
  • 2.
    Legal disclaimer This presentationis not subject to your license agreement or any other agreement with SAP. SAP has no obligation to pursue any course of business outlined in this presentation or to develop or release any functionality mentioned in this presentation. This presentation and SAP's strategy and possible future developments are subject to change and may be changed by SAP at any time for any reason without notice. This document is provided without a warranty of any kind, either express or implied, including but not limited to, the implied warranties of merchantability, fitness for a particular purpose, or non-infringement. SAP assumes no responsibility for errors or omissions in this document, except if such damages were caused by SAP intentionally or grossly negligent. © 2011 SAP AG. All rights reserved. 2
  • 3.
    Agenda API Samples  Introduction  Enable Analysis Office Add-In  Refresh  Drilldown with Button  Set Filter  Dynamic Grid
  • 4.
    Introduction Documentation Use the onlinehelp in Analysis Office as reference for the API © 2011 SAP AG. All rights reserved. 4
  • 5.
    Introduction API – Whatfor? Building sophisticated BI workbooks Using formulas  Call formulas within cell  Get and show information like filter values, meta data, and data  Set filter component Using macros  Used in VBA editor (e.g. behind some developer items like buttons, check box etc.)  Execute planning functions/sequences, set dimensions in grid, set filter, read cell context etc.  Typically using application.run (“”….) © 2011 SAP AG. All rights reserved. 5
  • 6.
    Agenda API Samples  Introduction  Enable Analysis Office Add-In  Refresh  Drilldown with Button  Set Filter  Dynamic Grid
  • 7.
    Enable Analysis OfficeAdd-In Scenario  Ensure that the Analysis Office Add-In is loaded  Before the workbook macros are executed © 2011 SAP AG. All rights reserved. 7
  • 8.
    Enable Analysis OfficeAdd-In Implement Workbook_Open in ThisWorkbook  Loop all COMAddIns  Set Connect to True for Analysis Office Option Explicit Private Sub Workbook_Open() Call EnableAnalysisOffice End Sub Private Sub EnableAnalysisOffice() Dim addin As COMAddIn For Each addin In Application.COMAddIns If addin.progID = "SBOP.AdvancedAnalysis.Addin.1" Then If addin.Connect = False Then addin.Connect = True End If Next End Sub © 2011 SAP AG. All rights reserved. 8
  • 9.
    Agenda API Samples  Introduction  Enable Analysis Office Add-In  Refresh  Drilldown with Button  Set Filter  Dynamic Grid
  • 10.
    Refresh Scenario  Ensure datais refreshed if needed  Before calling own functions or SAP function  Use error handling for refresh © 2011 SAP AG. All rights reserved. 10
  • 11.
    Refresh Use SAPExecuteCommand withRefresh  Refresh on error handling Public Function MyGetData() As String Dim lCellContent As String On Error GoTo refresh lCellContent = Application.Run("SAPGetData", "DS_1", "4J97S26KX1BYBQ4GGQJNZGD9T", "0D_PH1=DS20") MyGetData = lCellContent Exit Function refresh: Dim lResult As Long MsgBox "Refresh“ lResult = Application.Run("SAPSetRefreshBehaviour", "Off") lResult = Application.Run("SAPExecuteCommand", "Refresh") lResult = Application.Run("SAPSetRefreshBehaviour", "On“) lCellContent = Application.Run("SAPGetData", "DS_1", "4J97S26KX1BYBQ4GGQJNZGD9T", "0D_PH1=DS20“) MyGetData = lCellContent End Function © 2011 SAP AG. All rights reserved. 11
  • 12.
    Refresh Tip  Incase you are implementing your own formula (public function)  Which is refreshing the data  You should change the refresh behavior before and after this call  Or use the Refresh Workbook on Opening option Dim lResult as long lResult = Application.Run("SAPSetRefreshBehaviour", "Off") lResult = Application.Run("SAPExecuteCommand", "Refresh") lResult = Application.Run("SAPSetRefreshBehaviour", "On") © 2011 SAP AG. All rights reserved. 12
  • 13.
    Agenda API Samples  Introduction  Enable Analysis Office Add-In  Refresh  Drilldown with Button  Set Filter  Dynamic Grid
  • 14.
    Drilldown with Button DefineButton with some actions Scenario  Read current cell information for current dimension  Place new dimension “year” before the current dimension © 2011 SAP AG. All rights reserved. 14
  • 15.
    Drilldown with Button Step1: Insert Button and Create Macro Insert Button  Use “developer” tab (in case you don’t see it, you might need to activate it in your Excel settings) Create macro (that executes on click) © 2011 SAP AG. All rights reserved. 15
  • 16.
    Drilldown with Button Step2: Get Cell Context Use SAPGetCellInfo  IResult = Application.Run("SAPGetCellInfo", ActiveCell, "DIMENSION")  Reads context of cell that the cursor is placed on (ActiveCell)  Requests dimension information  lResult(1) will contain the data source alias  Iresult(2) will contain the dimension key Option Explicit Sub Button1_Click() Dim lResult On Error GoTo leave lResult = Application.Run("SAPGetCellInfo", ActiveCell, "DIMENSION") ... © 2011 SAP AG. All rights reserved. 16
  • 17.
    Drilldown with Button Step3: Define drilldown Use SAPMoveDimension  OkCode = Application.Run("SAPMoveDimension", IResult(1), "0CALYEAR", "BEFORE", IResult(2))  Inserts dimension “0CALYEAR” in currently chosen data source before (“BEFORE”) the dimension that the cursor is placed on Option Explicit Sub Button1_Click() Dim lResult Dim OkCode On Error GoTo leave lResult = Application.Run("SAPGetCellInfo", ActiveCell, "DIMENSION") OkCode = Application.Run("SAPMoveDimension", lResult(1), "0CALYEAR", "BEFORE", lResult(2)) leave: End Sub © 2011 SAP AG. All rights reserved. 17
  • 18.
    Drilldown with Button Result ©2011 SAP AG. All rights reserved. 18
  • 19.
    Drilldown with Button Tip  Improve error handling by combining… 1. Enable Analysis Office Add-In (see other slide) 2. Refresh 3. Drilldown with Button Option Explicit Sub Button1_Click() Dim lResult On Error GoTo leave lResult = Application.Run("SAPGetCellInfo", ActiveCell, "DIMENSION") lResult = Application.Run("SAPMoveDimension", lResult(1), "0CALYEAR", "BEFORE", lResult(2)) Exit Sub leave: lResult = Application.Run("SAPSetRefreshBehaviour", "Off") lResult = Application.Run("SAPExecuteCommand", "Refresh") lResult = Application.Run("SAPSetRefreshBehaviour", "On“) lResult = Application.Run("SAPGetCellInfo", ActiveCell, "DIMENSION") If IsError(lResult) = True Then MsgBox "No dimension selected." Else lResult = Application.Run("SAPMoveDimension", lResult(1), "0CALYEAR", "BEFORE", lResult(2)) End If End Sub © 2011 SAP AG. All rights reserved. 19
  • 20.
    Agenda API Samples  Introduction  Enable Analysis Office Add-In  Refresh  Drilldown with Button  Set Filter  Dynamic Grid
  • 21.
    Set Filter Scenario  Usefunction SAPSetFilter to set a filter  Use parameter Member Format for selecting single or multiple values  Use combo boxes for choosing parameter and values © 2011 SAP AG. All rights reserved. 21
  • 22.
    Set Filter Insert Combobox FormatControl Define Input range (values in dropdown) and Cell Link (selected index) © 2011 SAP AG. All rights reserved. 22
  • 23.
    Set Filter Use secondsheet for values (Input range) And selected index (Cell link) Transform selected index into value for SAPSetFilter using formula INDEX © 2011 SAP AG. All rights reserved. 23
  • 24.
    Set Filter Assign macroto control Option Explicit Sub DropDown2_Change() Dim selectedType As String Dim selectedValue As String Dim dimension As String Dim formulaAlias As String Dim r selectedType = Worksheets("Sheet2").Range("C1").Value selectedValue = Worksheets("Sheet2").Range("C7").Value dimension = "0D_PH2" formulaAlias = "DS_1" r = Application.Run("SAPSetFilter", formulaAlias, dimension, selectedValue, selectedType) End Sub © 2011 SAP AG. All rights reserved. 24
  • 25.
    Agenda API Samples  Introduction  Enable Analysis Office Add-In  Refresh  Drilldown with Button  Set Filter  Dynamic Grid
  • 26.
    Dynamic Grid Scenario  Useevent Workbook_SheetChange to react on changes Press Delete Key Type dimension and press return F4 © 2011 SAP AG. All rights reserved. 26
  • 27.
  • 28.
    Download All samples areavailable via the following link https://sapmats-de.sap- ag.de/download/download.cgi?id=SZMBCX9HYNLGEPDM4GWXZ865CWSHX4 YXWNN8BILFYW38Y7HMHX If the link is invalid, please contact tobias.kaufmann@sap.com © 2011 SAP AG. All rights reserved. 28
  • 29.
    Thank You! Contact information: TobiasKaufmann RIG Expert tobias.kaufmann@sap.com