Error in *.xlsm file when writing strings using xlsm - openpyxl

I am using openpyxl (2.3.2) to write out an xlsm file. I can write the numeric values correctly but the string values are corrupting the xlsm file. In the code example below, the xlsm file is corrupted but the xlsx file works well
When I open the xlsm file , I get a error
we found a problem with some content in samp2.xlsm
On continuing, I see only the numeric values are written and not the string values. This does not happen with the xlsx file.
This is a strange problem
PS The "Samp.xlsm" and "Sample.xlsx" Files are new files saved with these names. They do not have any content.
Appreciate any suggestions/feedback

Related

Which file types are associated with an Excel workbook that has a file format with a value of 63?

Does anyone know which file types are associated with an Excel workbook that has a file format with a value of 63? I have already looked at the MSDN article about the "XlFileFormat Enumeration", but I have not found the value of 63 there. Can anyone help or share a useful web link?
Update to deliver a Minimal, Complete, and Verifiable example:
Create a new blank .txt file and encode it as UTF-8 with BOM (you can use a program like Notepad++ for that task)!
Open Microsoft Excel (I used Microsoft Excel 2010 32-bit for Windows)!
Within the opened Excel application open the newly created .txt file with "File" --> "Open"!
Within Excel create the following VBA macro and execute it to get the "XLFileFormat" value for the currently active workbook or rather the opened .txt file!
Public Sub ShowFileFormatOfActiveWorkbook()
MsgBox ActiveWorkbook.FileFormat
End Sub
If you have followed the above instructions correctly, you should get a value of 63 (from Excel 2010, but a value of 64 from Excel 2013/2016).
Repeat the above instructions with an UTF-8 encoded .csv file! You will get a value of 63 (or 64 depending on the Excel version you use) again.
Do just UTF-8 encoded text-based files (e.g. .txt, .csv, .html) have a "XlFileFormat" value of 63 or do exist some more file types which match the mentioned "XlFileFormat" of 63?

.NET How to open CSV with Excel Interop without text converted to formulas

I'm currently presented with the following issue:
I need to open a CSV file with the Excel Interop Classes (15.0). This is done using the following code:
Dim app As New Excel.Application
Dim workbook As Excel.Workbook = app.Workbooks.Open(sFileNameCSV, Format:=4, Local:=True)
Unfortunately this converts some of the data into formulas (e.g. text starting with a hyphen [- this is an example] or phone numbers in international format [+41-555-123-45-67]) resulting in either a #NAME? error or a calculated result in case of the phone number.
After some searching in the web and on SO I tried the following things with no luck:
Saving the CSV File as a .txt file
Using the OpenText() Method instead of the Open() method
Combination of the two above
Is there any solution to this issue without having to change the CSV file data itself and still using the Interop classes, like disabling formulas altogether? Or am I just missing a param in the Open() / OpenText() functions?

Verifiy if an xls file contains VBA macros without opening it in MS Excel

I have a big set of .xls (Excel 97-2003 Workbook) files. A few of them contain VBA macros inside, I would like to find a way to filter them out automatically without opening them one by one in MS Excel.
There is an old post which is similar to my question, I have downloaded the OLE Doc viewer, but cannot open the .zip file, it seems that it is out of date...
Does anyone know if there is any API or tool to check if an .xls file contains VBA macros without opening it in MS Excel? In the first place, I don't bother to know the content of the macros.
PS: for a .xlsx or .xlsm file, we can change their file extension to .zip which contain some .xml files and eventually vbaProject.bin for VBA macros. However, this approach does not work for .xls file, renaming it does not make a valid .zipfile.
Here is a simple Python 2.7 solution I've cooked for you:
It depends only on the OleFileIO_PL module which is availble from the project page
The good thing with OleFile parser is that it is not aware of the "excel-specific" contents of the file ; it only knows the higher level "OLE storage". So it is quick in analyzing the file, and there is no risk that a potentially harmful macro would execute.
import OleFileIO_PL
import argparse
if __name__=='__main__':
parser = argparse.ArgumentParser(description='Determine if an Excel 97-2007 file contains macros.', epilog='Will exit successfully (0) only if provided file is a valid excel file containing macros.')
parser.add_argument('filename', help="file name to analyse")
args=parser.parse_args()
# Test if a file is an OLE container:
if (not OleFileIO_PL.isOleFile(args.filename)):
exit("This document is not a valid OLE document.")
# Open an OLE file:
ole = OleFileIO_PL.OleFileIO(args.filename)
# Test if known streams/storages exist:
if (not ole.exists('workbook')):
exit("This document is not a valid Excel 97-2007 document.")
# Test if VBA specific streams exist:
if (not ole.exists('_VBA_PROJECT_CUR')):
exit("This document does not contain VBA macros.")
print("Valid Excel 97-2007 workbook WITH macros")
I tested it on a couple of files with success. Let me know if it's suitable for you

Forcing to save xlsx format in client side from xlsm file in server side

I have myfile.xlsm formate file in server side as read only in that i wrote some VBA to file columns in excel.When client access that file i'm just showing the filled file.While client tend to save the file it goes to SaveAs because it read only.It saving as myfile.xlsm in client side.Now my problem is :)
if client tend to save the file from saveas option it is suppose to save only as myfile.xlsx formate as ordinary xml file not macro enabled file.
What i have to do in VBA to do this action (may be write some thing in before save event)..??
I have created the one private method and add this line `ActiveWorkbook.SaveAs Filename:="Formula.xlsx", FileFormat:=xlOpenXMLWorkbook` and calling this method .. ha it worked !!

BCP - Exported files (docx, xlsx & pptx) are corrupted

I am using the below bcp command to export the binary files from the filestream db and all other files are seems to be exported fine (i.e. txt, pdf, rtf, image files & etc) except docx, xlsx & pptx files. I am able to export these files (i.e. docx, xlsx & pptx) but I am getting a warning/error message when opening those files and then it opens the file properly.
BCP "SELECT content FROM [dbo].[Contents] WHERE ID=1" queryout "C:\Temp\" -T -S (local) -f C:\Temp\files.fmt
I am getting the below message when opening docx, xlsx & pptx files:
XLSX - Excel found unreadable content and Do you want to recover the contents of this document?
DOCX - The file test.docx cannot be opened bcos there are problems with the content and then Excel found unreadable content and Do you want to recover the contents of this document?
Also, I have this in my files.fmt file:
10.0
1
1 SQLBINARY 0 0 "\t" 1 content ""
Any help will be much appreciated.
I am not 100% sure if your problem was the same as what I have had but in my case, I found that the problem was actually in the writing part, not in the reading part. For example, my original writing code was like this:
Dim FILE_CONTENT(len) As Byte
File.InputStream.Read(FILE_CONTENT, 0, len)
SaveFileToDatabase(FILE_NAME, CONTENT_TYPE, FILE_CONTENT)
When I changed the first line as the following :
Dim FILE_CONTENT(0 To len - 1) As Byte
the reading error disappeared. I just forgot that VB actually allocates N+1 bytes (O to N) by default when you dimension it without specifying the lower bound. See similar situation described here: Uploaded Docx Files are getting corrupted . hope that helps.