Close and Save another workbook doesn't work?

sanantonio

Board Regular
Joined
Oct 26, 2021
Messages
124
Office Version
  1. 365
Platform
  1. Windows
Hi All,

Happy holidays!

Was hoping someone could help me out with this code.

The first part of the code is where I have SAP exporting and auto saving a file. When SAP exports and saves a file it also opens it automatically. So I have put in an "Application.wait" code to wait for the file to open before attempting to close it, then coming back to my original excel and refreshing all. It is the code in red that doesn't work. I just get a debug.

VBA Code:
session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "J:\filepath"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "ZMMX118.XLSX"
session.findById("wnd[1]/usr/ctxtDY_PATH").SetFocus
session.findById("wnd[1]/usr/ctxtDY_PATH").caretPosition = 102
session.findById("wnd[1]/tbar[0]/btn[11]").press
session.findById("wnd[0]/tbar[0]/btn[12]").press
session.findById("wnd[0]/tbar[0]/btn[12]").press

Application.Wait (Now + TimeValue("0:01:00")) 'Waiting for ZMMX118 export to open

Application.DisplayAlerts = False
Workbooks("ZMMX118.xlsx").Close SaveChanges:=False 'Closes the export without saving

 ActiveWorkbook.RefreshAll 'returns to macro enabled workbook where this code lives and refreshes all

What I need to do is just wait for the export to open, the "ZMMX118.XLSX", then close it without saving, return to the macro workbook where this code lives and refresh all.

Am I missing something obvious?
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hi. So, both the Workbooks("ZMMX118.xlsx").Close SaveChanges:=False part and the ActiveWorkbook.RefreshAll part don't work? I'm thinking you'd want to use ThisWorkbook instead of ActiveWorkbook, but not sure what's going on with the external workbook close part. What's the debug error message and which line does it occur on?
 
Upvote 0
Hi
May try
VBA Code:
Windows("Book3").Close False
 
Upvote 0
Hi. So, both the Workbooks("ZMMX118.xlsx").Close SaveChanges:=False part and the ActiveWorkbook.RefreshAll part don't work? I'm thinking you'd want to use ThisWorkbook instead of ActiveWorkbook, but not sure what's going on with the external workbook close part. What's the debug error message and which line does it occur on?
1672772350269.png

1672772358293.png


Hi. Sorry for the late reply, been off for the holidays!


These are the errors I'm getting. The macro works until here, it just will not close this other workbook.
 
Upvote 0
Hi
I Thought you would noticed the you have to change Book2 to the name of your file you want to close
VBA Code:
Windows("ZMMX118").Close False
 
Upvote 0
Solution
worbooks(your complete path).close False
 
Upvote 0
worbooks(your complete path).close False

Unfortunately this still doesn't work.

My exact code is this and it still hits the debug here:
1672832384684.png


VBA Code:
Sub test()
Application.DisplayAlerts = False
Workbooks("K:\Supply_Chain\Reporting\ZMMX118.xlsx").Close False
End Sub
 
Upvote 0
You should not need the full path of the file if the file is open. But the file's extension is needed. At least with my testing. Workbooks("ZMMX118.xlsx").Close False works for me.

What does the message box display when you run this code?

VBA Code:
Sub vba_check_workbook()

Dim WB As Workbook
Dim myWB As String

myWB = "ZMMX118.xlsx"

For Each WB In Workbooks
    If WB.Name = myWB Then
        WB.Activate
        MsgBox "Workbook Found!"
        Exit Sub
    End If
Next WB

MsgBox "Not Found"

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,583
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top