Skip to Content

Debug OpenConnection function

Any ideas how to debug OpenConnection

i.e.

'connect to target SAP System 
Set SAPConn = SAPApp.OpenConnection(System)

I am getting an error when connecting to a target system, however the error is throwing up the following

Hostname '/SYSID=ABC    /H/(ipaddresshere)/S/(porthere)' unknown

when using OpenConnectionByConnectionString & declaring ''/H/(ipaddresshere)/S/(porthere)"

it will connect as expected.

The System ID in the error is the declared System, & the host appears correct apart from the leading "/SYSID=ABC" component of the string.

Any ideas why the OpenConnection fucntion is failing?

Add comment
10|10000 characters needed characters exceeded

  • Hello Daniel,

    I have made the same experience, that is the reason why I use always OpenConnectionByConnectionString. Unfortunately I don't found a reason for this behaviour.

    Best regards
    Stefan

  • Interesting, this is the 1st time I've encountered it. Lucky me. What method would you recommend to parse the expected connection string?

    I suppose parse the SAPUILandscape.xml & saplogon.ini directly & build my own connection string on the fly.

  • Hello Daniel,

    you are right. Actual I parse the SAPUILandscape.xml, formerly was it saplogon.ini.

    Best regards
    Stefan

  • Get RSS Feed

1 Answer

  • Best Answer
    Nov 15, 2017 at 01:53 AM

    I've crafted a connectionstring parser for the xml that will launch the connection to the expected SAP system, any suggestions on improvements gratefully received.

    Sample code below.

    Connector logic:

                'connect to target SAP System
                
                Set SAPConn = SAPApp.OpenConnection(system, True, True)
                
                If SAPConn Is Nothing Then
                    systemString = SAPUILandscape(system)
                    If systemString <> "" Then
                        Set SAPConn = SAPApp.OpenConnectionByConnectionString(systemString)
                    Else:
                        Call Show_Script_status(system & " Not Found")
                        Exit Sub
                    End If
                End If
                
                'Fill out SAP Logon screen
                Set session = SAPConn.Sessions(0)
                session.findById("wnd[0]/usr/txtRSYST-MANDT").text = clientID
                session.findById("wnd[0]/usr/txtRSYST-BNAME").text = userID
                session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = Password
    <br>

    SAPUILandscape function below:

    Function SAPUILandscape(system)
    
        Dim fpath As String
        Dim sapfiles As String
        Dim SAPUILandscapefile As String
        Dim ConnString As String
        Dim id As Variant
        Dim isValid As Boolean
        Dim testing As Boolean
    
        Dim AttribName As String
        Dim AttribServer As String
        Dim CurrentXpath As String
        
        Dim xmlDoc As MSXML2.DOMDocument60
        Dim xmlSelection As MSXML2.IXMLDOMSelection
        Dim xmlAttributes As MSXML2.IXMLDOMNamedNodeMap
        Dim xmlItems As MSXML2.IXMLDOMAttribute
    
        Set xmlDoc = New MSXML2.DOMDocument60
        testing = False
        
        'xml structures
        AttribName = "systemid"
        AttribServer = "server"
        'alt AttribTag = "systemid"
        CurrentXpath = "//Landscape/Services/Service[@" & AttribName & "='" & system & "']"
        
        'find SAPUILandscape.xml
        fpath = Environ$("AppData")
        sapfiles = "SAP\Common\"
        SAPUILandscapefile = "SAPUILandscape.xml"
    
        If Right(fpath, 1) <> "\" Then
            fpath = fpath & "\"
        End If
        fpath = fpath & sapfiles & SAPUILandscapefile
    
        xmlDoc.async = False
        
        If xmlDoc.Load(fpath) Then
            'successfully loaded xml
            Set xmlSelection = xmlDoc.SelectNodes(CurrentXpath)
            If xmlSelection.Length = 0 Then
                'path not found
                isValid = False
            Else:
                Set xmlAttributes = xmlSelection.Item(0).Attributes
                For Each xmlItems In xmlAttributes
                    If xmlItems.BaseName = AttribServer Then
                        ConnString = xmlItems.NodeValue
                        isValid = True
                        Exit For
                    End If
                Next xmlItems
    
            End If
        
        Else:
            If testing = True Then
                ' The document failed to load.
                Dim strErrText As String
                Dim xPE As IXMLDOMParseError
                ' Obtain the ParseError object
                Set xPE = xmlDoc.parseError
                With xPE
                    strErrText = "Your XML Document failed to load" & _
                      "due the following error." & vbCrLf & _
                      "Error #: " & .ErrorCode & ": " & xPE.reason & _
                      "Line #: " & .Line & vbCrLf & _
                      "Line Position: " & .linepos & vbCrLf & _
                      "Position In File: " & .filepos & vbCrLf & _
                      "Source Text: " & .srcText & vbCrLf & _
                      "Document URL: " & .Url
                End With
                
                MsgBox strErrText, vbExclamation
                isValid = False
            Else:
                isValid = False
    
            End If
            
        End If
    
        If isValid = True Then
            'connection found in landscape
            SAPUILandscape = formatConnString(ConnString)
          
        Else:
            'connection not found in landscape
            SAPUILandscape = ""
            
        End If
    
    End Function
    
    Function formatConnString(ConnectionString)
    
        Dim hostPrefix As String
        Dim host As String
        Dim id As Variant
        Dim portPrefix As String
        Dim port As String
        Dim delimiter As String
        
        hostPrefix = "/H/"
        portPrefix = "/S/"
        delimiter = ":"
        
        id = Split(ConnectionString, delimiter, -1, vbTextCompare)
    
        host = id(0)
        port = id(1)
    
        formatConnString = hostPrefix & host & portPrefix & port
        
    End Function
    
    
    <br>
    Add comment
    10|10000 characters needed characters exceeded