Code without label gives Compile error: Label not defined
Code without label gives Compile error: Label not defined
我正在尝试使用 Excel 中的按钮 运行 一个 SAP GUI 脚本,我创建脚本的方式是从 Excel 获取信息以用于SAP,该脚本效果很好,如果我打开它,它 运行 很顺利,但我无法将该脚本添加到 Excel 命令按钮。
这是有效的原始脚本:
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
'REM ADDED BY EXCEL *************************************
Dim objExcel
Dim objSheet, intRow, i
Set objExcel = GetObject(,"Excel.Application")
Set objSheet = objExcel.ActiveWorkbook.ActiveSheet
For i = 2 to objSheet.UsedRange.Rows.Count
COL1 = Trim(CStr(objSheet.Cells(i, 1).Value)) 'Column1
COL2 = Trim(CStr(objSheet.Cells(i, 2).Value)) 'Column2
'REM ADDED BY EXCEL *************************************
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").selectedNode = "F00028"
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").doubleClickNode "F00028"
session.findById("wnd[0]/usr/ctxtVBRK-VBELN").text = Col1
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/mbar/menu[2]/menu[0]/menu[3]").select
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3").getAbsoluteRow(0).selected = true
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/lblDV70A-STATUSICON[0,0]").setFocus
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/lblDV70A-STATUSICON[0,0]").caretPosition = 0
session.findById("wnd[0]/tbar[1]/btn[6]").press
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/cmbNAST-NACHA[3,0]").key = "1"
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/cmbNAST-NACHA[3,0]").setFocus
session.findById("wnd[0]/tbar[0]/btn[11]").press
session.findById("wnd[0]/usr/chkNAST-DELET").selected = true
session.findById("wnd[0]/usr/ctxtNAST-LDEST").text = "LOCALNEW"
session.findById("wnd[0]/usr/txtNAST-TDRECEIVER").text = Col2
session.findById("wnd[0]/usr/txtNAST-TDRECEIVER").setFocus
session.findById("wnd[0]/usr/txtNAST-TDRECEIVER").caretPosition = 8
session.findById("wnd[0]/tbar[0]/btn[3]").press
session.findById("wnd[0]/tbar[0]/btn[11]").press
session.findById("wnd[0]/tbar[0]/btn[3]").press
'REM FINALIZATION CONTROL CHECK ************************
aux=col1 & " " & col2
CreateObject("WScript.Shell").run("cmd /c @echo %date% %time% " & aux & " >> C:\SCRIPT\PlOrCreationLog.txt")
next
msgbox "Process Completed"
'REM FINALIZATION CONTROL CHECK ************************
如果我将此代码添加到 Excel VBA 它不起作用,所以我按照 this post 并将 'application' 更改为 'GuiApplication' 并且根据 post 答案给出的代码做了一些更改。
完成更改后,我得到了以下代码:
Private Sub CommandButton1_Click()
If Not IsObject(GuiApplication) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set GuiApplication = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = GuiApplication.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 GuiApplication, "on"
End If
If (Connection.Children.Count > 1) Then GoTo Err_TooManySAP
Set aw = session.ActiveWindow()
aw.findById("wnd[0]").Maximize
'REM ADDED BY EXCEL *************************************
Dim objExcel
Dim objSheet, intRow, i
Set objExcel = GetObject(, "Excel.Application")
Set objSheet = objExcel.ActiveWorkbook.ActiveSheet
For i = 2 To objSheet.UsedRange.Rows.Count
col1 = Trim(CStr(objSheet.Cells(i, 1).Value)) 'Column1
col2 = Trim(CStr(objSheet.Cells(i, 2).Value)) 'Column2
'REM ADDED BY EXCEL *************************************
On Error GoTo Err_Description
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").selectedNode = "F00028"
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").doubleClickNode "F00028"
session.findById("wnd[0]/usr/ctxtVBRK-VBELN").Text = col1
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/mbar/menu[2]/menu[0]/menu[3]").Select
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3").getAbsoluteRow(0).Selected = True
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/lblDV70A-STATUSICON[0,0]").SetFocus
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/lblDV70A-STATUSICON[0,0]").caretPosition = 0
session.findById("wnd[0]/tbar[1]/btn[6]").press
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/cmbNAST-NACHA[3,0]").Key = "1"
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/cmbNAST-NACHA[3,0]").SetFocus
session.findById("wnd[0]/tbar[0]/btn[11]").press
session.findById("wnd[0]/usr/chkNAST-DELET").Selected = True
session.findById("wnd[0]/usr/ctxtNAST-LDEST").Text = "LOCALNEW"
session.findById("wnd[0]/usr/txtNAST-TDRECEIVER").Text = col2
session.findById("wnd[0]/usr/txtNAST-TDRECEIVER").SetFocus
session.findById("wnd[0]/usr/txtNAST-TDRECEIVER").caretPosition = 8
session.findById("wnd[0]/tbar[0]/btn[3]").press
session.findById("wnd[0]/tbar[0]/btn[11]").press
session.findById("wnd[0]/tbar[0]/btn[3]").press
'REM FINALIZATION CONTROL CHECK ************************
aux = col1 & " " & col2
CreateObject("WScript.Shell").Run ("cmd /c @echo %date% %time% " & aux & " >> C:\SCRIPT\PlOrCreationLog.txt")
Next
MsgBox "Process Completed"
'REM FINALIZATION CONTROL CHECK ************************
End Sub
当我点击“调试”>“编译 VBA 项目时,弹出一条错误提示
Compile error: Label not defined
并突出显示代码中的一行 Set objExcel = GetObject(, "Excel.Application")
对我做错了什么有什么想法吗?我确定我做错了很多事情,我正在学习。我只需要在 Excel.
上单击按钮即可 运行 该脚本
获取 Excel 对象用于从不同主机自动执行 Excel。当您的主机是 Excel 时,Application
对象包含 Excel.Application
对象。
这段代码有很多问题。如果将 Option Explicit
放在顶部,您会发现您还没有声明所有变量。当你有时,它们没有类型。
您遇到了一些使用 GoTo
但没有匹配行标签的错误陷阱。您需要为每个 GoTo
设置行标签,并且还需要决定在这些错误事件中要做什么。注意 sub 末尾的行标签。它们以冒号 :
结尾。这里没有错误处理代码,sub 将结束。您通常希望至少警告用户错误。如果您喜欢冒险,可以就如何解决问题或尝试以编程方式解决问题提供反馈。
我已经修改了你的代码,所以它可以编译,但我不知道它是否符合你的要求:
Option Explicit
Dim SAPGuiApp As Object
Dim SapGuiAuto As Object
Dim Connection As Object
Dim SAP_session As Object
Dim WScript As Object
Dim aw As Object
Private Sub CommandButton1_Click()
If Not IsObject(SAPGuiApp) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set SAPGuiApp = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = SAPGuiApp.Children(0)
End If
If Not IsObject(SAP_session) Then
Set SAP_session = Connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject SAP_session, "on"
WScript.ConnectObject SAPGuiApp, "on"
End If
If (Connection.Children.Count > 1) Then GoTo Err_TooManySAP
Set aw = SAP_session.ActiveWindow()
aw.findById("wnd[0]").Maximize
Dim objSheet As Worksheet
Set objSheet = ActiveSheet
Dim i As Integer
For i = 2 To objSheet.UsedRange.Rows.Count
Dim col1 As String
col1 = Trim(CStr(objSheet.Cells(i, 1).Value)) 'Column1
Dim col2 As String
col2 = Trim(CStr(objSheet.Cells(i, 2).Value)) 'Column2
On Error GoTo Err_Description
With SAP_session
.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").selectedNode = "F00028"
.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").doubleClickNode "F00028"
.findById("wnd[0]/usr/ctxtVBRK-VBELN").Text = col1
.findById("wnd[0]").sendVKey 0
.findById("wnd[0]/mbar/menu[2]/menu[0]/menu[3]").Select
.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3").getAbsoluteRow(0).Selected = True
.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/lblDV70A-STATUSICON[0,0]").SetFocus
.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/lblDV70A-STATUSICON[0,0]").caretPosition = 0
.findById("wnd[0]/tbar[1]/btn[6]").press
.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/cmbNAST-NACHA[3,0]").Key = "1"
.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/cmbNAST-NACHA[3,0]").SetFocus
.findById("wnd[0]/tbar[0]/btn[11]").press
.findById("wnd[0]/usr/chkNAST-DELET").Selected = True
.findById("wnd[0]/usr/ctxtNAST-LDEST").Text = "LOCALNEW"
.findById("wnd[0]/usr/txtNAST-TDRECEIVER").Text = col2
.findById("wnd[0]/usr/txtNAST-TDRECEIVER").SetFocus
.findById("wnd[0]/usr/txtNAST-TDRECEIVER").caretPosition = 8
.findById("wnd[0]/tbar[0]/btn[3]").press
.findById("wnd[0]/tbar[0]/btn[11]").press
.findById("wnd[0]/tbar[0]/btn[3]").press
End With
'REM FINALIZATION CONTROL CHECK ************************
Dim aux As String
aux = col1 & " " & col2
CreateObject("WScript.Shell").Run ("cmd /c @echo %date% %time% " & aux & " >> C:\SCRIPT\PlOrCreationLog.txt")
Next
MsgBox "Process Completed"
'REM FINALIZATION CONTROL CHECK ************************
' Put error handling here
Err_Description:
Err_TooManySAP:
End Sub
@HackSLash
我尝试了不同的代码,这次我没有对当前可用的原始脚本进行太多更改,我添加了您与我共享的代码的最后一部分并且它起作用了。
Private Sub CommandButton1_Click()
If Not IsObject(GuiApplication) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set GuiApplication = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = GuiApplication.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 GuiApplication, "on"
End If
session.findById("wnd[0]").maximize
'REM ADDED BY EXCEL *************************************
Dim objExcel
Dim objSheet, intRow, i
Set objExcel = GetObject(, "Excel.Application")
Set objSheet = objExcel.ActiveWorkbook.ActiveSheet
For i = 2 To objSheet.UsedRange.Rows.Count
col1 = Trim(CStr(objSheet.Cells(i, 1).Value)) 'Column1
col2 = Trim(CStr(objSheet.Cells(i, 2).Value)) 'Column2
'REM ADDED BY EXCEL *************************************
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").selectedNode = "F00028"
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").doubleClickNode "F00028"
session.findById("wnd[0]/usr/ctxtVBRK-VBELN").Text = col1
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/mbar/menu[2]/menu[0]/menu[3]").Select
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3").getAbsoluteRow(0).Selected = True
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/lblDV70A-STATUSICON[0,0]").SetFocus
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/lblDV70A-STATUSICON[0,0]").caretPosition = 0
session.findById("wnd[0]/tbar[1]/btn[6]").press
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/cmbNAST-NACHA[3,0]").Key = "1"
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/cmbNAST-NACHA[3,0]").SetFocus
session.findById("wnd[0]/tbar[0]/btn[11]").press
session.findById("wnd[0]/usr/chkNAST-DELET").Selected = True
session.findById("wnd[0]/usr/ctxtNAST-LDEST").Text = "LOCALNEW"
session.findById("wnd[0]/usr/txtNAST-TDRECEIVER").Text = col2
session.findById("wnd[0]/usr/txtNAST-TDRECEIVER").SetFocus
session.findById("wnd[0]/usr/txtNAST-TDRECEIVER").caretPosition = 8
session.findById("wnd[0]/tbar[0]/btn[3]").press
session.findById("wnd[0]/tbar[0]/btn[11]").press
session.findById("wnd[0]/tbar[0]/btn[3]").press
'REM FINALIZATION CONTROL CHECK ************************
Dim aux As String
aux = col1 & " " & col2
CreateObject("WScript.Shell").Run ("cmd /c @echo %date% %time% " & aux & " >> C:\SCRIPT\PlOrCreationLog.txt")
Next
MsgBox "Process Completed"
'REM FINALIZATION CONTROL CHECK ************************
' Put error handling here
Err_Description:
Err_TooManySAP:
End Sub
我正在尝试使用 Excel 中的按钮 运行 一个 SAP GUI 脚本,我创建脚本的方式是从 Excel 获取信息以用于SAP,该脚本效果很好,如果我打开它,它 运行 很顺利,但我无法将该脚本添加到 Excel 命令按钮。
这是有效的原始脚本:
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
'REM ADDED BY EXCEL *************************************
Dim objExcel
Dim objSheet, intRow, i
Set objExcel = GetObject(,"Excel.Application")
Set objSheet = objExcel.ActiveWorkbook.ActiveSheet
For i = 2 to objSheet.UsedRange.Rows.Count
COL1 = Trim(CStr(objSheet.Cells(i, 1).Value)) 'Column1
COL2 = Trim(CStr(objSheet.Cells(i, 2).Value)) 'Column2
'REM ADDED BY EXCEL *************************************
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").selectedNode = "F00028"
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").doubleClickNode "F00028"
session.findById("wnd[0]/usr/ctxtVBRK-VBELN").text = Col1
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/mbar/menu[2]/menu[0]/menu[3]").select
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3").getAbsoluteRow(0).selected = true
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/lblDV70A-STATUSICON[0,0]").setFocus
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/lblDV70A-STATUSICON[0,0]").caretPosition = 0
session.findById("wnd[0]/tbar[1]/btn[6]").press
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/cmbNAST-NACHA[3,0]").key = "1"
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/cmbNAST-NACHA[3,0]").setFocus
session.findById("wnd[0]/tbar[0]/btn[11]").press
session.findById("wnd[0]/usr/chkNAST-DELET").selected = true
session.findById("wnd[0]/usr/ctxtNAST-LDEST").text = "LOCALNEW"
session.findById("wnd[0]/usr/txtNAST-TDRECEIVER").text = Col2
session.findById("wnd[0]/usr/txtNAST-TDRECEIVER").setFocus
session.findById("wnd[0]/usr/txtNAST-TDRECEIVER").caretPosition = 8
session.findById("wnd[0]/tbar[0]/btn[3]").press
session.findById("wnd[0]/tbar[0]/btn[11]").press
session.findById("wnd[0]/tbar[0]/btn[3]").press
'REM FINALIZATION CONTROL CHECK ************************
aux=col1 & " " & col2
CreateObject("WScript.Shell").run("cmd /c @echo %date% %time% " & aux & " >> C:\SCRIPT\PlOrCreationLog.txt")
next
msgbox "Process Completed"
'REM FINALIZATION CONTROL CHECK ************************
如果我将此代码添加到 Excel VBA 它不起作用,所以我按照 this post 并将 'application' 更改为 'GuiApplication' 并且根据 post 答案给出的代码做了一些更改。
完成更改后,我得到了以下代码:
Private Sub CommandButton1_Click()
If Not IsObject(GuiApplication) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set GuiApplication = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = GuiApplication.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 GuiApplication, "on"
End If
If (Connection.Children.Count > 1) Then GoTo Err_TooManySAP
Set aw = session.ActiveWindow()
aw.findById("wnd[0]").Maximize
'REM ADDED BY EXCEL *************************************
Dim objExcel
Dim objSheet, intRow, i
Set objExcel = GetObject(, "Excel.Application")
Set objSheet = objExcel.ActiveWorkbook.ActiveSheet
For i = 2 To objSheet.UsedRange.Rows.Count
col1 = Trim(CStr(objSheet.Cells(i, 1).Value)) 'Column1
col2 = Trim(CStr(objSheet.Cells(i, 2).Value)) 'Column2
'REM ADDED BY EXCEL *************************************
On Error GoTo Err_Description
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").selectedNode = "F00028"
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").doubleClickNode "F00028"
session.findById("wnd[0]/usr/ctxtVBRK-VBELN").Text = col1
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/mbar/menu[2]/menu[0]/menu[3]").Select
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3").getAbsoluteRow(0).Selected = True
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/lblDV70A-STATUSICON[0,0]").SetFocus
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/lblDV70A-STATUSICON[0,0]").caretPosition = 0
session.findById("wnd[0]/tbar[1]/btn[6]").press
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/cmbNAST-NACHA[3,0]").Key = "1"
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/cmbNAST-NACHA[3,0]").SetFocus
session.findById("wnd[0]/tbar[0]/btn[11]").press
session.findById("wnd[0]/usr/chkNAST-DELET").Selected = True
session.findById("wnd[0]/usr/ctxtNAST-LDEST").Text = "LOCALNEW"
session.findById("wnd[0]/usr/txtNAST-TDRECEIVER").Text = col2
session.findById("wnd[0]/usr/txtNAST-TDRECEIVER").SetFocus
session.findById("wnd[0]/usr/txtNAST-TDRECEIVER").caretPosition = 8
session.findById("wnd[0]/tbar[0]/btn[3]").press
session.findById("wnd[0]/tbar[0]/btn[11]").press
session.findById("wnd[0]/tbar[0]/btn[3]").press
'REM FINALIZATION CONTROL CHECK ************************
aux = col1 & " " & col2
CreateObject("WScript.Shell").Run ("cmd /c @echo %date% %time% " & aux & " >> C:\SCRIPT\PlOrCreationLog.txt")
Next
MsgBox "Process Completed"
'REM FINALIZATION CONTROL CHECK ************************
End Sub
当我点击“调试”>“编译 VBA 项目时,弹出一条错误提示
Compile error: Label not defined
并突出显示代码中的一行 Set objExcel = GetObject(, "Excel.Application")
对我做错了什么有什么想法吗?我确定我做错了很多事情,我正在学习。我只需要在 Excel.
上单击按钮即可 运行 该脚本获取 Excel 对象用于从不同主机自动执行 Excel。当您的主机是 Excel 时,Application
对象包含 Excel.Application
对象。
这段代码有很多问题。如果将 Option Explicit
放在顶部,您会发现您还没有声明所有变量。当你有时,它们没有类型。
您遇到了一些使用 GoTo
但没有匹配行标签的错误陷阱。您需要为每个 GoTo
设置行标签,并且还需要决定在这些错误事件中要做什么。注意 sub 末尾的行标签。它们以冒号 :
结尾。这里没有错误处理代码,sub 将结束。您通常希望至少警告用户错误。如果您喜欢冒险,可以就如何解决问题或尝试以编程方式解决问题提供反馈。
我已经修改了你的代码,所以它可以编译,但我不知道它是否符合你的要求:
Option Explicit
Dim SAPGuiApp As Object
Dim SapGuiAuto As Object
Dim Connection As Object
Dim SAP_session As Object
Dim WScript As Object
Dim aw As Object
Private Sub CommandButton1_Click()
If Not IsObject(SAPGuiApp) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set SAPGuiApp = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = SAPGuiApp.Children(0)
End If
If Not IsObject(SAP_session) Then
Set SAP_session = Connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject SAP_session, "on"
WScript.ConnectObject SAPGuiApp, "on"
End If
If (Connection.Children.Count > 1) Then GoTo Err_TooManySAP
Set aw = SAP_session.ActiveWindow()
aw.findById("wnd[0]").Maximize
Dim objSheet As Worksheet
Set objSheet = ActiveSheet
Dim i As Integer
For i = 2 To objSheet.UsedRange.Rows.Count
Dim col1 As String
col1 = Trim(CStr(objSheet.Cells(i, 1).Value)) 'Column1
Dim col2 As String
col2 = Trim(CStr(objSheet.Cells(i, 2).Value)) 'Column2
On Error GoTo Err_Description
With SAP_session
.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").selectedNode = "F00028"
.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").doubleClickNode "F00028"
.findById("wnd[0]/usr/ctxtVBRK-VBELN").Text = col1
.findById("wnd[0]").sendVKey 0
.findById("wnd[0]/mbar/menu[2]/menu[0]/menu[3]").Select
.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3").getAbsoluteRow(0).Selected = True
.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/lblDV70A-STATUSICON[0,0]").SetFocus
.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/lblDV70A-STATUSICON[0,0]").caretPosition = 0
.findById("wnd[0]/tbar[1]/btn[6]").press
.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/cmbNAST-NACHA[3,0]").Key = "1"
.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/cmbNAST-NACHA[3,0]").SetFocus
.findById("wnd[0]/tbar[0]/btn[11]").press
.findById("wnd[0]/usr/chkNAST-DELET").Selected = True
.findById("wnd[0]/usr/ctxtNAST-LDEST").Text = "LOCALNEW"
.findById("wnd[0]/usr/txtNAST-TDRECEIVER").Text = col2
.findById("wnd[0]/usr/txtNAST-TDRECEIVER").SetFocus
.findById("wnd[0]/usr/txtNAST-TDRECEIVER").caretPosition = 8
.findById("wnd[0]/tbar[0]/btn[3]").press
.findById("wnd[0]/tbar[0]/btn[11]").press
.findById("wnd[0]/tbar[0]/btn[3]").press
End With
'REM FINALIZATION CONTROL CHECK ************************
Dim aux As String
aux = col1 & " " & col2
CreateObject("WScript.Shell").Run ("cmd /c @echo %date% %time% " & aux & " >> C:\SCRIPT\PlOrCreationLog.txt")
Next
MsgBox "Process Completed"
'REM FINALIZATION CONTROL CHECK ************************
' Put error handling here
Err_Description:
Err_TooManySAP:
End Sub
@HackSLash
我尝试了不同的代码,这次我没有对当前可用的原始脚本进行太多更改,我添加了您与我共享的代码的最后一部分并且它起作用了。
Private Sub CommandButton1_Click()
If Not IsObject(GuiApplication) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set GuiApplication = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = GuiApplication.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 GuiApplication, "on"
End If
session.findById("wnd[0]").maximize
'REM ADDED BY EXCEL *************************************
Dim objExcel
Dim objSheet, intRow, i
Set objExcel = GetObject(, "Excel.Application")
Set objSheet = objExcel.ActiveWorkbook.ActiveSheet
For i = 2 To objSheet.UsedRange.Rows.Count
col1 = Trim(CStr(objSheet.Cells(i, 1).Value)) 'Column1
col2 = Trim(CStr(objSheet.Cells(i, 2).Value)) 'Column2
'REM ADDED BY EXCEL *************************************
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").selectedNode = "F00028"
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").doubleClickNode "F00028"
session.findById("wnd[0]/usr/ctxtVBRK-VBELN").Text = col1
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/mbar/menu[2]/menu[0]/menu[3]").Select
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3").getAbsoluteRow(0).Selected = True
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/lblDV70A-STATUSICON[0,0]").SetFocus
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/lblDV70A-STATUSICON[0,0]").caretPosition = 0
session.findById("wnd[0]/tbar[1]/btn[6]").press
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/cmbNAST-NACHA[3,0]").Key = "1"
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/cmbNAST-NACHA[3,0]").SetFocus
session.findById("wnd[0]/tbar[0]/btn[11]").press
session.findById("wnd[0]/usr/chkNAST-DELET").Selected = True
session.findById("wnd[0]/usr/ctxtNAST-LDEST").Text = "LOCALNEW"
session.findById("wnd[0]/usr/txtNAST-TDRECEIVER").Text = col2
session.findById("wnd[0]/usr/txtNAST-TDRECEIVER").SetFocus
session.findById("wnd[0]/usr/txtNAST-TDRECEIVER").caretPosition = 8
session.findById("wnd[0]/tbar[0]/btn[3]").press
session.findById("wnd[0]/tbar[0]/btn[11]").press
session.findById("wnd[0]/tbar[0]/btn[3]").press
'REM FINALIZATION CONTROL CHECK ************************
Dim aux As String
aux = col1 & " " & col2
CreateObject("WScript.Shell").Run ("cmd /c @echo %date% %time% " & aux & " >> C:\SCRIPT\PlOrCreationLog.txt")
Next
MsgBox "Process Completed"
'REM FINALIZATION CONTROL CHECK ************************
' Put error handling here
Err_Description:
Err_TooManySAP:
End Sub