Exported file opens after macro completes - unwanted

前端 未结 6 675
猫巷女王i
猫巷女王i 2020-12-18 13:30

I have some VBA code that exports SAP data to a .XLSX file, then imports this file into excel. After the file is imported, I have code that performs many other actions (eg p

相关标签:
6条回答
  • 2020-12-18 13:43

    Today I saw another solution to the problem. I really wanted to share it with you.

    for example:

    ...
    ' Export from SAP to .xlsx file.
    Session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").contextMenu
    Session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectContextMenuItem "&XXL"
    Session.findById("wnd[1]/tbar[0]/btn[0]").press
    Session.findById("wnd[1]/usr/ctxt[0]").Text = "C:\tmp"
    Session.findById("wnd[1]/usr/ctxt[1]").Text = "SAP_export.XLSX"
    Session.findById("wnd[1]/tbar[0]/btn[11]").press
    
    
    Dim xlApp As Object
    Application.Wait Now + TimeSerial(0, 0, 5)
    
    Set xlApp = GetObject("c:\tmp\SAP_export.XLSX").Application
    
    xlApp.Workbooks(1).Close False
    'xlApp.Quit
    ...
    

    Regards, ScriptMan

    0 讨论(0)
  • 2020-12-18 13:45

    So I have been reading a lot about this issue and a lot of people wondering why... i finally got to the point!! Is a thing in SAP Configuration. Go Check on top right corner... click on the TV > Settings > Interactive > Quick Info and use accesibility mode. There it is! Set it to None! and you are done... I got this from a user that told me that a lot of books opened at the end.

    I wrote a small program to solved this issue, if anybody check on this let me know:

       Public Sub ConfSAP()
    
       Set objShell = CreateObject("WScript.Shell")
    
       reg = "REG_DWORD"
    
    
    
        strRoot_16 = "HKEY_CURRENT_USER\SOFTWARE\SAP\SAPGUI Front\SAP Frontend Server\Customize\BubbleDelay"
    strRoot_17 = "HKEY_CURRENT_USER\SOFTWARE\SAP\General\AccMode"
                strModify = objShell.RegWrite(strRoot_16, "00000003", reg)
       strModify = objShell.RegWrite(strRoot_17, "On", "REG_SZ")
                    strModify = Null
                    strRoot_16 = Null
    
    strRoot_17 = Null
    
        subfolder = Null 
        reg = Null Set 
        objShell = Nothing 
        End Sub
    
    0 讨论(0)
  • 2020-12-18 13:59

    I am trying to write a little more today so that my answer will not be deleted. ;-)

    I offer the following workaround for the issue:

    'old codes
    . . .
    Session.findById("wnd[1]/usr/ctxt[1]").Text = "SAP_export.XLSX"
    Session.findById("wnd[1]/tbar[0]/btn[11]").press
    'new codes
    SAP_Workbook = "SAP_export.xlsx" 
    on error resume next
    do
     err.clear
     Set xclApp = GetObject(, "Excel.Application")
     If Err.Number = 0 Then exit do
     'msgbox "Wait for Excel session"
     wscript.sleep 2000
     session.findById("wnd[0]").iconify
     session.findById("wnd[0]").maximize
    loop
    
    do 
     err.clear
     Set xclwbk = xclApp.Workbooks.Item(SAP_Workbook)
     If Err.Number = 0 Then exit do
     'msgbox "Wait for SAP workbook"
     wscript.sleep 2000
    loop
    
    on error goto 0 
    Set xclSheet = xclwbk.Worksheets(1)
    
    xclApp.Visible = True
    xclapp.DisplayAlerts = false
    
    xclapp.ActiveWorkbook.Close
    
    
    Set xclwbk = Nothing
    Set xclsheet = Nothing
    xclapp.Quit
    set xclapp = Nothing
    
    'old codes
    'Closes SAP connection
    Set Session = Nothing
    connection.CloseSession ("ses[0]")
    Set connection = Nothing
    . . . 
    

    Regards, ScriptMan

    0 讨论(0)
  • 2020-12-18 14:04

    My solution here if anybody still needs help. I created a sub in order to kill a lot of files i export. thx scriptman ...

        .findById("wnd[0]/mbar/menu[0]/menu[10]/menu[3]/menu[1]").Select
        .findById("wnd[1]/usr/ctxtDY_PATH").Text = "C:\Informe\"
        .findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "tcurr.xlsx"
        .findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 10
        .findById("wnd[1]/tbar[0]/btn[11]").press
        End With
    
        'Application.Wait Now + TimeValue("00:00:02")
    
        Call Esperar("tcurr.XLSX")
    
       Public Sub Esperar(Optional ByVal archivo As String)    
       On Error Resume Next    
       Set Wshell = CreateObject("WScript.Shell")    
       Do    
        Err.Clear
        Set xclapp = GetObject(, "Excel.Application")
        If Err.Number = 0 Then Exit Do
        DoEvents '---> This do the trick in VB7
        'msgbox "Wait for Excel session"
    
        wscript.Sleep 2000
        Loop
    
       Do
        Err.Clear
        Set xclwbk = xclapp.Workbooks.Item(archivo)
        If Err.Number = 0 Then Exit Do
        DoEvents
        'msgbox "Wait for SAP workbook"
        wscript.Sleep 2000    Loop
    
       On Error GoTo 0    
       Set xclsheet = xclwbk.Worksheets(1)
    
       xclapp.Visible = True
       xclapp.DisplayAlerts = False
    
       xclapp.ActiveWorkbook.Close
    
       Set xclwbk = Nothing    
       Set xclsheet = Nothing
       End Sub
    
    0 讨论(0)
  • 2020-12-18 14:06

    Further Suggestion:

    At the end of your VBA program (without changes) , run the script below.

    For example:

    . . .
    'Open the export and then close to avoid it opening at end of macro.
    set Wshell = CreateObject("WScript.Shell")
    Wshell.run "c:\tmp\SAP_Workbook_Close.vbs",1,false
    End Sub
    

    SAP_Workbook_Close.vbs:

    SAP_Workbook = "SAP_export.xlsx" 
    on error resume next
    do
     err.clear
     Set xclApp = GetObject(, "Excel.Application")
     If Err.Number = 0 Then exit do
     'msgbox "Wait for Excel session"
     wscript.sleep 2000
     loop
    
    do 
     err.clear
     Set xclwbk = xclApp.Workbooks.Item(SAP_Workbook)
     If Err.Number = 0 Then exit do
     'msgbox "Wait for SAP workbook"
     wscript.sleep 2000
    loop
    
    on error goto 0 
    Set xclSheet = xclwbk.Worksheets(1)
    
    xclApp.Visible = True
    xclapp.DisplayAlerts = false
    
    xclapp.ActiveWorkbook.Close
    
    
    Set xclwbk = Nothing
    Set xclsheet = Nothing
    'xclapp.Quit
    set xclapp = Nothing
    

    Regards, ScriptMan

    0 讨论(0)
  • 2020-12-18 14:06

    I have tried to do what I explained in the comments. It isn't very neat and I wasn't able to test. I only changed a couple of lines to tidy a couple of things up, I dont think that line is needed in the Insert .xlsx from file data section thought. Let me know how you go.

    Sub OriginalSub()
      'Opens SAP connection
      [snip as no change]
    
      'Closes SAP connection
      [snip as no change]
    
      Call ImportData()
    
      Kill "C:\Users\" & Environ("Username") & "\Downloads\SAP_export.XLSX"
    
    End Sub
    
    
    Sub ImportData()
    
    'Clear table from SMS Input
        ThisWorkbook.Worksheets("SMS Input").Cells.ClearContents
    
    'Insert .xlsx file data
        ThisWorkbook.Worksheets("SMS Input").Range("A6").Select
        [snip as no changes from here by me]
    
        Workbooks("SAP_export.XLSX").Close savechanges:=False
    
    End Sub
    
    0 讨论(0)
提交回复
热议问题