on 05-23-2014 4:42 PM
Hi - forgive me for this is so elementary! I need my gui script to clear out for the next number that I paste for the next run. Can it be done in SAP or do I need to go to VBS script?
Thanks for any help.
***************************************************************************************************************************************************************************
Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
Set connection = application.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 application, "on"
End If
session.findById("wnd[0]").maximize
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").doubleClickNode "F00221"
session.findById("wnd[0]/usr/ctxtDRAW-DOKNR").text = "H14538501"
session.findById("wnd[0]/usr/ctxtDRAW-DOKAR").text = "DRW"
session.findById("wnd[0]/usr/ctxtDRAW-DOKTL").text = "001"
session.findById("wnd[0]/usr/ctxtDRAW-DOKVR").text = "00"
session.findById("wnd[0]/usr/ctxtMCDOK-REFNR").text = "H14538501"
session.findById("wnd[0]/usr/ctxtMCDOK-REFTL").text = "000"
session.findById("wnd[0]/usr/ctxtMCDOK-REFVR").text = "00"
session.findById("wnd[0]/usr/ctxtMCDOK-REFNR").setFocus
session.findById("wnd[0]/usr/ctxtMCDOK-REFNR").caretPosition = 9
session.findById("wnd[0]/tbar[0]/btn[0]").press
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]/usr/tabsTAB_MAIN/tabpTSMAIN/ssubSCR_MAIN:SAPLCV110:0102/txtDRAT-DKTXT").text = "145-385-01.1"
session.findById("wnd[0]/usr/tabsTAB_MAIN/tabpTSMAIN/ssubSCR_MAIN:SAPLCV110:0102/ctxtTDWST-STABK").text = "FR"
session.findById("wnd[0]/usr/tabsTAB_MAIN/tabpTSMAIN/ssubSCR_MAIN:SAPLCV110:0102/ctxtTDWST-STABK").setFocus
session.findById("wnd[0]/usr/tabsTAB_MAIN/tabpTSMAIN/ssubSCR_MAIN:SAPLCV110:0102/ctxtTDWST-STABK").caretPosition = 2
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[1]").sendVKey 0
session.findById("wnd[0]/tbar[0]/btn[11]").press
session.findById("wnd[0]/usr/ctxtMCDOK-REFVR").caretPosition = 2
Hi Cynthia,
if you wonna transform the document number from
"H14538501" to "145-385-01.1" you can do it in vbs like this:
. . .
session.findById("wnd[0]").maximize
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").doubleClickNode "F00221"
Do
DokuNr = Inputbox("Enter Dokument Number:", "Dokumentnumber")
DokuNrLn = Len(DokuNr)
Loop Until (DokuNrLn = 9) and (isnumeric(Right(DokuNr, 8)) = true) and (isnumeric(left(DokuNr, 1) = False))
'REMARK: the LOOP wasn't testet
session.findById("wnd[0]/usr/ctxtDRAW-DOKNR").text = DokuNr
session.findById("wnd[0]/usr/ctxtDRAW-DOKAR").text = "DRW"
session.findById("wnd[0]/usr/ctxtDRAW-DOKTL").text = "001"
session.findById("wnd[0]/usr/ctxtDRAW-DOKVR").text = "00"
session.findById("wnd[0]/usr/ctxtMCDOK-REFNR").text = DokuNr
session.findById("wnd[0]/usr/ctxtMCDOK-REFTL").text = "000"
. . .
'for your control you can show the dokument number parts
'to check the right positions of the string:
str = str & "Length: " & Len(DokuNr) & vbcr
str = str & "Letter: " & Left(DokuNr, 1) & vbcr
str = str & "Part 1: " & mid(DokuNr, 2, 3) & vbcr
str = str & "Part 2: " & mid(DokuNr, 4, 3) & vbcr
str = str & "Part 3: " & Right(DokuNr, 2)
Msgbox str, vbsystemmodal + vbinformation, "Result"
NewDokuNr = mid(DokuNr, 2, 3) & "-" & mid(DokuNr, 4, 3) & "-" & Right(DokuNr, 2) & ".1"
. . .
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]/usr/tabsTAB_MAIN/tabpTSMAIN/ssubSCR_MAIN:SAPLCV110:0102/txtDRAT-DKTXT").text = NewDokuNr
session.findById("wnd[0]/usr/tabsTAB_MAIN/tabpTSMAIN/ssubSCR_MAIN:SAPLCV110:0102/ctxtTDWST-STABK").text = "FR"
. . .
Tell us how it works, please.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I have also a question related to SAP Script.
would you please take a look at my coding:
Option explicit 'Requires declaration of variables
Dim SapGuiAuto, application, connection, session, WScript
Dim sDebug 'set to vbYes for Debug on, or to vbNo for Debug off
sDebug = vbNo
'-----------------------------------------------------------------------------------
'Declaration of variables to read form the Excel Sheet
' and initiate connection to an Excel sheet
'-----------------------------------------------------------------------------------
Const constValueNameIndex = 0, constValueIndex = 1
Dim tSheetRowValues()
Dim oExcel, oSheet
Const constSheetStartRow = 3 '3 for two header rows. Or more for more header rows
Const constSheetStartCol = 2 '2 to start at second column from the left, or more to start from other columns
Const constExcelFileName = "" 'Leave blank ("") to link the active Workbook 'Ex: "C:\Temp\MM03 Display Material.xls"
Const constSheetName = "" 'Leave blank ("") to link the active Workbook's Worksheet 'Ex: "MM03 Display Material"
Call ExcelConnection(oExcel, oSheet)
Dim iRow, iCol, i
iRow = constSheetStartRow
iCol = constSheetStartCol
Dim Var1, Var2, Var3, Var4, Var5 , var6 ,var7
For iRow = constSheetStartRow To oSheet.UsedRange.Rows.Count
SetSheetRowValues(iRow)
Var1 = GetSheetRowValue("V1")
Var2 = GetSheetRowValue("V2")
Var3 = GetSheetRowValue("V3")
Var4 = GetSheetRowValue("V4")
Var5 = GetSheetRowValue("V5")
Var6 = GetSheetRowValue("V6")
Var7 = GetSheetRowValue("V7")
' copy SAP RECORDED:
If Not IsObject(application) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
Set connection = application.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 application, "on"
End If
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").text = "/ncv01n"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtDRAW-DOKNR").text = Var1
session.findById("wnd[0]/usr/ctxtDRAW-DOKAR").text = Var2
session.findById("wnd[0]/usr/ctxtDRAW-DOKTL").text = Var3
session.findById("wnd[0]/usr/ctxtDRAW-DOKVR").text = Var4
session.findById("wnd[0]/usr/ctxtDRAW-DOKVR").setFocus
session.findById("wnd[0]/usr/ctxtDRAW-DOKVR").caretPosition = 2
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[1]").sendVKey 2
session.findById("wnd[0]/usr/tabsTAB_MAIN/tabpTSMAIN/ssubSCR_MAIN:SAPLCV110:0102/txtDRAT-DKTXT").text = Var5
session.findById("wnd[0]/usr/tabsTAB_MAIN/tabpTSMAIN/ssubSCR_MAIN:SAPLCV110:0102/txtDRAT-DKTXT").caretPosition = 23
' session.findById("wnd[0]/usr/tabsTAB_MAIN/tabpTSMAIN/ssubSCR_MAIN:SAPLCV110:0102/txtPB_FILE_BROWSER").text=var6
' session.findById("wnd[0]/usr/tabsTAB_MAIN/tabpTSMAIN/ssubSCR_MAIN:SAPLCV110:0102/btnPB_FILE_BROWSER").text=var6
session.findById("wnd[0]/usr/tabsTAB_MAIN/tabpTSMAIN/ssubSCR_MAIN:SAPLCV110:0102/btnPB_FILE_BROWSER").press
session.findById("wnd[0]/tbar[0]/btn[11]").press
Call DebugMessage( "Message: " & session.findById("wnd[0]/sbar").text,sDebug )
'**************************** MM02 *********************************
If Not IsObject(application) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
Set connection = application.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 application, "on"
End If
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").text = "/nmm02"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtRMMG1-MATNR").text = var7
session.findById("wnd[0]/usr/ctxtRMMG1-MATNR").caretPosition = 8
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[1]/usr/tblSAPLMGMMTC_VIEW").getAbsoluteRow(5).selected = false
session.findById("wnd[1]/usr/tblSAPLMGMMTC_VIEW/txtMSICHTAUSW-DYTXT[0,5]").setFocus
session.findById("wnd[1]/usr/tblSAPLMGMMTC_VIEW/txtMSICHTAUSW-DYTXT[0,5]").caretPosition = 0
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]/tbar[1]/btn[30]").press
session.findById("wnd[0]/usr/tabsTABSPR1/tabpZU04").select
session.findById("wnd[0]/usr/tabsTABSPR1/tabpZU04/ssubTABFRA1:SAPLMGMM:2110/subSUB2:SAPLMGD1:3400/subDOCU:SAPLCV140:0204/tblSAPLCV140SUB_DOC/ctxtDRAW-DOKAR[0,1]").text = var2
session.findById("wnd[0]/usr/tabsTABSPR1/tabpZU04/ssubTABFRA1:SAPLMGMM:2110/subSUB2:SAPLMGD1:3400/subDOCU:SAPLCV140:0204/tblSAPLCV140SUB_DOC/ctxtDRAW-DOKNR[1,1]").text = var1
session.findById("wnd[0]/usr/tabsTABSPR1/tabpZU04/ssubTABFRA1:SAPLMGMM:2110/subSUB2:SAPLMGD1:3400/subDOCU:SAPLCV140:0204/tblSAPLCV140SUB_DOC/ctxtDRAW-DOKTL[2,1]").text = var3
session.findById("wnd[0]/usr/tabsTABSPR1/tabpZU04/ssubTABFRA1:SAPLMGMM:2110/subSUB2:SAPLMGD1:3400/subDOCU:SAPLCV140:0204/tblSAPLCV140SUB_DOC/ctxtDRAW-DOKVR[3,1]").text = var4
session.findById("wnd[0]/usr/tabsTABSPR1/tabpZU04/ssubTABFRA1:SAPLMGMM:2110/subSUB2:SAPLMGD1:3400/subDOCU:SAPLCV140:0204/tblSAPLCV140SUB_DOC/ctxtDRAW-DOKVR[3,1]").setFocus
session.findById("wnd[0]/usr/tabsTABSPR1/tabpZU04/ssubTABFRA1:SAPLMGMM:2110/subSUB2:SAPLMGD1:3400/subDOCU:SAPLCV140:0204/tblSAPLCV140SUB_DOC/ctxtDRAW-DOKVR[3,1]").caretPosition = 2
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[0]/btn[11]").press
session.findById("wnd[0]/tbar[0]/btn[12]").press
Call DebugMessage( "Message: " & session.findById("wnd[0]/sbar").text,sDebug )
Next
'===================================================================================
'Sub to output Debug message when required.
'===================================================================================
Sub DebugMessage(pMessage, pDebug)
'Call samples...
'Call DebugMessage( "",sDebug )
'Call DebugMessage( "",vbNo )
'Call DebugMessage( "Click Ok to continue if change is successful...",vbYes )
Dim sRes
If pDebug <> vbNo Then
If pMessage = "" Then
pMessage = "Debug pause. Click Yes to continue with Debug..."
Else
pMessage = pMessage & Chr(10) & Chr(10) & "Debug pause. Click Yes to continue with Debug..."
End if
sRes = Msgbox( pMessage,vbInformation+vbYesNoCancel+vbSystemModal,"Script current state information" )
If sRes = vbNo Then sDebug = vbNo
If sRes = vbCancel Then Err.Raise 1,,"Cancelled by user!"
End if
End sub
'===================================================================================
'Set up connection to the Excel data to be keyed into SAP:
'===================================================================================
Sub ExcelConnection(poExcel, poSheet)
If constExcelFileName = "" Then
Call DebugMessage( "Open Excel Workbook: [ActiveWorkbook] ...",sDebug )
Set poExcel = GetObject(, "Excel.Application")
Else
Call DebugMessage( "Open Excel Workbook: [" & constExcelFileName & "] ...",sDebug )
Set poExcel = GetObject(constExcelFileName)
End if
If constSheetName = "" Then
Call DebugMessage( "Open Excel Sheet: [ActiveSheet] ...",sDebug )
Set poSheet = poExcel.ActiveWorkbook.ActiveSheet
Else
Call DebugMessage( "Open Excel Sheet: [" & constSheetName & "] ...",sDebug )
Set poSheet = poExcel.Worksheets(constSheetName)
End if
poSheet.Activate
End sub
'===================================================================================
' Get all the Column values for the Row
'===================================================================================
Sub SetSheetRowValues(pRow)
Dim iCol, sSheetRowValuesTemp
For iCol = constSheetStartCol To oSheet.UsedRange.Columns.Count
'Call DebugMessage( "[" & iCol & "]",sDebug )
ReDim Preserve tSheetRowValues(constValueNameIndex+1,iCol-1) 'ReDim to 2 dimentional table of iCol by 2
tSheetRowValues(constValueNameIndex,iCol-1) = Trim(CStr(oSheet.Cells(constSheetStartRow-1, iCol).Value))
tSheetRowValues(constValueIndex,iCol-1) = Trim(CStr(oSheet.Cells(pRow, iCol).Value))
oSheet.Cells(pRow, iCol).Select
sSheetRowValuesTemp = sSheetRowValuesTemp & "(" & iRow & "," & iCol & ") " & _
tSheetRowValues(constValueNameIndex,iCol-1) & "=" & _
tSheetRowValues(constValueIndex,iCol-1) & Chr(10)
Next
Call DebugMessage( sSheetRowValuesTemp,6 )
End Sub
'===================================================================================
' Get the value as per the field description
'===================================================================================
Function GetSheetRowValue(psValueName)
Dim i
For i = constValueNameIndex To UBound(tSheetRowValues,2)
If tSheetRowValues(constValueNameIndex,i) = psValueName Then
GetSheetRowValue = tSheetRowValues(constValueIndex,i)
Call DebugMessage( tSheetRowValues(constValueNameIndex,i) & "=" & tSheetRowValues(constValueIndex,i),sDebug )
Exit Function
End If
Next
End Function
please let me know how can I change the bold line in order to accept data instead of calling dialog box for a file path ?
something like :
var = 'd: test.txt'
....= Var7
I want to give the file and it's path to an excel file , and force the sap to have the data from there not to ask from a dialog box during the runing time.
also if there is something wrong in my coding please let me know
thanks a lot
Mitra
Hi Cynthia,
It could be that I do not understand your question correctly. But for example, if you had the numbers in an Excel spreadsheet, then you might try the following:
If Not IsObject(application) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
Set connection = application.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 application, "on"
End If
Set xclapp = CreateObject("Excel.Application")
Set xclwbk = xclapp.Workbooks.Open("c:\tmp\myNumbers.xlsx")
set xclsht = xclwbk.Sheets("Sheet1")
for i = 2 to xclapp.ActiveCell.SpecialCells(11).Row
for j = 1 to xclapp.ActiveCell.SpecialCells(11).Column
if j=1 then number_1 = xclsht.Cells(i,j).Value
if j=2 then number_2 = xclsht.Cells(i,j).Value
next
'Here begins your SAP GUI script.
session.findById("wnd[0]").maximize
'session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").doubleClickNode "F00221"
'Here it would be better to start the transaction directly and not out of the SAP Easy Access Menu.
'for example:
session.findById("wnd[0]/tbar[0]/okcd").text = "/n" & "myTransaction"
'e.g. myTransaction = "MM03"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtDRAW-DOKNR").text = number_1
session.findById("wnd[0]/usr/ctxtDRAW-DOKAR").text = "DRW"
session.findById("wnd[0]/usr/ctxtDRAW-DOKTL").text = "001"
session.findById("wnd[0]/usr/ctxtDRAW-DOKVR").text = "00"
session.findById("wnd[0]/usr/ctxtMCDOK-REFNR").text = number_1
session.findById("wnd[0]/usr/ctxtMCDOK-REFTL").text = "000"
session.findById("wnd[0]/usr/ctxtMCDOK-REFVR").text = "00"
session.findById("wnd[0]/usr/ctxtMCDOK-REFNR").setFocus
'session.findById("wnd[0]/usr/ctxtMCDOK-REFNR").caretPosition = 9
'The above command is not strictly necessary.
session.findById("wnd[0]/tbar[0]/btn[0]").press
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]/usr/tabsTAB_MAIN/tabpTSMAIN/ssubSCR_MAIN:SAPLCV110:0102/txtDRAT-DKTXT").text = number_2
session.findById("wnd[0]/usr/tabsTAB_MAIN/tabpTSMAIN/ssubSCR_MAIN:SAPLCV110:0102/ctxtTDWST-STABK").text = "FR"
session.findById("wnd[0]/usr/tabsTAB_MAIN/tabpTSMAIN/ssubSCR_MAIN:SAPLCV110:0102/ctxtTDWST-STABK").setFocus
'session.findById("wnd[0]/usr/tabsTAB_MAIN/tabpTSMAIN/ssubSCR_MAIN:SAPLCV110:0102/ctxtTDWST-STABK").caretPosition = 2
'The above command is not strictly necessary.
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[1]").sendVKey 0
session.findById("wnd[0]/tbar[0]/btn[11]").press
session.findById("wnd[0]/usr/ctxtMCDOK-REFVR").caretPosition = 2
'The above command is not strictly necessary.
'Here is the end of your SAP GUI script.
next
msgbox "All " & cstr(xclapp.ActiveCell.SpecialCells(11).Row - 1) & " Excel rows have been processed."
Set xclwbk = Nothing
Set xclsht = Nothing
xclapp.Quit
set xclapp = Nothing
The Excel sheet named as myNumbers.xlsx could be as follows.
for example:
1. row: number_1 number_2
2. row H14538501 145-385-01.1
3. row H14538502 145-385-02.1
4. row etc.
Regards,
ScriptMan
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
79 | |
9 | |
9 | |
7 | |
7 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.