简体   繁体   中英

Prevent or Close Exported Files - From SAP GUI to Excel

I am exporting multiple data sets from SAP GUI into EXCEL 365. My code exports the data to a uniquely named place holder file, then moves on the next T-Code. The exported files open slowly, depending on the file size, while the Macro is running. I plan to have the exported files referenced by queries to eliminate any copy paste issues by users, and to speed the total processing of the fully loaded workbook.

Workbooks("FileName").Close triggers the Run-Time Error ('9').

Workbooks.Close closes all workbooks not opened via SAP GUI Export procedure.

I found an oShell taskkill line I'll post below which works well, although the exported files still completely open, with the exception of it also closing my template workbook (the one triggering the Macro). Half the time, this closure triggers the "Do you want to Save?" Message Box, which I am fine handling with SendKeys (I can live with this solution). However, the other half the time "Can not close Excel window" pops up, and the template file closes as soon as "Ok" is clicked.

I've tried declaring variable objects for the workbooks, then closing the variables. The issue here is the only function which seems to work is "Open", which just opens the last save file. If I include this code as the files are exported, the last saved files will all open during the run time, and the exported files will open once the Macro has finished.

I've tried creating an independent Macro for the Workbooks ("FileName").Close and the Workbooks ("PathName\\FileName").Close methods to be run after the export Macro is finished. Same run time error result.

I've included my multiple Close procedure attempts, but they are all commented out. I am still learning the vocab and symbolism, so please be patient with me if I have questions to better understand any aid offered.

Edit: I trimmed down my code for a single data dump and the related attempts to close the exported files. Also added "GUI" after SAP, and deleted reference to my variant.

Here is my Code:

Sub Data_Dumps()

''Check for open Excel Workbooks
'Dim oShell: Set oShell = CreateObject("WScript.Shell")

'Dim MB51 As Workbook

If Not IsObject(GetDataDump) Then
   Set SapGuiAuto = GetObject("SAPGUI")
   Set GetDataDump = SapGuiAuto.GetScriptingEngine
End If

If Not IsObject(Connection) Then
   Set Connection = GetDataDump.Children(0)
End If

If Not IsObject(session) Then
   Set session = Connection.Children(0)
End If

If IsObject(WScript) Then
   WScript.ConnectObject session, "on"
   WScript.ConnectObject GetDataDump, "on"
End If

'Consumption - MB51
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nmb51"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/btn%_MATNR_%_APP_%-VALU_PUSH").showContextMenu
session.findById("wnd[0]/usr").selectContextMenuItem "DELACTX"
session.findById("wnd[0]/usr/btn%_WERKS_%_APP_%-VALU_PUSH").showContextMenu
session.findById("wnd[0]/usr").selectContextMenuItem "DELACTX"
session.findById("wnd[0]/usr/btn%_LGORT_%_APP_%-VALU_PUSH").showContextMenu
session.findById("wnd[0]/usr").selectContextMenuItem "DELACTX"
session.findById("wnd[0]/usr/btn%_CHARG_%_APP_%-VALU_PUSH").showContextMenu
session.findById("wnd[0]/usr").selectContextMenuItem "DELACTX"
session.findById("wnd[0]/usr/btn%_LIFNR_%_APP_%-VALU_PUSH").showContextMenu
session.findById("wnd[0]/usr").selectContextMenuItem "DELACTX"
session.findById("wnd[0]/usr/btn%_KUNNR_%_APP_%-VALU_PUSH").showContextMenu
session.findById("wnd[0]/usr").selectContextMenuItem "DELACTX"
session.findById("wnd[0]/usr/btn%_KUNNR_%_APP_%-VALU_PUSH").showContextMenu
session.findById("wnd[0]/usr").selectContextMenuItem "DELACTX"
session.findById("wnd[0]/usr/btn%_BWART_%_APP_%-VALU_PUSH").showContextMenu
session.findById("wnd[0]/usr").selectContextMenuItem "DELACTX"
session.findById("wnd[0]/usr/btn%_SOBKZ_%_APP_%-VALU_PUSH").showContextMenu
session.findById("wnd[0]/usr").selectContextMenuItem "DELACTX"
session.findById("wnd[0]/usr/btn%_MAT_KDAU_%_APP_%-VALU_PUSH").showContextMenu
session.findById("wnd[0]/usr").selectContextMenuItem "DELACTX"
session.findById("wnd[0]/usr/btn%_MAT_KDPO_%_APP_%-VALU_PUSH").showContextMenu
session.findById("wnd[0]/usr").selectContextMenuItem "DELACTX"
session.findById("wnd[0]/usr/btn%_BUDAT_%_APP_%-VALU_PUSH").showContextMenu
session.findById("wnd[0]/usr").selectContextMenuItem "DELACTX"
session.findById("wnd[0]/usr/btn%_USNAM_%_APP_%-VALU_PUSH").showContextMenu
session.findById("wnd[0]/usr").selectContextMenuItem "DELACTX"
session.findById("wnd[0]/usr/btn%_VGART_%_APP_%-VALU_PUSH").showContextMenu
session.findById("wnd[0]/usr").selectContextMenuItem "DELACTX"
session.findById("wnd[0]/usr/btn%_XBLNR_%_APP_%-VALU_PUSH").showContextMenu
session.findById("wnd[0]/usr").selectContextMenuItem "DELACTX"
session.findById("wnd[0]/tbar[1]/btn[17]").press
session.findById("wnd[1]/usr/txtV-LOW").Text = "/SIOP Central"
session.findById("wnd[1]/usr/txtENAME-LOW").Text = ""
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 = "Z:\Rebar Fab Planning\DSI & Inventory Data Dumps"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "Consumption - MB51.XLSX"
session.findById("wnd[1]/tbar[0]/btn[11]").press
'
''Closes Excel Sheets
'oShell.Run "taskkill /im excel.exe", , True
'
''Keeps DSI & Inventory Sheet Open
'
'SendKeys "{Tab}{Tab}~"

''Closes Data Dump Workbooks

'MB51.Close

'Workbooks("Consumption - MB51.XLSX").Close

'Workbooks("Z:\Rebar Fab Planning\DSI & Inventory Data Dumps\Consumption - MB51.XLSX").Close

End Sub

If you are lucky resp. have setup Excel in such a way that your file Z:\\Rebar Fab Planning\\DSI & Inventory Data Dumps\\Consumption - MB51.XLSX is opened in the same Excel instance you can use the following code to close this file

Option Explicit

Function isWorkbookOpen(ByVal wkbName As String) As Boolean
    ' Only working for same Excel instance
    Dim wkb As Workbook

    On Error Resume Next
    Set wkb = Workbooks(wkbName)
    On Error GoTo 0

    If wkb Is Nothing Then
        isWorkbookOpen = False
    Else
        isWorkbookOpen = True
    End If

End Function

Function closeWkb(ByVal wkbName As String, counter As Long)

    Const MAX_TRIES As Long = 10

    If isWorkbookOpen(wkbName) Then
        Workbooks(wkbName).Close False
    Else
        counter = counter + 1
        If counter <= MAX_TRIES Then
            Application.OnTime Now + TimeValue("00:00:05"), "'closeWkb """ & wkbName & """,""" & counter & "'"
        End If
    End If
    
End Function

And in your code you just call

closeWkb "Consumption - MB51.XLSX", 0

The code will try to close the Excel file Consumption - MB51.XLSX MAX_TRIES times (10 in the code above) every 5 seconds if it finds a open workbook with this name. This works only if the workbook you try to close has been opened in the same instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM