SAP通过VBA从内存中提取到EXCEL
我试图通过我记录的SAP脚本将一个SAP事务提取到一个.txt文档中,该脚本最后被提取到我的excel工作表中 当我在SAP中运行脚本时,它会将值复制到我的.txt文档中。所以我的问题似乎是excel中的宏没有激活SAP脚本 当我运行VBA宏执行相同操作时,它会显示 运行时错误“7”:内存不足 并且不会将任何内容复制到我的.txt文档中 我希望有人能告诉我为什么SAP通过VBA从内存中提取到EXCEL,excel,vba,sap-gui,Excel,Vba,Sap Gui,我试图通过我记录的SAP脚本将一个SAP事务提取到一个.txt文档中,该脚本最后被提取到我的excel工作表中 当我在SAP中运行脚本时,它会将值复制到我的.txt文档中。所以我的问题似乎是excel中的宏没有激活SAP脚本 当我运行VBA宏执行相同操作时,它会显示 运行时错误“7”:内存不足 并且不会将任何内容复制到我的.txt文档中 我希望有人能告诉我为什么 Public SapGuiAuto, WScript, msgcol Public objGui As GuiApplication
Public SapGuiAuto, WScript, msgcol
Public objGui As GuiApplication
Public objConn As GuiConnection
Public objSess As GuiSession
Public objSBar As GuiStatusbar
Public objSheet As Worksheet
Dim W_System
Const fpath = "S:\Supply\WChristian\SAP GUI"
Const ffilename = "script2.txt"
Sub OpenCSVFile()
'
' Load the CSV extract
'
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;S:\Supply\WChristian\SAP GUI\script2.txt", Destination:=Range( _
"$A$4:$I$24"))
.Name = "mb52"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = "|"
.TextFileColumnDataTypes = Array(9, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
Sub DeleteAll()
'
' DeleteAll Macro
On Error Resume Next
Cells.Select
Selection.QueryTable.Delete
Selection.ClearContents
Range("A1").Select
End Sub
Function Attach_Session() As Boolean
Dim il, it
Dim W_conn, W_Sess
If W_System = "" Then
Attach_Session = False
Exit Function
End If
If Not objSess Is Nothing Then
If objSess.Info.SystemName & objSess.Info.Client = W_System Then
Attach_Session = True
Exit Function
End If
End If
If objGui Is Nothing Then
Set SapGuiAuto = GetObject("SAPGUI")
Set objGui = SapGuiAuto.GetScriptingEngine
End If
For il = 0 To objGui.Children.Count - 1
Set W_conn = objGui.Children(il + 0)
For it = 0 To W_conn.Children.Count - 1
Set W_Sess = W_conn.Children(it + 0)
If W_Sess.Info.SystemName & W_Sess.Info.Client = W_System Then
Set objConn = objGui.Children(il + 0)
Set objSess = objConn.Children(it + 0)
Exit For
End If
Next
Next
If objSess Is Nothing Then
MsgBox "No active session to system " + W_System + ", or scripting is not enabled.", vbCritical + vbOKOnly
Attach_Session = False
Exit Function
End If
If IsObject(WScript) Then
WScript.ConnectObject objSess, "on"
WScript.ConnectObject objGui, "on"
End If
Set objSBar = objSess.findById("wnd[0]/sbar")
objSess.findById("wnd[0]").maximize
Attach_Session = True
End Function
Public Sub RunGUIScript()
Dim W_Ret As Boolean
' Connect to SAP
W_Ret = Attach_Session
If Not W_Ret Then
Exit Sub
End If
On Error GoTo myerr
ojbSess.findById("wnd[0]").ResizeWorkingPane 174, 29, False
ojbSess.findById("wnd[0]/tbar[0]/okcd").Text = "mb52"
ojbSess.findById("wnd[0]").sendVKey 0
ojbSess.findById("wnd[0]/usr/ctxtWERKS-LOW").Text = "DO"
ojbSess.findById("wnd[0]/usr/ctxtLGORT-LOW").Text = "01"
ojbSess.findById("wnd[0]/usr/ctxtMATKLA-LOW").Text = "2"
ojbSess.findById("wnd[0]/usr/ctxtMATKLA-LOW").SetFocus
ojbSess.findById("wnd[0]/usr/ctxtMATKLA-LOW").caretPosition = 3
ojbSess.findById("wnd[0]").sendVKey 8
ojbSess.findById("wnd[0]/tbar[1]/btn[45]").press
ojbSess.findById("wnd[1]/tbar[0]/btn[0]").press
ojbSess.findById("wnd[1]/usr/ctxtDY_PATH").Text = "S:\Supply\WChristian\SAP GUI\"
ojbSess.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "script2.txt"
ojbSess.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 11
ojbSess.findById("wnd[1]/tbar[0]/btn[11]").press
Exit Sub
myerr:
MsgBox "Error occured while retrieving data", vbCritical + vbOKOnly
End Sub
Sub StartExtract()
' Set the sid and client to connect to
W_System = "DCG210"
' Run the GUI script
RunGUIScript
' End the GUI session
' Switch to the worksheet where the data is loaded to
Sheets("Extract").Select
' Delete the entire sheet before loading the file
DeleteAll
' Load the CSV file
OpenCSVFile
' Update the time and date on the control worksheet
Sheets("Control").Select
Cells(2, 2).Value = Now()
End Sub
正如我向@Christian承诺的那样,我在这里粘贴了我与SAP连接的方法。这种方法一直有效。我基于这一点对SAP的大量报告使用了所有宏:)
希望它能对您有所帮助:)正如我向@Christian承诺的那样,我在这里粘贴了我与SAP连接的方法。它一直在工作。我的所有宏都是基于这一点从SAP获取大量报告的:)
希望它能对您有所帮助:)不久前,我还在为SAP连接苦苦挣扎,我没有太多时间来研究您的代码,但是如果没有人发布答案,我可以发布我的方法与您的代码混合(我没有使用
SapGuiAuto.GetScriptingEngine
)您能解释一下“在SAP中运行脚本”是什么意思吗?(VBA脚本?SAP中的“那么”是什么意思“?)@SandraRossi抱歉,我的意思是,当我在SAP中运行SAP脚本时,它可以很好地将数据提取到文档中。所以我的意思是,当我的代码需要激活我的SAP脚本时,它似乎做错了。我仍然不明白。但是,让我们忘记这个“SAP脚本”,您提到它,但没有提供代码(您不应该发布它,因为它可能是一个转换错误吗?)。在VBA宏(我称之为“VBA脚本”)中,您能否使用调试器告诉我们它在哪个子例程中失败。注:很难提供帮助,因为前一段时间我还在努力使用SAP连接,我没有太多时间来研究你的代码,但是如果没有人发布答案,我可以发布我的方法与你的代码混合(我没有使用SapGuiAuto.GetScriptingEngine
)你能解释一下“在SAP中运行脚本”是什么意思吗?(VBA脚本?那么“在SAP中”是什么意思?)@SandraRossi抱歉,我的意思是,当我在SAP中运行SAP脚本时,它可以很好地将数据提取到文档中。所以我的意思是,当我的代码需要激活我的SAP脚本时,它似乎做错了。我仍然不明白。但是,让我们忘记这个“SAP脚本”,您提到它,但没有提供代码(您不应该发布它,因为它可能是一个转换错误吗?)。在VBA宏(我称之为“VBA脚本”)中,您能否使用调试器告诉我们它在哪个子例程中失败。NB:很难帮到你,因为。非常感谢你!我去试试。如果你不介意的话,我只想问一个简单的问题。在我的大多数SAP事务中,我需要输入物料代码进行查找。是否有任何方法可以创建一个宏,将Excel中的列表复制粘贴到材料中?如果我理解正确,我通常是从Excel记录副本,在SAP中使用“从剪贴板复制”按钮。SAP窗口列表中显示了材料/产品/备件(无论什么)的完整列表。如果这种方法有效,请给出一个信号。我不知道它有效!你是上帝。非常感谢:)整个SAP登录问题也可以在这里找到,非常感谢!我去试试。如果你不介意的话,我只想问一个简单的问题。在我的大多数SAP事务中,我需要输入物料代码进行查找。是否有任何方法可以创建一个宏,将Excel中的列表复制粘贴到材料中?如果我理解正确,我通常是从Excel记录副本,在SAP中使用“从剪贴板复制”按钮。SAP窗口列表中显示了材料/产品/备件(无论什么)的完整列表。如果这种方法有效,请给出一个信号。我不知道它有效!你是上帝。非常感谢:)整个SAP登录问题也可以在这里找到
Sub SapConn()
Dim Appl As Object
Dim Connection As Object
Dim session As Object
Dim WshShell As Object
Dim SapGui As Object
'Of course change for your file directory
Shell "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", 4
Set WshShell = CreateObject("WScript.Shell")
Do Until WshShell.AppActivate("SAP Logon ")
Application.Wait Now + TimeValue("0:00:01")
Loop
Set WshShell = Nothing
Set SapGui = GetObject("SAPGUI")
Set Appl = SapGui.GetScriptingEngine
Set Connection = Appl.Openconnection("01. PRD - ERP Production", True) 'here paste your name of module you are trying to connect/enter
Set session = Connection.Children(0)
'if You need to pass username and password - not connect automatically
'session.findById("wnd[0]/usr/txtRSYST-MANDT").Text = "900"
'session.findById("wnd[0]/usr/txtRSYST-BNAME").Text = "user"
'session.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = "password"
'session.findById("wnd[0]/usr/txtRSYST-LANGU").Text = "EN"
If session.Children.Count > 1 Then
answer = MsgBox("You've got opened SAP already, please leave and try again", vbOKOnly, "Opened SAP")
session.findById("wnd[1]/usr/radMULTI_LOGON_OPT3").Select
session.findById("wnd[1]/usr/radMULTI_LOGON_OPT3").SetFocus
session.findById("wnd[1]/tbar[0]/btn[0]").press
Exit Sub
End If
session.findById("wnd[0]").maximize
session.findById("wnd[0]").sendVKey 0 'ENTER
'and there goes your code in SAP