bo analusis macros

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

Upload: yogeeswar-reddy

Post on 02-Jul-2015

1.088 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Bo analusis  macros

Tobias Kaufmann

SAP Customer Solution Adoption

Using the VBA API in SAP BusinessObjects

Analysis Office 1.1

Page 2: Bo analusis  macros

© 2011 SAP AG. All rights reserved. 2

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.

Page 3: Bo analusis  macros

AgendaAPI

Introduction

Samples

Enable Analysis Office Add-In

Refresh

Drilldown with Button

Set Filter

Dynamic Grid

Page 4: Bo analusis  macros

© 2011 SAP AG. All rights reserved. 4

IntroductionDocumentation

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

Page 5: Bo analusis  macros

© 2011 SAP AG. All rights reserved. 5

IntroductionAPI – 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 (“”….)

Page 6: Bo analusis  macros

AgendaAPI

Introduction

Samples

Enable Analysis Office Add-In

Refresh

Drilldown with Button

Set Filter

Dynamic Grid

Page 7: Bo analusis  macros

© 2011 SAP AG. All rights reserved. 7

Enable Analysis Office Add-In

Scenario

Ensure that the Analysis Office Add-In is loaded

Before the workbook macros are executed

Page 8: Bo analusis  macros

© 2011 SAP AG. All rights reserved. 8

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

Page 9: Bo analusis  macros

AgendaAPI

Introduction

Samples

Enable Analysis Office Add-In

Refresh

Drilldown with Button

Set Filter

Dynamic Grid

Page 10: Bo analusis  macros

© 2011 SAP AG. All rights reserved. 10

Refresh

Scenario

Ensure data is refreshed if needed

Before calling own functions or SAP function

Use error handling for refresh

Page 11: Bo analusis  macros

© 2011 SAP AG. All rights reserved. 11

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

Page 12: Bo analusis  macros

© 2011 SAP AG. All rights reserved. 12

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")

Page 13: Bo analusis  macros

AgendaAPI

Introduction

Samples

Enable Analysis Office Add-In

Refresh

Drilldown with Button

Set Filter

Dynamic Grid

Page 14: Bo analusis  macros

© 2011 SAP AG. All rights reserved. 14

Drilldown with Button Define Button with some actions

Scenario

Read current cell information for current dimension

Place new dimension “year” before the current dimension

Page 15: Bo analusis  macros

© 2011 SAP AG. All rights reserved. 15

Drilldown with ButtonStep 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)

Page 16: Bo analusis  macros

© 2011 SAP AG. All rights reserved. 16

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")

...

Page 17: Bo analusis  macros

© 2011 SAP AG. All rights reserved. 17

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

Page 18: Bo analusis  macros

© 2011 SAP AG. All rights reserved. 18

Drilldown with Button Result

Page 19: Bo analusis  macros

© 2011 SAP AG. All rights reserved. 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

Page 20: Bo analusis  macros

AgendaAPI

Introduction

Samples

Enable Analysis Office Add-In

Refresh

Drilldown with Button

Set Filter

Dynamic Grid

Page 21: Bo analusis  macros

© 2011 SAP AG. All rights reserved. 21

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

Page 22: Bo analusis  macros

© 2011 SAP AG. All rights reserved. 22

Set Filter

Insert Combobox

Format Control

Define Input range (values in dropdown)

and Cell Link (selected index)

Page 23: Bo analusis  macros

© 2011 SAP AG. All rights reserved. 23

Set Filter

Use second sheet for values

(Input range)

And selected index

(Cell link)

Transform selected index into

value for SAPSetFilter

using formula INDEX

Page 24: Bo analusis  macros

© 2011 SAP AG. All rights reserved. 24

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

Page 25: Bo analusis  macros

AgendaAPI

Introduction

Samples

Enable Analysis Office Add-In

Refresh

Drilldown with Button

Set Filter

Dynamic Grid

Page 26: Bo analusis  macros

© 2011 SAP AG. All rights reserved. 26

Dynamic Grid

Scenario

Use event Workbook_SheetChange to react on changes

Press Delete Key

Type dimension

and press return

F4

Page 27: Bo analusis  macros

Summary

Page 29: Bo analusis  macros

Thank You!

Contact information:

Tobias Kaufmann

RIG Expert

[email protected]