如何将多个交易放入同一代码

时间:2019-06-11 11:27:32

标签: vba sap

我有几个要自动化的事务,然后粘贴到几个表中。 我的代码适用于我的第一个事务,但对于其他事务,我则输入相同的代码,只是删除了放入它的工作表,这根本不起作用。

 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 = "C:\Users\p100789\Documents\SAP\SAP GUI"
  Const ffilename = "text.txt"


   Sub OpenCSVFile()
  '
  ' Load the CSV extract

   With ActiveSheet.QueryTables.Add(Connection:= _
    "TEXT;" & fpath & "\" & ffilename, Destination:=Range("$A$1"))
    .Name = "text"
    .FieldNames = True
    .RowNumbers = False
   [...]
End With

  With ActiveSheet
 .Columns(1).EntireColumn.Delete 
 'delete first column
 .Columns(1).EntireColumn.Insert
 .Rows("1:11").EntireRow.Delete 'delete first 9 rows
End With

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
     Dim Société As String
     Sheets("Extraction").Select
      Société = Range("b9")

     ' Connect to SAP
     W_Ret = Attach_Session
     If Not W_Ret Then
     Exit Sub
     End If

   On Error GoTo myerr

   [....script]

   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 = "P10320"
   ' Run the GUI script
     RunGUIScript
    ' End the GUI session
      objSess.EndTransaction
      'effacer contenu feuille temp
    Sheets("temp").Select
    Cells.Select
    Selection.Delete Shift:=xlUp
   ' Switch to the worksheet where the data is loaded to
    Sheets("temp").Select

   ' Load the CSV file
     OpenCSVFile


     [...]

      ' Update the time and date on the control worksheet
       Sheets("Extraction").Select
       Cells(2, 2).Value = Now()
简而言之,

为了自动执行另一笔交易,我在新脚本中填充了相同的代码。它不起作用,有人解决方案吗?

Error

0 个答案:

没有答案