File disappearing on file close

Faulkner5649

New Member
Joined
Oct 11, 2022
Messages
2
Office Version
  1. 365
Platform
  1. 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.


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:

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Hi. I tried your code; it created the VBS file and the file did not disappear. I suspect that perhaps your anti-virus software might be proactively scanning and deleting the file; VBS files tend to be a target of AVS.
 
Upvote 0
Solution
Thanks for your reply Dan,
yeah I just tried changing the format to .txt and it worked fine so i think you could be right. i'll look into the anti-virus. thanks again.
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,274
Members
452,628
Latest member
dd2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top