我开发了一个宏来搜索SAP(SAP GUI)中的一些信息并将其复制到excel工作表中,但遗憾的是,只有在我打开一个会话时它才有效。 即使我尝试通过代码(“session.createSession”)创建新会话,接下来的步骤(重要的步骤)也是在之前打开的第一个会话中完成的,例如,它在文本框中输入代码在该屏幕中不存在并且作为连续性它似乎是错误的。 我知道我可以通过在运行此宏之前关闭所有会话来解决这个问题,但我确实认为有更好的选择。希望你们能帮助我。
这是宏:
> Sub SAP()
'Macro desarrollada por Marketing
Dim application
Dim connection
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
Sheets("Carga").Select
Range("A2", Range("P" & Rows.Count).End(xlUp).Address).Sort Key1:=[K2], Order1:=xlAscending, Header:=xlYes
Sheets("Carga").Select
Range("a2").Select
Selection.Copy
Sheets("Consulta").Select
Range("c5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Carga").Select
Range("m2").Select
Selection.Copy
Sheets("Consulta").Select
Range("c6").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Código y producto
Sheets("Consulta").Select
Range("B9").Select
Sheets("Carga").Select
Range("b2:C2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Consulta").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'lote y paquete
Sheets("Carga").Select
Range("e2:F2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Consulta").Select
Range("g9").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Peso y Unidad
Sheets("Carga").Select
Range("G2:H2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Consulta").Select
Range("i9").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Entrega
Sheets("Carga").Select
Range("k2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Consulta").Select
Range("D9").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Sheets("Carga").Select
' Range("M2").Select
' Range(Selection, Selection.End(xlDown)).Select
' Selection.Copy
'Sheets("Consulta").Select
'Range("I9").Select
'Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Copiar todos los valores de las remesas y eliminar los que se repiten
Sheets("Consulta").Select
Range("d9").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("BusquedaSAP").Select
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Eliminar las remesas duplicadas
ActiveSheet.Range("A:A").RemoveDuplicates Columns:=1, Header:=xlNo
'Comenzar la búsqueda de Factura SAP a partir de las remesas
Sheets("BusquedaSAP").Select
For K = 2 To Cells(Rows.Count, 1).End(xlUp).Row
'Si no hay datos, parar la macro
If IsEmpty(Range("A" & K)) Then
'Volver a la página de inicio de SAP
session.findById("wnd[0]/tbar[0]/btn[12]").press
Exit Sub
Sheets("Consulta").Select
End If
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "VL03N"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtLIKP-VBELN").Text = Sheets("BusquedaSAP").Range("A" & K).Text
session.findById("wnd[0]").sendVKey 0
'Click en Flujo de Documentos(F7)
session.findById("wnd[0]/tbar[1]/btn[7]").press
'la segunda siempre es la remesa
session.findById("wnd[0]/usr/shell/shellcont[1]/shell[1]").selectItem " 2", "&Hierarchy"
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\07").Select
guiasider = session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\07/ssubSUBSCREEN_BODY:SAPMV50A:2110/txtLIKP-XBLNR").Text
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\07/ssubSUBSCREEN_BODY:SAPMV50A:2110/txtLIKP-XBLNR").SetFocus
Worksheets("BusquedaSAP").Range("C" & K) = guiasider
session.findById("wnd[0]/tbar[0]/btn[3]").press
session.findById("wnd[0]/tbar[0]/btn[3]").press
'Click en Botón buscar
session.findById("wnd[0]/usr/shell/shellcont[1]/shell[0]").pressButton "&FIND"
'Ingresar "Documento Contable en el campo de búsqueda
session.findById("wnd[1]/usr/txtLVC_S_SEA-STRING").Text = "GT - Factura"
'Dar click en aceptar varias veces
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[1]").Close
session.findById("wnd[0]/tbar[1]/btn[8]").press
'Buscar numero de Factura SAP
facturaSAP = session.findById("wnd[0]/usr/ctxtVBRK-VBELN").Text
session.findById("wnd[0]/usr/ctxtVBRK-VBELN").SetFocus
Worksheets("BusquedaSAP").Range("B" & K) = facturaSAP
session.findById("wnd[0]/tbar[0]/btn[3]").press
'Si es la primera remesa que busque todos los datos
If K = 2 Then
'Click en la lupa
session.findById("wnd[0]/usr/shell/shellcont[1]/shell[1]").selectItem " 1", "&Hierarchy"
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/usr/subSUBSCREEN_HEADER:SAPMV45A:4021/btnBT_HEAD").press
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\07").Select
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\07/ssubSUBSCREEN_BODY:SAPMV45A:4352/subSUBSCREEN_PARTNER_OVERVIEW:SAPLV09C:1000/cmbGV_FILTER").SetFocus
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\07/ssubSUBSCREEN_BODY:SAPMV45A:4352/subSUBSCREEN_PARTNER_OVERVIEW:SAPLV09C:1000/cmbGV_FILTER").Key = "PAREH"
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\07/ssubSUBSCREEN_BODY:SAPMV45A:4352/subSUBSCREEN_PARTNER_OVERVIEW:SAPLV09C:1000/tblSAPLV09CGV_TC_PARTNER_OVERVIEW/txtGVS_TC_DATA-REC-NAME1[2,0]").SetFocus
session.findById("wnd[0]").sendVKey 2
'Tomar valor de cliente
cliente = session.findById("wnd[1]/usr/subGCS_ADDRESS:SAPLSZA1:0300/subCOUNTRY_SCREEN:SAPLSZA1:0301/txtADDR1_DATA-NAME1").Text
session.findById("wnd[1]/usr/subGCS_ADDRESS:SAPLSZA1:0300/subCOUNTRY_SCREEN:SAPLSZA1:0301/txtADDR1_DATA-NAME1").SetFocus
cliente1 = session.findById("wnd[1]/usr/subGCS_ADDRESS:SAPLSZA1:0300/subCOUNTRY_SCREEN:SAPLSZA1:0301/txtADDR1_DATA-NAME2").Text
session.findById("wnd[1]/usr/subGCS_ADDRESS:SAPLSZA1:0300/subCOUNTRY_SCREEN:SAPLSZA1:0301/txtADDR1_DATA-NAME2").SetFocus
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\07/ssubSUBSCREEN_BODY:SAPMV45A:4352/subSUBSCREEN_PARTNER_OVERVIEW:SAPLV09C:1000/tblSAPLV09CGV_TC_PARTNER_OVERVIEW/txtGVS_TC_DATA-REC-NAME1[2,3]").SetFocus
session.findById("wnd[0]").sendVKey 2
destinatario = session.findById("wnd[1]/usr/subGCS_ADDRESS:SAPLSZA1:0300/subCOUNTRY_SCREEN:SAPLSZA1:0301/txtADDR1_DATA-NAME1").Text
session.findById("wnd[1]/usr/subGCS_ADDRESS:SAPLSZA1:0300/subCOUNTRY_SCREEN:SAPLSZA1:0301/txtADDR1_DATA-NAME1").SetFocus
destinatario1 = session.findById("wnd[1]/usr/subGCS_ADDRESS:SAPLSZA1:0300/subCOUNTRY_SCREEN:SAPLSZA1:0301/txtADDR1_DATA-NAME2").Text
session.findById("wnd[1]/usr/subGCS_ADDRESS:SAPLSZA1:0300/subCOUNTRY_SCREEN:SAPLSZA1:0301/txtADDR1_DATA-NAME2").SetFocus
session.findById("wnd[0]/tbar[0]/btn[12]").press
session.findById("wnd[0]/tbar[0]/btn[12]").press
'tomar valor de empresa de transportes
session.findById("wnd[0]/usr/shell/shellcont[1]/shell[1]").selectItem " 3", "&Hierarchy"
session.findById("wnd[0]/tbar[1]/btn[8]").press
'En esta parte se selecciona la empresa de transporte, pero no todos lo tienen y arroja un error, por eso no se considera
'session.findById("wnd[0]/usr/tabsHEADER_TABSTRIP1/tabpTABS_OV_PR/ssubG_HEADER_SUBSCREEN1:SAPMV56A:1021/btnB_PR_ADR").press
'empresatransporte = session.findById("wnd[1]/usr/txtADDR1_DATA-NAME1").Text
'session.findById("wnd[1]/usr/txtADDR1_DATA-NAME1").SetFocus
'empresatransporte1 = session.findById("wnd[1]/usr/txtADDR1_DATA-NAME2").Text
'session.findById("wnd[1]/usr/txtADDR1_DATA-NAME2").SetFocus
'session.findById("wnd[0]/tbar[0]/btn[12]").press
'Selecciona el nombre del transportista
transportista = session.findById("wnd[0]/usr/tabsHEADER_TABSTRIP1/tabpTABS_OV_PR/ssubG_HEADER_SUBSCREEN1:SAPMV56A:1021/ctxtVTTK-EXTI1").Text
session.findById("wnd[0]/usr/tabsHEADER_TABSTRIP1/tabpTABS_OV_PR/ssubG_HEADER_SUBSCREEN1:SAPMV56A:1021/ctxtVTTK-EXTI1").SetFocus
Worksheets("BusquedaSAP").Range("d2") = cliente & " " & cliente1
Worksheets("BusquedaSAP").Range("e2") = destinatario & " " & destinatario1
Worksheets("BusquedaSAP").Range("f2") = empresatransporte & " " & empresatransporte1
Worksheets("BusquedaSAP").Range("g2") = transportista
session.findById("wnd[0]/tbar[0]/btn[15]").press
session.findById("wnd[0]/tbar[0]/btn[15]").press
session.findById("wnd[0]/tbar[0]/btn[15]").press
End If
session.findById("wnd[0]/tbar[0]/btn[3]").press
session.findById("wnd[0]/tbar[0]/btn[12]").press
session.findById("wnd[0]/tbar[0]/btn[12]").press
Next K
Sheets("Consulta").Activate
Range("e9").Select
Selection.Copy
For i = 9 To Cells(Rows.Count, 4).End(xlUp).Row
If Range("B" & i) <> Empty Then
If i = 9 Then
Range("A" & i) = 1
Else
Range("A" & i) = Range("A" & i - 1) + 1
Range("e9").Select
Selection.Copy
Range("E" & i).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Range("f9").Select
Selection.Copy
Range("f" & i).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
End If
End If
Next i
Sheets("Consulta").Activate
Range("O16").Select
Selection.Copy
Range(Cells(Rows.Count, 5).End(xlUp).Offset(1, -4), Cells(39, 13)).Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("a9:m9").Select
Selection.Copy
Range(Cells(10, 1), Cells(Rows.Count, 5).End(xlUp).Offset(0, 8)).Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("a10").Select
'application.CutCopyMode = False
End Sub
干杯
答案 0 :(得分:0)
您可以测试以下解决方法:
> Sub SAP()
'Macro desarrollada por Marketing
Dim application
Dim connection
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
msgbox "Please select a session within the next 5 seconds"
wscript.sleep 5000
set session = application.ActiveSession
if session is nothing then Set session = connection.Children(0)
Sheets("Carga").Select
. . .
您也可能对此链接感兴趣:
https://archive.sap.com/discussions/thread/1730563
此致 ScriptMan