Exported file opens after macro completes - unwanted

前端 未结 6 689
猫巷女王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 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

提交回复
热议问题