Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
551 views
in Technique[技术] by (71.8m points)

sap gui - Export SAP to excel completely via VBA

I hope you can assist me with the code below. I am trying to export a report from SAP using a recorded SAP GUI Script via VBA on a daily basis. Subsequently I want to copy the data to another workbook and do some reformatting/calculations with it. However, the excel export file will only open after I have fully completed my macro.

Using for instance ShellAndWait http://www.cpearson.com/excel/ShellAndWait.aspx assumes that it is an outside application, but I would like to find a solution in which I can keep everything in 1 file.

Thanks in advance for your assistance!

Edit: Solved! Thanks to Scriptman.

ExportInvest

Public Sub ExportInvest()

Dim Today As String
Dim FileName As String
Dim Ret

Today = Format$(DateTime.Now, "yyyymmdd__hh-MM-ss")
FileName = "Invest_" & Today & ".xml"

Set SapGuiAuto = GetObject("SAPGUI")  'Get the SAP GUI Scripting object
Set SAPApp = SapGuiAuto.GetScriptingEngine 'Get the currently running SAP GUI
Set SAPCon = SAPApp.Children(0) 'Get the first system that is currently connected
Set session = SAPCon.Children(0) 'Get the first session (window) on that connection

session.findById("wnd[0]/tbar[0]/okcd").Text = "/nzx03"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[1]/btn[17]").press
session.findById("wnd[1]/usr/txtENAME-LOW").Text = "TXO4074"
session.findById("wnd[1]/usr/txtENAME-LOW").SetFocus
session.findById("wnd[1]/tbar[0]/btn[8]").press
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/mbar/menu[0]/menu[1]/menu[1]").Select
session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "S:FL_DMASAP Scriptsestackup"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = FileName
session.findById("wnd[1]/tbar[0]/btn[0]").press

Set wshell = CreateObject("Wscript.Shell")
wshell.Run "C:Usersxo4074DocumentsProjectsSAPscriptINVESTmyTest.vbs" & " " & "999Invest.xlsm" & " " & FileName

End Sub

MyTest.vbs

set xclapp = getObject(,"Excel.Application")

on error resume next
do 
    err.clear
    Set xclwbk = xclApp.Workbooks.Item(wscript.arguments(1))
    If Err.Number = 0 Then exit do
    wscript.sleep 2000
loop
on error goto 0

xclapp.Run wscript.arguments(0) & "!ThisWorkbook.ExportInvest_1"

ExportInvest_1

Sub ExportInvest_1()


FileName = NewestFile("S:FL_DMASAP Scriptsestackup", "****")
   
Workbooks(Left(FileName, 25)).Worksheets(1).Activate
Range("A2:K100").Select
Selection.Copy ThisWorkbook.Worksheets("Input").Range("A2:K100")
Workbooks(Left(FileName, 25)).Close
ThisWorkbook.Activate

End Sub
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I would solve it as follows:

ExportInvest: (in myTest.xlsm)

. . .
session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "S:FL_DMASAP Scriptsestackup"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = FileName
session.findById("wnd[1]/tbar[0]/btn[0]").press

'new commands
Set wshell = CreateObject("Wscript.Shell")
wshell.Run "c:mpmyTest.vbs" & " " & "myTest.xlsm"
End Sub

myTest.vbs:

set xclapp = getObject(,"Excel.Application")
xclapp.Run wscript.arguments(0) & "!ExportInvest_1"

ExportInvest_1: (in myTest.xlsm)

Sub ExportInvets_1()
    Dim Today as String
    Today = Format$(DateTime.Now, "yyyymmdd__hh-MM-ss")

    MsgBox "file open", vbOKOnly, ""
    Workbooks("Invest_" & Today).Activate
    Worksheets(1).Range("A2:K100").Select
    Selection.Copy ThisWorkbook.Worksheets("Input").Range("A2:K100")
End Sub

Regards, ScriptMan


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...