cancel
Showing results for 
Search instead for 
Did you mean: 

Is it possible to use RFC_READ_TEXT using existing connection??

michael_propst
Explorer
0 Kudos

Hello all!  I am new to VBA and know very little, your assistance is greatly apprecitated.

I am trying to create a macro that will copy long texts from a PIR.  The only way I have found to do it so far is using RFC_READ_TEXT.

My macro starts out using a SAP screen/connection that I already have open but when it gets to the RFC_READ_TEXT it wants me to sign in again.

Is there another way to call the read_text function module that will use my pre-existing conection?

Here is what I haave so far.

Sub pirCopier()

Dim Application, SapGuiAuto, Connection, session
Dim SID, bh1Wnd, CollCon, i, CollSes

Dim materialGroup
Dim vendor
Dim orderUnit
Dim sortTerm
Dim piDelivTime
Dim shippgInstr
Dim condition1
Dim condition2
Dim condition3
Dim condition4
Dim condition5
Dim description
Dim noteText
Dim poText


Set SapGuiAuto = GetObject("SAPGUI")
Set Application = SapGuiAuto.GetScriptingEngine
  Set CollCon = Application.Connections()
      If Not IsObject(CollCon) Then
        Exit Sub
      End If

'- Find R3 window -------------------------------------------
        For i = 0 To CollCon.Count() - 1

          Set Connection = Application.Children(CLng(i))
          If Not IsObject(Connection) Then
            Exit Sub
          End If

          Set CollSes = Connection.sessions()
          If Not IsObject(CollSes) Then
            Exit Sub
          End If
       
                    Set session = Connection.Children(0)
                    SID = session.info.systemname()
                   If Not IsObject(bh1Wnd) Then
                    If SID = "BH1" Then Set bh1Wnd = Application.Children(CLng(i)) 'R3
                   End If
        Next

If Not IsObject(bh1Wnd) Then
MsgBox "SAP R/3 window not found. Please open at least on session of SAP R/3."
Exit Sub
End If

'-------------- start pirCopier -------------

Set Connection = bh1Wnd
Set session = Connection.Children(0)

'open pir to be copied
session.findById("wnd[0]/tbar[0]/okcd").text = "/nME13"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/radRM06I-NORMB").Select
session.findById("wnd[0]/usr/ctxtEINA-LIFNR").text = ""
session.findById("wnd[0]/usr/ctxtEINA-MATNR").text = ""
session.findById("wnd[0]/usr/ctxtEINE-EKORG").text = "1001"
session.findById("wnd[0]/usr/ctxtEINE-WERKS").text = "0002"
session.findById("wnd[0]/usr/ctxtEINA-INFNR").text = Range("B2")
session.findById("wnd[0]/usr/radRM06I-NORMB").SetFocus
session.findById("wnd[0]").sendVKey 0

vendor = session.findById("wnd[0]/usr/ctxtEINA-LIFNR").text
desciption = session.findById("wnd[0]/usr/txtEINA-TXZ01").text
orderUnit = session.findById("wnd[0]/usr/ctxtEINA-MEINS").text
sortTerm = session.findById("wnd[0]/usr/txtEINA-SORTL").text
session.findById("wnd[0]/tbar[1]/btn[7]").press
piDelivTime = session.findById("wnd[0]/usr/txtEINE-APLFZ").text
shippgInstr = session.findById("wnd[0]/usr/ctxtEINE-EVERS").text
session.findById("wnd[0]/tbar[1]/btn[8]").press


'--- start of copy long texts ---------------------------------------------

Set funcControl = VBA.CreateObject("SAP.Functions")
Set RFC_READ_TEXT = funcControl.Add("RFC_READ_TEXT") '<------------ This is where it wants me to re sign in
Set tblText_Lines = RFC_READ_TEXT.Tables("TEXT_LINES")

'----- copy po long

text from pir ---------

tblText_Lines.AppendRow
tblText_Lines(1, "TDOBJECT") = "EINE"
tblText_Lines(1, "TDNAME") = Range("B2") & "100100002"
tblText_Lines(1, "TDID") = "BT" 'PO text

If RFC_READ_TEXT.Call = True Then
   
        If tblText_Lines.RowCount > 0 Then
   
            For intRow = 1 To tblText_Lines.RowCount ' Change Next line to write a different header row
       
                If intRow = 1 Then
                poText = tblText_Lines(intRow, "TDLINE")
                Else
                poText = poText & vbCrLf & tblText_Lines(intRow, "TDLINE")
                End If
            Next
        Else
       
            GoTo poTextEnd
   
        End If

    Else

        MsgBox "ERROR CALLING SAP REMOTE FUNCTION CALL FOR PO TEXT"

    End If
   
    'Result Table set back
Do Until tblText_Lines.RowCount = 0
     Call tblText_Lines.rows.Remove(1)
Loop

poTextEnd:


End Sub

Accepted Solutions (0)

Answers (1)

Answers (1)

former_member213011
Participant
0 Kudos

The GetObject("SAPGUI") and CreateObject("SAP.FUnction") handles automation process for different connection methods. The former is for front end automation i.e. script for the SAP interface, while the latter is direct database query via remote function call (RFC). You don't need the former in order to perform the latter, vice versa. Both are independent of each other.

I have no experience at all with RFC hence will not be able to help you much with this function.