Esempio n. 1
0
        public static bool ConvertToNewOfficeDocument(string fileName, out string newFileName, out string newExt)
        {
            var ext = Path.GetExtension(fileName).ToLower();

            newExt = ext;

            newFileName = fileName;

            if (!DestinationDictionary.ContainsKey(ext))
            {
                return(false);
            }
            if (fileName.StartsWith("~*"))
            {
                return(false);
            }

            var dest = DestinationDictionary[ext];

            newExt = dest.Extension;

            newFileName = ReplaceExtension(fileName, dest.Extension);

            object obj;

            var result = true;

            switch (dest.Application)
            {
            case OfficeApplication.Word:
                if (_wordApp == null)
                {
                    _wordApp = new Application();
                }
                _Document wordDoc = null;

                try
                {
                    wordDoc = _wordApp.Documents.Open(fileName);
                    wordDoc.Convert();
                    if (wordDoc.HasVBProject)
                    {
                        newFileName = ReplaceExtension(newFileName, ".docm");
                        dest        = new OfficeDestination(OfficeApplication.Word, ".docm",
                                                            WdSaveFormat.wdFormatXMLDocumentMacroEnabled); //for files with macro
                    }
                    wordDoc.SaveAs(newFileName, dest.FileFormat);
                }
                catch (Exception e)
                {
                    newFileName = fileName;
                    result      = false;
                    DisplayError(fileName, e);
                }
                finally
                {
                    if (wordDoc != null)
                    {
                        wordDoc.Close();
                        obj = wordDoc;
                        DisposeInteropObject(ref obj);
                    }
                    if (!KeepOpen || !result)
                    {
                        _wordApp.Quit();
                        obj = _wordApp;
                        DisposeInteropObject(ref obj);
                        _wordApp = null;
                    }
                }
                break;

            case OfficeApplication.Excel:
                if (_excelApp == null)
                {
                    _excelApp = new Microsoft.Office.Interop.Excel.Application();
                }
                _Workbook excelDoc = null;
                try
                {
                    excelDoc = _excelApp.Workbooks.Open(fileName);
                    if (excelDoc.HasVBProject)
                    {
                        newFileName = ReplaceExtension(newFileName, ".xlsm");
                        dest        = new OfficeDestination(OfficeApplication.Excel, ".xlsm",
                                                            XlFileFormat.xlOpenXMLWorkbookMacroEnabled); //for files with macro
                    }
                    excelDoc.SaveAs(newFileName, dest.FileFormat);
                }
                catch (Exception e)
                {
                    newFileName = fileName;
                    result      = false;
                    DisplayError(fileName, e);
                }
                finally
                {
                    if (excelDoc != null)
                    {
                        excelDoc.Close();
                        obj = excelDoc;
                        DisposeInteropObject(ref obj);
                    }
                    if (!KeepOpen || !result)
                    {
                        _excelApp.Quit();
                        obj = _excelApp;
                        DisposeInteropObject(ref obj);
                        _excelApp = null;
                    }
                }
                break;

            case OfficeApplication.PowerPoint:
                if (_powerpointApp == null)
                {
                    _powerpointApp = new Microsoft.Office.Interop.PowerPoint.Application();
                }
                _Presentation powerpointDoc = null;
                try
                {
                    powerpointDoc = _powerpointApp.Presentations.Open(fileName, MsoTriState.msoTrue,
                                                                      MsoTriState.msoTrue, MsoTriState.msoFalse);
                    if (powerpointDoc.HasVBProject)
                    {
                        newFileName = ReplaceExtension(newFileName, ".pptm");
                        dest        = new OfficeDestination(OfficeApplication.PowerPoint, ".pptm",
                                                            PpSaveAsFileType.ppSaveAsOpenXMLPresentationMacroEnabled); //for files with macro
                    }
                    powerpointDoc.SaveAs(newFileName, (PpSaveAsFileType)dest.FileFormat);
                }
                catch (Exception e)
                {
                    newFileName = fileName;
                    result      = false;
                    DisplayError(fileName, e);
                }
                finally
                {
                    if (powerpointDoc != null)
                    {
                        powerpointDoc.Close();
                        obj = powerpointDoc;
                        DisposeInteropObject(ref obj);
                    }
                    if (!KeepOpen || !result)
                    {
                        _powerpointApp.Quit();
                        obj = _powerpointApp;
                        DisposeInteropObject(ref obj);
                        _powerpointApp = null;
                    }
                }
                break;
            }

            GarbageCollector();
            return(result);
        }