I'm trying to automate a SAP transaction using a VBA script. The transaction extracts some data to an Excel template, displays the resulting Excel file and for some reason tries to exit it which shows the "save changes" pop up window, this is where the transaction ends.
I want my VBA code to click "cancel" on the save changes pop up window.
I want my VBA execute this transaction on different items and save the resulting .xls files to a specific location, the problem is this pop up window stops the code and I have to click it manually.
Disabling the Pop up will not help as the file will exit without saving and will be lost. I don't have authorization to change the SAP code so my only option is to run a script through VBA.
To force a workbook to save changes, type the following code in a Visual Basic module of that workbook:
Sub Auto_Close()
If ThisWorkbook.Saved = False Then
ThisWorkbook.Save
End If
End Sub
This subprocedure checks to see if the file Saved property has been set to False. If so, the workbook has been changed since the last save, and those changes are saved.
SOURCE: https://support.microsoft.com/en-us/office/-how-to-suppress-save-changes-prompt-when-you-close-a-workbook-in-excel-189a257e-ec1b-40f7-9195-56d82e673071?ui=en-us&rs=en-us&ad=us
Related
I run a lot of macros, and lately had to change to O365, which gave me the "wonderful" 64 bit Excel. One of its most maddening shenanigans is that when I run some macros (working with SAP GUI, creating SAP reports, naming them, saving and downloading to a specific folder where the next macro step opens them and incorporates into a relevant macro sheet), the new Excel opens these in a new instance (which I don't want but cannot prevent - can you help here?), which causes a dialog "File in Use" (which I don't want but cannot prevent - can anyone help me here?) because it tries to open my PERSONAL.XLSB (which I need and therefore don't want to get rid of just because of this) and qualifies it as "locked for editing" by another user, who is myself (which is absolutely stupid but I cannot change it - can anyone help me perhaps with this part?).
Sometimes the macro finishes fine when I don't answer that dialog; sometimes it seems to cause the Excel to hang up in trying to do next steps, sometimes the macro quits on me when I click "Read only" and so on.
All this is maddening also because these files that (possibly) SAP GUI opens, I have to waste my time by closing them after the macro finishes (has anyone a possible solution in stopping SAP opening these files? I was unable to find it anywhere so far.).
In some macros I fixed it by a specific part of code which waits till when these files open up and then it closes them - yet even that wait is wasting my time and it would be better without.
But with O365 and other instances it is even more colorful, because some files open in the same instance (this issue was there even before the O365 though) and I can manually close them after the macro finishes (although again, I have to wait till they open, which is again "vanity and torture of the soul"), but those opening in the new instance (specific to O365) show as blank sheets, I have to click into them, a warning sound follows but no dialog is visible, then I have to click into them again from the Excel icon in the bottom menu bar and then it tells me that I cannot close them because a dialog is open.
Then I need to close this dialog plus the one which appeared about the "File in Use", then I finally wait for the file (that I don't need) to open (because it is already saved inside the folder and already copied and pasted onto the relevant macro sheet) and only then I can close it.
So far I was unable to find anywhere on the internet an advice for how to stop SAP opening these files (when they are already saved where I need them), neither how to stop them opening in new Excel instances. So reluctantly, I think I should aim for at least a tiny little VBA code which will cause my macro to click on the "Read Only" button. Can anyone here help me with any part of this "Excel complaint"?
I do not know how/if SAP can be 'persuaded' to not open the workbooks in discussion... But, I think, you can handle that in a different way the "PERSONAL.XLSB" behavior, from the opening point of view. Of course, if SAP itself has this "bad habit" and not your code opens it in a new session, which have to be solved in a different way...
"PERSONAL.XLSB" is located in "...AppData\Roaming\Microsoft\Excel\XLSTART" folder. All workbooks located in this folder are automatically open when an Excel session starts;
You may change the "PERSONAL.XLSB" workbook path. If you want it to be open when a specific (necessary) workbook needs it, add a reference to it.
addRef. In order to do that, modify the (standard) VBAProject (of "PERSONAL.XLSB"), in, let us say, "VBPersProj", then (being in VBE) trough 'Tools - References...` tick its check box and press 'OK';
If the exported workbook is open in a new session and, even if it does not bother you any more because of the annoying dialogs and you need to identify that specific session, you can use the next function:
Function sameExSession(wbFullName As String, Optional boolClose As Boolean) As Boolean
Dim sessEx As Excel.Application, wb As Workbook
Set sessEx = GetObject(wbFullName).Application
If sessEx.hWnd = Application.hWnd Then
sameExSession = True
Else
sameExSession = False
If boolClose Then
sessEx.Workbooks(Right(wbFullName, Len(wbFullName) - InStrRev(wbFullName, "\"))).Close False
sessEx.Quit: Set sessEx = Nothing
End If
End If
End Function
It offers the possibility to close the workbook and the session itself if it is a different against the one where the code runs. It is an Optional parameter to be used, only if you need that. You can test it in the next way:
Sub testSameExSession()
Dim wbFullName As String
wbFullName = ThisWorkbook.fullName 'use here the full name of the SAP exported workbook
If sameExSession(wbFullName, True) Then
'process the workbook if this is what you need
End If
'if not open in the active session, the workbook could be closed and session quit, choosing the above way
'You can also simply call the function, without the closing parameter, as:
Debug.Print sameExSession(wbFullName)
End Sub
In case of False function retur, it can be closed and reopen in the existing session, if needed...
Edited:
If you need personal.xlsb being used/open when ribbon controls calls some of its Subs/functions, please adapt the code to call it in the next way:
Dim x as Long
x = Run("'C:\Users\your_UserName\AppData\Roaming\Microsoft\Excel\XLSTART\PERSONAL.XLSB'!GiveMeFive", 4, 3)
if calling a function (with parameters), which must return. Or
Run "'C:\Users\your_UserName\AppData\Roaming\Microsoft\Excel\XLSTART\PERSONAL.XLSB'!YourSubName"
when calling a Sub. In such a way, "Personal.xlsb" is opened if it is closed. And, in your specific case nothing will press the ribbon controls.
But, isn't the ribbon added when a specific workbook is open? Probably, the one you are using to make the above mentioned calls... If so, adding the suggested reference at "Personal.xlsb", will configure the new ribbon tab only when it is loaded. And your mentioned "problem" does not exist, in fact. Is it an add-in?
I have an excel file in which I want to freez the view to cell D5 and I want the grid lines to be invisible. Getting this done works fine, however, these settings get lost sporadically after saving and re-opening the file. The file is on a server and four people work on it. I trust them when they tell me that they did not change it back. Could some local setting do this automatically?
If you cannot save it, maybe you can run some code to set your settings every time you open the file.
The following code will show you how to Run a Macro Automatically.
Private Sub Workbook_Open()
ActiveWindow.DisplayGridlines = False
MsgBox "Hi dude! your settings were applied"
End Sub
That will eliminate the grid lines everytime you open the workbook.
I don't know what you mean by freezing D5 cell, but can use the macro recorder to get the code of what you record to then use it in your procedures.
You should have your button "record macro" (mine is in spanish "Grabar macro")
For the code to execute when the workbook opens you should name the procedureas told (Private Sub Workbook_Open()) and paste it in the "ThisWorkbook" module.
Hope it helps
It seems that the view seetings are saved with the window and new windows are initialized with grid and without freez pane. If the file is then saved with the new window open, all settings are lost (i.e. set to default).
I have an excel file that opens a messagebox when the file is opened . However, when I open the file, because of the messagebox, the file and its messagebox do not come to the front of my windows gain focus (they appear on my taskbar and I have to click on it from there). After the messagebox is completed, the file gains focus as normal.
In its simplest form, this code seems to trigger this
Sub Auto_Open()
MsgBox ""
End Sub
I have tried adding code in to give the window focus before executing the messagebox, but to no success. I don't mind this issue but other people will be using this document. Is there a solution to this?
The legacy Auto_Open auto-macro is going to run regardless of the state of Application.EnableEvents, which may be a problem if another macro-enabled workbook needs to open that workbook.
I can't reproduce the issue on Excel 2010, with this code in Module1:
Sub Auto_Open()
MsgBox "Auto_Open()"
End Sub
And this code in ThisWorkbook:
Private Sub Workbook_Open()
MsgBox "Workbook_Open()"
End Sub
I get two successive message boxes, first from Workbook_Open, then from Auto_Open, both show up right where I'd expect them - right in the middle of an empty worksheet:
Try putting the message box in the Open event handler of ThisWorkbook, see if it helps.
Have an Excel file for different days of the year. also have a master excel file. Would like to create a link button from the day excel files to the VBA in the Master file. So if need to change some code, I can change it in the master file and the day files will run the new code from the master file.
Assuming that, in an open workbook called "Master.xlsm", you have a macro called test:
Sub test()
MsgBox "in master"
End Sub
and that you have a forms control button on a sheet in another open workbook (perhaps "Daily.xlsx"), then you can assign the macro to that button as:
If you subsequently open just "Daily.xlsx" without first opening "Master.xlsm", you will get the
message (due to the link to "Master.xlsm"). Irrespective of whether you say "Update" or "Don't Update", "Master.xlsm" will be automatically opened when you click the button, and the test macro will be executed.
Similar to a question in
MS Access VBA How to delete a row in Excel
However, the problem is Excel asks for "Do you want to save the changes you made to ...?"
Is there a way to force the saving of changes and disable any message box? I try DoCmd.SetWarnings False, but it does not work.
You'll need to add the following code to ThisWorkbook
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Save
End Sub
If you're running vba in Access and accessing an Excel object, just save the workbook (e.g. xlApp.ThisWorkbook.Save) in your code before closing