My program at the very end uses the below code to open an Excel file, it is the final line of code too:
os.system(f'start EXCEL.EXE "{file_name}"')
Excel starts fine and opens the file with no issues since the file is in the current working directory. After Excel opens and I select "Enable Editing", it requires me to reopen the file in Excel with the below notification:
Excel Reopen File Notification
The author of the file is openpyxl immediately after it first opens, so I think that the newer version it is referring to has to do with openpyxl still affecting the Excel file while I have it open. I am using Microsoft Office 365 with Autosave feature on. I cannot figure out how to prevent this from happening. I use a with statement as my context manager to write out the various dataframes to the sheets, so to my knowledge, the file should be closed once the code executes through the context manager. I hope this is enough info, I am new to coding (I am doing this to automate some of my day-to-day job tasks).
I haven't been able to think of anything that might prevent the file from having me reopen to a newer version since I thought the with statement context manager closes the Excel file so openpyxl shouldn't be able to touch it after that code block executes.
Related
I have an issue where certain workbooks (.XLSM) with included macros and conditional formatting are not saving properly on my Windows 10 machine. I have not been able to determine what is triggering the issue...
Upon trying to save even just a minor change (eh: adding a 1 to an empty cell), I encounter the following error:
Errors were detected while saving to 'my_path'. Microsoft Excel may be
able to save the file by removing or repairing some features. To make
the repairs in a new file, click Continue. To cancel saving the file,
click Cancel.
If I try the Continue option, I encounter further errors:
Excel encountered errors during save. However, Excel was able to
minimally save your file to my_path.
Even the 'repaired' files cannot be saved. All changes are essentially lost (unless using the temporary solution I describe at the end)
What is strange is that this seems to occur only on my own machine. Some other details about this case:
Office 2016 - Excel 32bit
Windows 10 21H1
All updates applied
Same behavior occurs after uninstall and reinstall of Office 2016
Same behavior occurs after uninstall using Microsoft's stand-alone removal tool
Same behavior in Excel safe mode
Same behavior with Add-Ins and COM Add-Ins disabled
Same behavior whether file/save path is local (C:\path) or on a network drive
Same behavior when saving with new filename
Same behavior when saving/exporting to .xlsx, .xlsb (and .xlsm)
Error doesn't occur on other Windows machines
The ONLY way to save the document seems to be to save the file as an .xls.
Why is this and what could the matter be with my particular Office installation?
Since upgrading to Excel 365, my company has been having all sorts of trouble with VBA-supported Excel workbooks crashing or not functioning properly. The issues have popped up in various workbooks and various departments, including seemingly simple VBA workbooks. I've discovered a fix (see below) but it's not sustainable.
The Issues
The various issues we have experienced are listed below. Note, these issues only occur when you open a file in Excel 365 desktop. The same files, when opened in the Excel 365 browser app or in Excel 2016 will work fine. Also, all these issues happen at random. A user may have been working in a file for weeks and then the next time they open the file they get one of these errors.
"Can't find project or library" errors even when we are using the standard set of libraries and basic VBA. Then when you open the VBA Editor window, all the VBA screens are essentially frozen up and the library list is inaccessible.
Excel hard crashes or locks up when opening these files and enabling VBA. Note, the crash only occurs when the VBA is enabled. If you open a file without enabling VBA, it will work fine (though obviously you can't use any of the code).
The file opens seemingly fine, but the VBA doesn't work and once again, everything is locked up when you access the VBA editor window.
"unhandled win32 exception occurred" error
when executing a command, getting an error 32809 which seems to indicate the compiled VBA has been corrupted
The Fix
Opening the VBA Editor window, then selecting Debug -> Compile VBAProject seemingly fixes the issue. I've yet to experience any of the above issues where the file had been manually compiled this way. The problem with this is that every time you add code to a file or any time you add a new tab to a file, you have to go in and perform this manual compile again.
Yes, apparently adding a single tab in a file changes the workbook structure enough that it is necessary to compile again. The Compiled VBAProject selection will be greyed out. But adding a tab (or adding new VBA) will un-grey it and require another manual compile or the problems start occurring again.
This fix also works to repair files where the above issues are occurring. To fix those files, you can:
Make sure "Disable Trusted Documents" and "Disable VBA macros with notification" are toggled on in your security settings so that VBA doesn't automatically run when you open a file.
Open the file with the errors but do not enable the VBA.
Go to the VBA Editor window, select Debug -> Compile VBAProject.
Save the file, close it, reopen it, enable the VBA, and everything works fine again.
But again, this isn't sustainable because all my users will have to remember to go compile VBA any time they add a new tab to a workbook.
Help?
Has anyone found a more sustainable fix to this issue? Is there a bug with the Excel 365 Desktop automatic compiler that Microsoft is working on?
I think I perhaps found an answer in this thread:
https://social.msdn.microsoft.com/Forums/en-US/814ac2ce-ab45-45dc-8c6a-8ef0775d189a/excel-64bit-crashes-when-activating-macros-but-excel-32bit-does-not?forum=exceldev&prof=required
Per that thread – “the cause is that Excel does not correctly save the compile state of the VBA code and 64 bit Excel cannot recover from that issue when opening the afflicted Excel file (32 bit usually can). A fix was released for only Excel 2016 and not for other versions". That would indeed confirm that it is a bug within Excel and explains why we only see the issue with 365 64 bit Excel.
That also explains why my manual compile fix works. Based on the article I found, there is a more sustainable fix. You can change Excel’s registry and force VBA to compile accurately.
To implement the permanent fix:
Open the start menu and type “reg” and select the “Registry Editor”
Navigate to: Computer\HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\16.0\Excel\options
On the Edit menu, point to New, and then click DWORD Value.
Type ForceVBALoadFromSource, and then press Enter.
In the Details pane, right-click ForceVBALoadFromSource, and then click Modify.
In the Value data box, type 1, and then click OK.
I have a workbook that was saved yesterday afternoon, and was working perfectly. I have opened it this morning, and none of the modules in the VBE are 'found'. Visually I can see them all sitting there.
When trying to open any of these modules to edit the code, the windows are greyed out, as below.
When I try exporting the code modules, I get the 'Module Not Found' errror.
Does anyone know a) why this has happened, and b) how can I fix this?
I thought initially it was the instance of my Excel, so have restarted the PC.
Any help is appreciated!
I managed to find a workaround to this problem, so sharing the solution in the event that someone else comes across a similar problem.
It seems that the VBA Project got corrupt somehow. Below, find some suggestions and workarounds in trying to solve something similar in the future.
This is what worked for me. Convert the .xlsm file to a .xls file. You can do this by changing the file extension when renaming the file.
You can also try to convert to .zip file type, and then convert back to .xlsm. Note: convert COPIES of your original, just in case.
Other suggestions (as Chris Nelisen suggested) are:
Export your VBA modules regularly
Save different versions as you are building
My workaround, works perfectly:
Open Excel in safe mode (pressing and holding Ctrl while you start
the program, or by using the /safe switch (excel.exe /safe) when you
start the program from the command line)
Open corrupted workbook (from safe mode, File->Open-> navigate)
Do not enable macro if asked
Make sure macro is present (Alt+F11) - not necessary
Save as new workbook
Close safe mode excel
Open saved workbook as usual
This is a well-described issue, and it exactly matches what I have just experienced (even including the fact that I haven't been versioning recently).
My file has an xlsb suffix. Resaving with a different suffix did not work for me on the same PC, but I emailed the file to another PC, opened it, saved as xlsm, sent it back to the original machine and it now works fine again. I can even re-save with my preferred xlsb suffix and it still works.
I've also run this script to make a backup of my modules:
Sub ExportVbaModules()
'Acknowledgements to Andy Pope [ozgrid thread 60787]
Dim objMyProj As VBProject 'if error, go to VBA editor - tools - References - Microsoft Visual Basic-Extensibility5.3
Dim objVBComp As VBComponent
Set objMyProj = Application.ActiveWorkbook.VBProject
For Each objVBComp In objMyProj.VBComponents
If objVBComp.Type = vbext_ct_StdModule And objVBComp.Name <> "" Then
objVBComp.Export "C:\Users\MyName\VbaBackups\" & objVBComp.Name & ".txt"
End If
Next
End Sub
The And objVBComp.Name <> "" stops it from erroring when it encounters a corrupted module but turned out not to be necessary as the 'fixed' file contained no corrupted modules.
Since that export routine is so fast (40 modules / 100kb saved in <1s) I will be assigning it to a button on the ribbon with a better naming convention for the files it creates.
I copied my .xslm file from my PC to my OneDrive account.
I open the file on my iPad OneDrive account and then export to Excel for iOS. The file opens and says links and macros are disabled. I then save a copy of the file back to the OneDrive account. I go back to my PC and open the file from OneDrive. I re-establish the links.
The macros are from a backup. This is an issue if you aren't backing up your macros.
Do you have this file on OneDrive?
If yes, I was facing that issue, and resolved restoring the last save. If you open OneDrive site (onedrive.live.com), find the file, and select Version History. Download the penultimate.
When this happens on 64 bit Excel, I simply open the exact same file in 32 bit Excel and the macros re-appear.
When this happens on 32 bit Excel, I simply open the exact file in 64 bit Excel and the macros re-appear.
Try to open the excel file in repair mode and save as the file one more time.
Open and Repair
I tried everything suggested and nothing worked. I could only see the module when I opened my VB editor. It was not available through the view macros ribbon shortcut. I was unable to export the module or copy it to a new workbook.
What finally worked for me was emailing it to myself, downloading it through my 365 outlook account via a web browser, and then the code was there.
I created a simple vba addin that colors cells based on their value, and I created a function that calls it with a shortcut then I saved it as an Excel addin and added it to Excel.
The problem is the addin works fine when I add it the first time, but when I open a new Excel file, I need to disable and enable the addin for it to work.
Update: I tried it on another computer and it works, but it shows an error that when I ignore it works fine. I am adding screenshots for the error and code
Error Message
Code
Sometimes, Excel will open workbooks in another Excel Application. This second application can sometimes face some issues with addins. You should double-check that the new file is opened in the same Excel Application. By looking at the task manager:
In this example, I'm using Window 10 and you can see that Book3.xlsx is in a different Excel Application than Book2.xlsx and Book1.xlsx
EDIT:
This question could also be of interest to you. The accepted answer reads:
This problem results from security patch in KB31152, released in July 2016. According to private communication with Microsoft software engineers:
"With this update, we changed the behavior of Excel so that it will
not load certain file types (including .xlam) when they are untrusted.
The easiest workaround is to find the add-in that is causing you
trouble, right-clicking on it in Windows Explorer, and checking
Unblock"
An easier approach is to simply place the add-in in a Trusted Location
(in Excel, go to File > Options > Trust Center > Trust Center Settings
Trusted Locations), such as the following folder, and load it from there:
C:\Users\%USER NAME%\AppData\Roaming\Microsoft\Excel\XLSTART
EDIT2:
And don't forget the option of just restarting your computer just to make sure that the problem is still there.
I crashed the XLSM file already several times during programming in Excel (2010). (lastly during debugging)
The result was, that it was either
reported as corrupt (sometimes repairable) or
crashed the whole Excel application :-/ or
opening the VBA editor with a message "automation error (2147467259) unspecified error" and one cannot close Excel anymore without using the Task Manager and killing the whole Excel instance
How can I get the original or same version back or get it fixed?
(We have a version tracking system in place, but sometimes the lost changes would be quite big)
Sometimes it is possible to fix the VBA code execution when opening the workbook.
If the automation error occurs it may be just due to some uncompiled code (in combination with triggering features like Open, Select, Activate events or (re)calculating formulas).
So this helps in a lot of situations (especially during development):
rename the MyFile.xlsm file to MyFile2.xlsm
=> thus the Activate Macro dialog will appear on next open
do not Activate Macro, but push F11 to (edit the code or/and) VBA Editor => Menu => Debugging => Compile ... the code
close the file and rename back to MyFile.xlsm
on open: choose Activate Macro
Open a new workbook, navigate to your corrupt file, click Open and Repair (not just Open).
Luckily we had some Excel 2016 in place and it could be opened there and saved as the same XLSM file. Afterwards it could be opened by Excel 2010 without a problem :)
(In Excel 2016 there is also an Open and Repair... option that may be necessary - as mentioned by ryguy72).
(In general it is a good idea to have a versioning system like Mercurial, Git or some versioning DMS etc. in place!)
Btw: We could also open and save it in LibreOffice (6.0.6.2 64bit), but there were too many non-LibreOffice compatible formattings (e.g. images collapsing with left-side row groups), features (e.g. Named Tables) etc. inside, so the resulting file was not very useful for us (but for others this may be enough - without big licensing/installing hassle).