Application.Run("SAPSetVariable", <variable technical name>, <variable value>, "INPUT_STRING", <variable datasource>)
Application.Run("SAPSetVariable", <variable technical name>, "", "INPUT_STRING", <variable datasource>)
Application.Run("SAPSetFilter", <filter datasource>, <filter technical name>, <filter value>, "INPUT_STRING")
Application.Run("SAPSetFilter", <filter datasource>, <filter technical name>, "", "INPUT_STRING")
Application.Run("SAPExecuteCommand", "RefreshData")
Call Application.Run("SAPSetRefreshBehaviour", "Off")
Call Application.Run("SAPExecuteCommand", "PauseVariableSubmit", "On")
Call Application.Run("SAPExecuteCommand", "PauseVariableSubmit", "Off")
Call Application.Run("SAPSetRefreshBehaviour", "On")
Call Application.Run("SAPSetRefreshBehaviour", "Off")
Call Application.Run("SAPSetRefreshBehaviour", "On")
' Set up some variables first
Dim lresult, mytable As ListObject
Dim paramatersarray As Variant
Dim Field_Value As String, Filter_Value As String, Field_Datasource As String, Field_Type As String
Dim wb As Workbook
' Lets put the PARAMETERS table into memory in an internal array
' We only want the data, not the headings, so we refer to DataBodyRange on the table
' open the parameters file read only
Set wb = Workbooks.Open(<path and filename of your parameter file>, True, True)
' Now take a copy of the parameters table into memory
Set mytable = wb.Sheets("Parameters").ListObjects("PARAMETERS")
paramatersarray = mytable.DataBodyRange
' Now close the parameters file and clear out the memory
wb.Close False
Set wb = Nothing
' Start a new instance of excel, load up the file we want to refresh
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set wb2 = xlApp.Workbooks.Open(<the data file you want to refresh>, False, False)
' Force the Analysis for Office addin to be enabled on the second instance of Excel
xlApp.Application.StatusBar = "Making sure Analysis for Office addin is active..."
For Each AddIn In xlApp.Application.COMAddIns
If AddIn.progID = "SapExcelAddIn" Then
If AddIn.Connect = False Then
AddIn.Connect = True
ElseIf AddIn.Connect = True Then
AddIn.Connect = False
AddIn.Connect = True
End If
End If
Next
' Now log in to the BW system
lresult = xlApp.Application.Run("SAPLogon", "DS_1", bw_client, bw_user, bw_password)
' Refresh all of the data sources in the whole workbook so we can start making use of it
lresult = xlApp.Application.Run("SAPExecuteCommand", "RefreshData")
' Now lets loop through the parameters array.
' LBound = Lower Bound, the lowest record number (in this case 1)
' UBound = Upper Bound, the highest record number
myloop = 0 ' setting this to something that won't match during the first loop, as we know everything starts from 1 in the parameter file
For mainloop = LBound(paramatersarray) To UBound(paramatersarray)
' put the fields into easier variables for the moment
Field_Loopnum = paramatersarray(mainloop, 1)
Field_Datasource = paramatersarray(mainloop, 2)
Field_Type = paramatersarray(mainloop, 3)
Field_Field = paramatersarray(mainloop, 4)
Field_Value = paramatersarray(mainloop, 5)
' We also want to see if there is a 'next' record, to determine if we are on the last record of the current loop.
If (mainloop + 1) > UBound(paramatersarray) Then
Field_Loopnum_next = "No more records" ' Doesn't matter what this is, as long as its not the number of the last loop entry
Else
Field_Loopnum_next = paramatersarray(mainloop + 1, 1)
End If
If Field_Loopnum <> myloop Then
' get ready for a new set of variables
' We are going to process variables first so lets turn off variable submissions to keep the speed up
Call xlApp.Application.Run("SAPSetRefreshBehaviour", "Off")
Call xlApp.Application.Run("SAPExecuteCommand", "PauseVariableSubmit", "On")
End If
' If we find a VARIABLE field, action it.
If Field_Type = "VARIABLE" Then
lresult = xlApp.Application.Run("SAPSetVariable", Field_Field, Field_Value, "INPUT_STRING", Field_Datasource)
End If
If Field_Loopnum <> Field_Loopnum_next Then
' the next row in our parameters table has a different loop number, so lets unpause things to refresh the data
Call xlApp.Application.Run("SAPExecuteCommand", "PauseVariableSubmit", "Off")
' At this stage we have set up refreshed the data for variables, but the user may want to then set some filters.
' Loop through the same parameters array from the start again, but this time for the same loop number as the variables and look for filters this time.
For i = LBound(paramatersarray) To UBound(paramatersarray)
Filter_Loopnum = paramatersarray(i, 1)
Filter_DataSource = paramatersarray(mainloop, 2)
Filter_Type = paramatersarray(mainloop, 3)
Filter_Field = paramatersarray(mainloop, 4)
Filter_Value = paramatersarray(mainloop, 5)
If Filter_Loopnum = Field_Loopnum And Filter_Type = "FILTER" Then
lresult = xlApp.Application.Run("SAPSetFilter", Filter_DataSource, Filter_Field, Filter_Value, "INPUT_STRING")
End If
Next i
' We are all done with the filters for this specific loop Now refresh whats on screen
Call xlApp.Application.Run("SAPSetRefreshBehaviour", "On")
' ***************************************************************************
' At this point we now have a refreshed file with all the new variables and filters.
' Now you can save/saveas/email here before you move on to the next loop.
' ***************************************************************************
End If
' make a note of the loop we just finished as we will check it in the next loop
myloop = Field_Loopnum
Next mainloop
yesterday = Format(Date - 1, "dd.mm.yyyy")
today = Format(Date, "dd.mm.yyyy")
currentmonth = Format(Date, "mm.yyyy")
lastmonth = Format(Application.WorksheetFunction.EoMonth(Date, -1), "mm.yyyy")
endoflastmonth = Format(DateSerial(Year(Date), Month(Date), 0), "dd.mm.yyyy")
If <value field> Like "*TODAY*" Then
<value field> = Replace(<value field>, "TODAY", today)
End If
If Field_Type = "VARIABLE" Then
' see if we need to replace some date texts with actual dates
Field_Value = Text_Replace(Field_Value)
lresult = xlApp.Application.Run("SAPSetVariable", Field_Field, Field_Value, "INPUT_STRING", Field_Datasource)
End If
If Filter_Loopnum = Field_Loopnum And Filter_Type = “FILTER” Then
' see if we need to replace some date texts with actual dates
Filter_Value = Text_Replace(Filter_Value)
lresult = xlApp.Application.Run("SAPSetFilter", Filter_DataSource, Filter_Field, Filter_Value, "INPUT_STRING")
End If
Public Function Text_Replace(thetext As String) As String
If thetext Like "*TODAY*" Then
thetext = Replace(thetext, "TODAY", today)
End If
If thetext Like "*YESTERDAY*" Then
thetext = Replace(thetext, "YESTERDAY", yesterday)
End If
If thetext Like "*CURRENTMONTH*" Then
thetext = Replace(thetext, "CURRENTMONTH", currentmonth)
End If
If thetext Like "*ENDOFLASTMONTH*" Then
thetext = Replace(thetext, "ENDOFLASTMONTH", endoflastmonth)
End If
If thetext Like "*LASTMONTH*" Then
thetext = Replace(thetext, "LASTMONTH", lastmonth)
End If
Text_Replace = thetext
End Function
If Field_Loopnum <> myloop Then
' get ready for a new set of variables for potentially a new DataSource too
' Lets see if we need to undo any variables and filters first
Get_Active_Variables_and_Filters Field_Datasource
Cleanup_the_Datasource Field_Type, Field_Datasource
Call xlApp.Application.Run("SAPSetRefreshBehaviour", "Off")
Call xlApp.Application.Run("SAPExecuteCommand", "PauseVariableSubmit", "On")
End If
Application.Run(“SAPListOfVariables”, <datasource>, "INPUT_STRING", "ALL_FILLED")
Application.Run(“SAPGetVariable”, <datasource>, <non-technical name>, "TECHNICALNAME")
Application.Run(“SAPListOfEffectiveFilters”, <datasource>, "INPUT_STRING")
Application.Run("SAPListOfDimensions", <datasource>)
Dim MyVariablesArray As Variant
MyVariablesArray = Application.Run("SAPListOfVariables", <datasource>)
Sub Get_Active_Variables_and_Filters(ByVal thedatasource As String)
Dim tempVariablesArray As Variant, tempFiltersArray As Variant
Dim DimensionArray As Variant, x As Long
Dim techname As String, mysource As String
' If there is only 1 Variable, then SAPListOfVariables returns a one dimensional array. LBound = 1, UBound = 2
' If there is more than 1 Variable, SAPListOfVariables returns a two dimensional array.
' Same thing happens for SAPListOfEffectiveFilters
' Lets start by grabbing the Variables, Filters and Dimensions into seperate internal arrays.
tempVariablesArray = xlApp.Application.Run("SAPListOfVariables", thedatasource)
tempFiltersArray = xlApp.Application.Run("SAPListOfEffectiveFilters", thedatasource, "INPUT_STRING")
DimensionArray = xlApp.Application.Run("SAPListOfDimensions", thedatasource)
' We want to add to the correct global arrays, so start from row 1
Erase VariablesArray
Erase FiltersArray
ReDim VariablesArray(1 To 1) As String
ReDim FiltersArray(1 To 1) As String
' We grab the Dimensions info because that also holds the technical names of the filters.
' There is no SAPGetFilter function, so we'll make use of the Dimension list instead
'************
' VARIABLES
'************
' Lets count how many records there are in the Variables array
' We know each row has 2 fields, so we divide by 2 later to get the actual number of rows
array_start = 1
array_end = Application.CountA(tempVariablesArray) / 2
If array_end = 1 Then
' We have only 1 real record, so we know VariablesArray is ONE dimensional
techname = xlApp.Application.Run("SAPGetVariable", thedatasource, tempVariablesArray(1), "TECHNICALNAME")
' add the variable technical name to our global VariableArray
VariablesArray(UBound(VariablesArray)) = techname
Else
' We have more than 1 record, so we know VariablesArray is TWO dimensional
For arrayloop = array_start To array_end
techname = xlApp.Application.Run("SAPGetVariable", thedatasource, tempVariablesArray(arrayloop, 1), "TECHNICALNAME")
' add the variable technical name to our global VariableArray
VariablesArray(UBound(VariablesArray)) = techname
Next arrayloop
End If
'**********
' FILTERS
'**********
' Lets count how many records there are in the Filters array
' We know each row has 2 fields, so we divide by 2 later to get the actual number of rows
array_start = 1
array_end = Application.CountA(tempFiltersArray) / 2
If array_end = 1 Then
' We have only 1 real record, so we know FiltersArray is ONE dimensional
' Now lets get the technical name of the filter from the DimensionArray by looping through it
' until we find a match.
techname = ""
For dimensionloop = LBound(DimensionArray) To UBound(DimensionArray)
If DimensionArray(dimensionloop, 2) = FiltersArray(1) Then
techname = DimensionArray(dimensionloop, 1)
Exit For
End If
Next dimensionloop
' add the filter to our global FiltersArray
FiltersArray(UBound(FiltersArray)) = techname
Else
' We have more than 1 real record, so we know FiltersArray is TWO dimensional
For arrayloop = array_start To array_end
' Now lets get the technical name of the filter from the DimensionArray by looping through it
' until we find a match.
techname = ""
For dimensionloop = LBound(DimensionArray) To UBound(DimensionArray)
If DimensionArray(dimensionloop, 2) = tempFiltersArray(arrayloop, 1) Then
techname = DimensionArray(dimensionloop, 1)
Exit For
End If
Next dimensionloop
' add the filter to our global FiltersArray
FiltersArray(UBound(FiltersArray)) = techname
Next arrayloop
End If
' Lets do some cleanup on the temporary arrays
On Error Resume Next
Erase tempVariablesArray
Erase tempFiltersArray
Erase DimensionArray
End Sub
Private Sub Cleanup_the_Datasource(cleanuptype As String, thedatasource As String)
If cleanuptype = "CLEARALL" Then
' Lets loop through our filters array and clear them out
For i = LBound(FiltersArray) To UBound(FiltersArray)
lresult = xlApp.Application.Run("SAPSetFilter", thedatasource, FiltersArray(i), "", "INPUT_STRING")
Next i
' Pause any refreshing while we work on clearing our variables
Call xlApp.Application.Run("SAPSetRefreshBehaviour", "Off")
Call xlApp.Application.Run("SAPExecuteCommand", "PauseVariableSubmit", "On")
' Loop through the Variables array and clear them out
For i = LBound(VariablesArray) To UBound(VariablesArray)
lresult = xlApp.Application.Run("SAPSetVariable", VariablesArray(i), "", "INPUT_STRING", thedatasource)
Next i
' Temporarily turn back on refreshing to update things
Call xlApp.Application.Run("SAPSetRefreshBehaviour", "On")
Call xlApp.Application.Run("SAPExecuteCommand", "PauseVariableSubmit", "Off")
' And then turn it off again
Call xlApp.Application.Run("SAPSetRefreshBehaviour", "Off")
Call xlApp.Application.Run("SAPExecuteCommand", "PauseVariableSubmit", "On")
ElseIf cleanuptype = "CLEARVARIABLES" Then
' Pause any refreshing while we work on clearing our variables
Call xlApp.Application.Run("SAPSetRefreshBehaviour", "Off")
Call xlApp.Application.Run("SAPExecuteCommand", "PauseVariableSubmit", "On")
' Loop through the Variables array and clear them out
For i = LBound(VariablesArray) To UBound(VariablesArray)
lresult = xlApp.Application.Run("SAPSetVariable", VariablesArray(i), "", "INPUT_STRING", thedatasource)
Next i
' Temporarily turn back on refreshing to update things
Call xlApp.Application.Run("SAPSetRefreshBehaviour", "On")
Call xlApp.Application.Run("SAPExecuteCommand", "PauseVariableSubmit", "Off")
' And then turn it off again
Call xlApp.Application.Run("SAPSetRefreshBehaviour", "Off")
Call xlApp.Application.Run("SAPExecuteCommand", "PauseVariableSubmit", "On")
ElseIf cleanuptype = "CLEARFILTERS" Then
' Lets loop through our filters array and clear them out
For i = LBound(FiltersArray) To UBound(FiltersArray)
lresult = xlApp.Application.Run("SAPSetFilter", thedatasource, FiltersArray(i), "", "INPUT_STRING")
Next i
End If
End Sub
' ***************************************************************************
' At this point we now have a refreshed file with all the new variables and filters.
' Now you can save/saveas/email here before you move on to the next loop.
' ***************************************************************************
' Now lets see if there is an action we need to perform on this loop.
' Similar to what we did for the filters, lets do the same for the Action.
For i = LBound(paramatersarray) To UBound(paramatersarray)
Action_Loopnum = paramatersarray(i, 1)
Action_DataSource = paramatersarray(mainloop, 2)
Action_Type = paramatersarray(mainloop, 3)
Action_Field = paramatersarray(mainloop, 4)
Action_Value = paramatersarray(mainloop, 5)
If Action_Loopnum = Field_Loopnum And Filter_Type = "ACTION" Then
' lets read all the action stuff and do something with it.
Action_Action = paramatersarray(i, 6)
Action_NewFilename = paramatersarray(i, 7)
Action_AddDate = paramatersarray(i, 😎
Action_FileType = paramatersarray(i, 9)
Action_EmailType = paramatersarray(i, 10)
Action_EmailDisplay = paramatersarray(i, 11)
Action_EmailAddress = paramatersarray(i, 12)
Action_EmailSubject = paramatersarray(i, 13)
Action_EmailMessage = paramatersarray(i, 14)
' now this is where you make use of the fields and call the relevant saving and emailing subroutines
End If
Next i
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
5 | |
5 | |
4 | |
4 | |
4 | |
4 | |
4 | |
4 | |
3 | |
3 |