简体   繁体   English

阻止或关闭导出的文件 - 从 SAP GUI 到 Excel

[英]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.我将多个数据集从 SAP GUI 导出到 EXCEL 365。我的代码将数据导出到一个唯一命名的占位符文件,然后移动到下一个 T 代码。 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("FileName").Close 会触发运行时错误 ('9')。

Workbooks.Close closes all workbooks not opened via SAP GUI Export procedure. Workbooks.Close 关闭所有未通过 SAP GUI 导出程序打开的工作簿。

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).我发现了一个 oShell taskkill 行,我将在下面发布它运行良好,尽管导出的文件仍然完全打开,但它也关闭了我的模板工作簿(触发宏的那个)。 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).消息框,我可以用 SendKeys 很好地处理它(我可以接受这个解决方案)。 However, the other half the time "Can not close Excel window" pops up, and the template file closes as soon as "Ok" is clicked.但是,另一半时间弹出“无法关闭Excel窗口”,单击“确定”立即关闭模板文件。

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.在 SAP 之后还添加了“GUI”,并删除了对我的变体的引用。

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以这样的方式设置 Excel,您的文件Z:\\Rebar Fab Planning\\DSI & Inventory Data Dumps\\Consumption - MB51.XLSX在同一个 Excel 实例中打开,您可以使用以下代码关闭此文件

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.如果找到具有此名称的打开工作簿,代码将每 5 秒尝试关闭 Excel 文件Consumption - MB51.XLSX MAX_TRIES次(上面代码中的 10 次)。 This works only if the workbook you try to close has been opened in the same instance.仅当您尝试关闭的工作簿在同一实例中打开时才有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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