0
votes

I'm writing a program (in C#) that will be able to replace a local workbook from a server if the server version is higher, and then open it. To this end I'm trying to read Custom Property "Revision Number" of both local and server copies. The issue is that the workbook contains macros that launch on open, and I don't want to run any macros just to check the Revision Code. So is there a way to read the Revision Number of an excel 2007 xlsm file without actually opening it? If not, is there a way to open a workbook in C# and not execute it's macros?

1
Did you check out ClosedXML (or OpenXML)? Excel files (2007 and >) are also XMLs. So opening them via these libraries will not execute any macrosAnalystCave.com

1 Answers

0
votes

Actually I tried the tkacprow's suggestion to use OpenXML and it worked. It just took me a while to produce a working code and I just got it working yesterday. Fratyx, your tip also looks interesting - i'll keep that in mind. Here's a working code:

public string GetVersion(string fileName)
    {
        string propertyValue = string.Empty;

        try
        {



            using (var wb = SpreadsheetDocument.Open(fileName, false))
            {


                const string corePropertiesSchema = "http://schemas.openxmlformats.org/package/2006/metadata/core-properties";
                const string dcPropertiesSchema = "http://purl.org/dc/elements/1.1/";
                const string dcTermsPropertiesSchema = "http://purl.org/dc/terms/";




                // Get the core properties part (core.xml).
                CoreFilePropertiesPart xCoreFilePropertiesPart;
                xCoreFilePropertiesPart = wb.CoreFilePropertiesPart;



                // Manage namespaces to perform XML XPath queries.
                NameTable nt = new NameTable();
                XmlNamespaceManager nsManager = new XmlNamespaceManager(nt);
                nsManager.AddNamespace("cp", corePropertiesSchema);
                nsManager.AddNamespace("dc", dcPropertiesSchema);
                nsManager.AddNamespace("dcterms", dcTermsPropertiesSchema);

                // Get the properties from the package.
                XmlDocument xdoc = new XmlDocument(nt);

                // Load the XML in the part into an XmlDocument instance.
                xdoc.Load(xCoreFilePropertiesPart.GetStream());

                string searchString = string.Format("//cp:coreProperties/{0}", "cp:version");

                XmlNode xNode = xdoc.SelectSingleNode(searchString, nsManager);
                if (!(xNode == null))
                {
                    //Console.WriteLine(" version is " +  xNode.InnerText);
                    propertyValue = xNode.InnerText;
                }



            }


        }


        catch (OpenXmlPackageException e)
        {

            throw new ApplicationException(String.Format("Incorrect Format detected in a file: {0}" , fileName),e.GetBaseException());
        }


        return propertyValue;

    }