on 03-11-2013 2:13 AM
Greetings,
I am having an issue with using VBS to place text within shell container. Transaction CG12, create a phrase, click phrase item, then double click on your entered phrase text to open the long text editor. Trying to make script write to this word application within the SAP GUI, haven't had much luck. If you're a script master please assist. A simple subset of the code is below, simply get the focus within the window to set text using hard coded text or a variable.
Can run this script after navigating to CG12 if you like.
Sub Phrase_Load()
If Not IsObject(App) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set App = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = App.Children(0)
End If
If Not IsObject(session) Then
Set session = Connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject App, "on"
End If
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[1]/btn[18]").press
session.findById("wnd[0]/usr/ctxtRCGPHIOT-PHRGRP").Text = "GRAPHIC"
session.findById("wnd[0]/usr/txtRCGPHIOT-PHRTEXT").Text = "Jelly beans on the sidewalk"
session.findById("wnd[0]/usr/txtRCGPHIOT-PHRTEXT").SetFocus
session.findById("wnd[0]/usr/txtRCGPHIOT-PHRTEXT").caretPosition = 5
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[1]/btn[6]").press
session.findById("wnd[0]/usr/tblSAPLC115LG_IOTAB_CTR/txtRCGPPIOT-PHRTEXT[1,3]").SetFocus
session.findById("wnd[0]/usr/tblSAPLC115LG_IOTAB_CTR/txtRCGPPIOT-PHRTEXT[1,3]").caretPosition = 23
session.findById("wnd[0]").sendVKey 2
container_text = session.findById("wnd[0]/usr/cntlSCMSW_CONTAINER_2102/shellcont/shell").Text
'session.findById("wnd[0]/usr/cntlSCMSW_CONTAINER_2102/shellcont/shell").setDocument 1, "SAP.DocumentContainerControl.1"
End Sub
Hello Kirk.
I had just an similar issue for add longtext on purchasing inforecords. As this longtext is convert to an HEX-format which include the whole page format it´s not possible to add them via scripting.
I have written an Excel VBA script which call functionmodule RFC_SAVE_TEXT and upload longtext for specific request.
As the different objects are seperated by TDOBJECTS and TDID you just need to find out how the TDNAME for CG12 phrases longtext is setup.
TDOBJECT TDLINESIZE TDTEXT
C_SHES_TPP 72 EHS: Long Text for Phrase
TDOBJECT TDID TDTEXT
C_SHES_TPP 0001 EHS: Text ID for Phrases
C_SHES_TPP 0002 EHS: Formatted Phrase Texts
Setup data on first sheet
1.Column => TDOBJECT (C_SHES_TPP)
2.Column => TDNAME
3.Column => Lanuage (EN)
4.Column => TDID (0001 or 0002)
5.Column => your text per line for one object (72 character) if you have more lines add more lines with same TDNAME
If you got any messages this will populated on third sheet. On second sheet it will store your logon-data. Delete this before you use it again. Test first in an sandbox-system. This function require RFC-access for user who execute it.
We have an SSO logon in our environment. If you use user/password combination you need to modify SAP Logon section for your system environment.
__________________________________________________________________
Public Sub RFC_SAVE_TEXT()
'******************************************************************************************
'* Declaration *
'******************************************************************************************
Dim Destination_System As Integer
Dim objBAPIControl As Object 'Function Control (Collective object)
Dim sapConnection As Object 'Connection object
Dim objRfcSaveText As Object
Dim tblData
Dim intRow%
Dim tblData_RowsCount%
Dim tblReturn
Dim ReturnFunc
Set objBAPIControl = CreateObject("SAP.Functions")
Set sapConnection = objBAPIControl.Connection
'******************************************************************************************
'* Count how many different items will changed *
'******************************************************************************************
With ThisWorkbook.Sheets(1)
tblData_RowsCount = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
'******************************************************************************************
'* RFC-Logon with initial values (Only SSO Logon supported by below configuration) *
'******************************************************************************************
If ThisWorkbook.Sheets(2).Cells(2, 1).Value <> "" Then
sapConnection.Client = ThisWorkbook.Sheets(2).Cells(2, 1).Value
sapConnection.User = ThisWorkbook.Sheets(2).Cells(2, 2).Value
sapConnection.SNC = ThisWorkbook.Sheets(2).Cells(2, 3).Value
sapConnection.SNCName = ThisWorkbook.Sheets(2).Cells(2, 4).Value
sapConnection.SNCQuality = ThisWorkbook.Sheets(2).Cells(2, 5).Value
sapConnection.ApplicationServer = ThisWorkbook.Sheets(2).Cells(2, 6).Value
sapConnection.SystemNumber = ThisWorkbook.Sheets(2).Cells(2, 7).Value
sapConnection.System = ThisWorkbook.Sheets(2).Cells(2, 8).Value
sapConnection.Reconnect
Else
If sapConnection.logon(1, False) <> True Then
MsgBox "No connection to R/3!"
Exit Sub 'End program
Else
'******************************************************************************************
'* RFC-Logon with stored values *
'******************************************************************************************
ThisWorkbook.Sheets(2).Cells(2, 1).Value = sapConnection.Client
ThisWorkbook.Sheets(2).Cells(2, 2).Value = sapConnection.User
ThisWorkbook.Sheets(2).Cells(2, 3).Value = sapConnection.SNC
ThisWorkbook.Sheets(2).Cells(2, 4).Value = sapConnection.SNCName
ThisWorkbook.Sheets(2).Cells(2, 5).Value = sapConnection.SNCQuality
ThisWorkbook.Sheets(2).Cells(2, 6).Value = sapConnection.ApplicationServer
ThisWorkbook.Sheets(2).Cells(2, 7).Value = sapConnection.SystemNumber
ThisWorkbook.Sheets(2).Cells(2, 8).Value = sapConnection.System
End If
End If
'******************************************************************************************
'* prepare call of FM RFC_SAVE_TEXT *
'******************************************************************************************
Set objRfcSaveText = objBAPIControl.Add("RFC_SAVE_TEXT")
Set tblData = objRfcSaveText.tables("TEXT_LINES")
Set tblReturn = objRfcSaveText.tables("MESSAGES")
'******************************************************************************************
'* prepare data for execution FM RFC_SAVE_TEXT *
'******************************************************************************************
For intRow = 2 To tblData_RowsCount
tblData.Rows.Add
tblData.Value((intRow - 1), "TDOBJECT") = ThisWorkbook.Sheets(1).Cells(intRow, 1).Value
tblData.Value((intRow - 1), "TDNAME") = ThisWorkbook.Sheets(1).Cells(intRow, 2).Value
tblData.Value((intRow - 1), "TDID") = ThisWorkbook.Sheets(1).Cells(intRow, 3).Value
tblData.Value((intRow - 1), "TDSPRAS") = ThisWorkbook.Sheets(1).Cells(intRow, 4).Value
tblData.Value((intRow - 1), "TDLINE") = ThisWorkbook.Sheets(1).Cells(intRow, 5).Value
Next
ReturnFunc = objRfcSaveText.Call
If ReturnFunc = True Then
If tblReturn.RowCount > 0 Then
For intRow = 1 To tblReturn.RowCount
ThisWorkbook.Sheets(3).Cells((intRow + 1), 1).Value = tblData(intRow, "TYPE")
ThisWorkbook.Sheets(3).Cells((intRow + 1), 1).Value = tblData(intRow, "MESSAGE")
Next
ThisWorkbook.Sheets(3).Activate
End If
End If
'******************************************************************************************
'* clear tblData and tblReturn *
'******************************************************************************************
Do Until tblData.RowCount = 0
Call tblData.Rows.Remove(1)
Loop
Do Until tblReturn.RowCount = 0
Call tblReturn.Rows.Remove(1)
Loop
'******************************************************************************************
'* Close connection to R/3 ! *
'******************************************************************************************
objBAPIControl.Connection.logoff
'******************************************************************************************
'* Release the objects to free storage space *
'******************************************************************************************
Set sapConnection = Nothing
Set objRfcSaveText = Nothing
Set tblData = Nothing
Set tblReturn = Nothing
End Sub
_______________________________________________________________________
Hope this give some help how you can achieve your goal.
Best regards,
Holger
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Dear Kirk,
One way you can try is using Appactivate and SendKeys i.e.
Appactivate("Change text") 'the windows title bar text.
SendKeys "the text to paste"
Thanks.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.