Purpose
You want to import data from a Microsoft Project file to a SAP transaction.
Solution
Microsoft offers an interface for the Office products, which you can address with the GuiXT Controls.
Please note that Microsoft Project must be installed on the respective PC in this case.
You can address the interface either via JavaScript (callJS command) or via a .NET extension (callVB command). We describe both possibilities below, each of which has advantages and disadvantages.
In this example, we want to import data from Microsoft Project to the SAP Project Builder (CJ20):
Import data from Microsoft project:
The InputScript will import all tasks from a .MPP file and puts the data into the SAP table.
In this example, we used the template "simple project plan" offered by MS Project.
Using a JavaScript call:
Call the following InputScript, which in this example inserts all tasks from the MS Project file:
GuiXT
CreateTable V[tasktable] taskname taskstart taskend
// Read tasks from MS Project file
callJS gettasks "C:\data\simple_project.mpp" "tasktable"
Set V[k] 1
label nextrow
if not V[k>&V[tasktable.rowcount]]
Set cell[table,RCWBT-PIDEN,&V[k]] "&V[tasktable.taskname.&V[k]]"
Set cell[table,RCWBT-STERM,&V[k]] "&V[tasktable.taskstart.&V[k]]"
Set cell[table,RCWBT-ETERM,&V[k]] "&V[tasktable.taskend.&V[k]]"
Set V[k] "&V[k]" + 1
goto nextrow
endif
if V[r]
message "E: &V[r]" -statusline
else
message "Tasks imported from MS Project" -statusline
endif
The method "gettasks" must be present in the JavaScript library specified in the GuiXT profile and looks like this:
JavaScript
Using a .NET library:
The InputScript from above just slightly changes: The JavaScript call is replaced by a call to the function in the guixt_office.dll file:
'''
''' Get all task in a MS Project file
'''
''' Path to the MS project file
''' Name of GuiXT longtext variable '''
''' empty string if success, errortext otherwise
Public Function GetTasks(filename As String, output As String) As String
Dim returnText As String = ""
Try
'Create the Application object to interact with MS project
Dim projectApplication As
Microsoft.Office.Interop.MSProject.Application =
New Microsoft.Office.Interop.MSProject.Application()
Dim mv As Object = System.Reflection.Missing.Value
projectApplication.FileOpenEx(filename, mv, mv,
mv, mv, mv, mv,
mv, mv, mv, mv,
Microsoft.Office.Interop.MSProject.PjPoolOpen.pjPoolReadOnly,
mv, mv,
mv, mv, mv)
' Get the active project in the file
Dim project As Microsoft.Office.Interop.MSProject.Project _
= projectApplication.ActiveProject
' Define a list to put the tasks into
Dim tasks As ArrayList = New ArrayList
' Hint:
' We create a list containing rows of dictionary objects
' A dictionary stores key-value pairs
'
' We can convert this to JSON notation later
For Each task As Microsoft.Office.Interop.MSProject.Task
In project.Tasks
If task IsNot Nothing Then
' Grab some of the information of this task and save it
Dim td As New Dictionary(Of String, String)
td.Item("taskname") = task.Name.ToString
td.Item("taskstart") = task.Start.ToString
td.Item("taskend") = task.Finish.ToString
tasks.Add(td)
End If
Next
' Convert the list of tasks to JSON format
Dim serializer As New JavaScriptSerializer()
Dim arrayJSON As String = serializer.Serialize(tasks)
' Save the data into a GuiXT table variable
myguixt.SetVariable(output, arrayJSON)
' Hint: If you use createTable in GuiXT, you can use JSON
' to exchange data. All rows are automatically updated by GuiXT
Catch ex As Exception
' Something went wrong: Return the message to GuiXT
' so it can be shown on the SAP transaction
'
' e.g. file was not found
returnText = ex.Message
End Try
Return returnText
End Function
Comparison between JavaScript and .NET
Using JavaScript is possible without much effort, but it has the following disadvantage: you have to figure out the methods you need and their parameters, and you have few tools or testing options available. You can therefore also create a .NET library,
have tools such as code completion available there and can first test independently of GuiXT and the SAP transaction.