Faulkner5649
New Member
- Joined
- Oct 11, 2022
- Messages
- 2
- Office Version
- 365
- Platform
- Windows
Hi,
I'm having issues with a disappearing file when my macro runs i can see the file prior to running the code but it disappears when the macro runs. I ran a step through and the issue seems to occur at the ts.close line below. any help appreciated.
I'm having issues with a disappearing file when my macro runs i can see the file prior to running the code but it disappears when the macro runs. I ran a step through and the issue seems to occur at the ts.close line below. any help appreciated.
VBA Code:
Sub StockTransfer()
Dim fso As FileSystemObject
Dim ts As TextStream
'Set Character length as string
Dim FS As String * 14
Dim cHeader, cDest, cArticle, cSource, cBatch, cQuantity As String
Sheets("Input Data").Activate
cfile = "C:\Users\613120881\Documents\SAP\SAP GUI\TransferScriptLive.vbs"
Set fso = New FileSystemObject
Set ts = fso.CreateTextFile(cfile)
i = 2
Do While Not Cells(i, 1) Like ""
If Cells(i, 1) <> "" Then
cHeader = Cells(i, 1).Text
cDest = Cells(i, 3).Text
cArticle = Cells(i, 4)
cQuantity = Cells(i, 5)
cSource = Cells(i, 6).Text
cBatch = Cells(i, 7).Text
'Script Header
ts.WriteLine "If Not IsObject(application) Then"
ts.WriteLine " Set SapGuiAuto = GetObject(" + Chr(34) + "SAPGUI " + Chr(34) + ")"
ts.WriteLine " Set application = SapGuiAuto.GetScriptingEngine"
ts.WriteLine "End If"
ts.WriteLine "If Not IsObject(connection) Then"
ts.WriteLine " Set connection = application.Children(0)"
ts.WriteLine "End If"
ts.WriteLine "If Not IsObject(session) Then"
ts.WriteLine " Set session = application.Children(0)"
ts.WriteLine "End If"
ts.WriteLine "If IsObject(WScript) Then"
ts.WriteLine " WScript.ConnectObject; session, " + Chr(34) + "on" + Chr(34) + ""
ts.WriteLine " WScript.ConnectObject Application, " + Chr(34) + "on" + Chr(34) + ""
ts.WriteLine " End If"
ts.WriteLine "' Data Input'"
ts.WriteLine "session.findById(" + Chr(34) + "wnd[0]" + Chr(34) + ").maximize"
ts.WriteLine "'Enter Transaction'"
ts.WriteLine "session.findById(" + Chr(34) + "wnd[0]/tbar[0]/okcd" + Chr(34) + ").text = " + Chr(34) + "mb11" + Chr(34)
ts.WriteLine "session.findById(" + Chr(34) + "wnd[0]" + Chr(34) + ").sendVKey 0"
'Script Data Input
'Enter Header Text
ts.WriteLine "session.findById(" + Chr(34) + "wnd[0]/usr/txtMKPF-BKTXT" + Chr(34) + ").text = " + Chr(34) + cHeader + Chr(34)
'Enter Movement Type
ts.WriteLine "session.findById(" + Chr(34) + "wnd[0]/usr/ctxtRM07M-BWARTWA" + Chr(34) + ").text = " + Chr(34) + "311" + Chr(34)
'Enter Site
ts.WriteLine "session.findById(" + Chr(34) + "wnd[0]/usr/ctxtRM07M-WERKS" + Chr(34) + ").text = " + Chr(34) + "7L50" + Chr(34)
ts.WriteLine "session.findById(" + Chr(34) + "wnd[0]/usr/ctxtRM07M-WERKS" + Chr(34) + ").caretPosition = 1"
ts.WriteLine "session.findById(" + Chr(34) + "wnd[0]" + Chr(34) + ").sendVKey 0"
'Enter Destination SLOC
ts.WriteLine "session.findById(" + Chr(34) + "wnd[0]/usr/ctxtMSEGK-UMLGO" + Chr(34) + ").text = " + Chr(34) + cDest + Chr(34)
'Enter Article
ts.WriteLine "session.findById(" + Chr(34) + "wnd[0]/usr/sub:SAPMM07M:0421/ctxtMSEG-MATNR[0,7]" + Chr(34) + ").text = " + Chr(34) + cArtilce + Chr(34)
'Enter Quantity
ts.WriteLine "session.findById(" + Chr(34) + "wnd[0]/usr/sub:SAPMM07M:0421/txtMSEG-ERFMG[0,26]" + Chr(34) + ").text = " + Chr(34) + cQuantity + Chr(34)
'Enter Source SLOC
ts.WriteLine "session.findById(" + Chr(34) + "wnd[0]/usr/sub:SAPMM07M:0421/ctxtMSEG-LGORT[0,48]" + Chr(34) + ").text = " + Chr(34) + cSource + Chr(34)
'Enter Batch
ts.WriteLine "session.findById(" + Chr(34) + "wnd[0]/usr/sub:SAPMM07M:0421/ctxtMSEG-CHARG[0,53]" + Chr(34) + ").text = " + Chr(34) + cBatch + Chr(34)
ts.WriteLine "session.findById(" + Chr(34) + "wnd[0]/usr/sub:SAPMM07M:0421/ctxtMSEG-CHARG[0,53]" + Chr(34) + ").setFocus"
ts.WriteLine "session.findById(" + Chr(34) + "wnd[0]/usr/sub:SAPMM07M:0421/ctxtMSEG-CHARG[0,53]" + Chr(34) + ").caretPosition = 10"
'Enter /Aceept Input
ts.WriteLine "'session.findById(" + Chr(34) + "wnd[0]" + Chr(34) + ").sendVKey 0'"
'Save Data
ts.WriteLine "'session.findById(" + Chr(34) + "wnd[0]/tbar[0]/btn[11]" + Chr(34) + ").press'"
'Return to Menu
ts.WriteLine "session.findById(" + Chr(34) + "wnd[0]/tbar[0]/okcd" + Chr(34) + ").text =" + Chr(34) + "/n" + Chr(34)
ts.WriteLine "session.findById(" + Chr(34) + "wnd[0]" + Chr(34) + ").sendVKey 0"
End If
i = i + 1
Loop
ts.Close
End Sub
Last edited by a moderator: