示例#1
0
        private LateBindingObject SaveDocumentAs(LateBindingObject doc, string fileName)
        {
            bool addToRecentFiles = false;
            int  wdDisplayAlerts  = this._application.GetInt32("DisplayAlerts");

            this._application.SetInt32("DisplayAlerts", 0);

            switch (this._officeVersion)
            {
            case OfficeVersion.Office2000:
                doc.Invoke("SaveAs",
                           fileName, _word12SaveFormat, Type.Missing, Type.Missing, addToRecentFiles,
                           Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                           Type.Missing, Type.Missing);
                break;

            default:
                doc.Invoke("SaveAs",
                           fileName, _word12SaveFormat, Type.Missing, Type.Missing, addToRecentFiles,
                           Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                           Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                           Type.Missing, Type.Missing, Type.Missing);
                break;
            }
            this._application.SetInt32("DisplayAlerts", wdDisplayAlerts);

            return(doc);
        }
示例#2
0
        private LateBindingObject SaveDocumentAs(LateBindingObject doc, string fileName)
        {
            // prevent any popup dialogs which block the application in the background
            bool bDisplayAlerts = this._application.GetBool("DisplayAlerts");

            this._application.SetBool("DisplayAlerts", false);

            switch (this._officeVersion)
            {
            case OfficeVersion.OfficeXP:
                doc.Invoke("SaveAs", fileName, Excel12Class, Type.Missing, Type.Missing, Type.Missing,
                           Type.Missing, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing,
                           Type.Missing, Type.Missing, Type.Missing);
                break;

            case OfficeVersion.Office2003:
                doc.Invoke("SaveAs", fileName, Excel12Class, Type.Missing, Type.Missing, Type.Missing,
                           Type.Missing, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing,
                           Type.Missing, Type.Missing, Type.Missing);
                break;

            default:
                doc.Invoke("SaveAs", fileName, XlFileFormat.xlOpenXMLWorkbook, Type.Missing, Type.Missing, Type.Missing,
                           Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                           Type.Missing, Type.Missing, Type.Missing);
                break;
            }
            this._application.SetBool("DisplayAlerts", bDisplayAlerts);

            return(doc);
        }
示例#3
0
        /// <summary>
        /// Initializes Word12Format field
        /// </summary>
        private int FindWord12SaveFormat()
        {
            int saveFormat = -1;

            try
            {
                if (_officeVersion >= OfficeVersion.Office2007)
                {
                    saveFormat = 12;
                }
                else
                {
                    // iterate through file converters to find the correct format
                    LateBindingObject converters = _application.Invoke("FileConverters");

                    for (int i = 1; i <= converters.GetInt32("Count"); i++)
                    {
                        LateBindingObject converter = converters.Invoke("Item", i);
                        string            className = converter.GetString("ClassName");
                        if (className.Equals(Word12Class))
                        {
                            // Converter found
                            saveFormat = converter.GetInt32("SaveFormat");
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.ToString());
                throw;
            }
            return(saveFormat);
        }
示例#4
0
 private LateBindingObject SaveCopyAs(LateBindingObject doc, string fileName)
 {
     switch (this._officeVersion)
     {
     default:
         doc.Invoke("SaveCopyAs", fileName, PpSaveAsFileType.ppSaveAsTemplate, MsoTriState.msoTrue);
         break;
     }
     return(doc);
 }
示例#5
0
        /// <summary>
        /// Read an ODF file.
        /// </summary>
        public override bool importOdfFile(string odfFileName)
        {
            try
            {
                bool isTemplate = Path.GetExtension(odfFileName).ToUpper().Equals(".OTT");
                // create a temporary file
                string outputExtension = isTemplate ? ".dotx" : ".docx";
                string outputFileName  = this._addinLib.GetTempFileName(odfFileName, outputExtension);

                this._application.Invoke("System").SetInt32("Cursor", (int)WdCursorType.wdCursorWait);

                ConversionOptions options = new ConversionOptions();
                options.InputFullName       = odfFileName;
                options.OutputFullName      = outputFileName;
                options.ConversionDirection = ConversionDirection.OdtToDocx;
                options.Generator           = this.GetGenerator();
                options.DocumentType        = isTemplate ? DocumentType.Template : DocumentType.Document;
                options.ShowProgress        = true;
                options.ShowUserInterface   = true;

                this._addinLib.OdfToOox(odfFileName, outputFileName, options);

                this._application.Invoke("System").SetInt32("Cursor", (int)WdCursorType.wdCursorNormal);

                // open the document
                bool confirmConversions = false;
                bool readOnly           = true;
                bool addToRecentFiles   = false;
                bool isVisible          = true;
                bool openAndRepair      = false;

                // conversion may have been cancelled and file deleted.
                if (File.Exists((string)outputFileName))
                {
                    LateBindingObject doc = OpenDocument(outputFileName, confirmConversions, readOnly, addToRecentFiles, isVisible, openAndRepair);

                    // update document fields
                    //doc.Invoke("Fields").Invoke("Update");

                    // and activate it
                    doc.Invoke("Activate");
                    doc.Invoke("Windows").Invoke("Item", 1).Invoke("Activate");
                }
                return(true);
            }
            catch (Exception ex)
            {
                _application.Invoke("System").SetInt32("Cursor", (int)WdCursorType.wdCursorNormal);
                System.Diagnostics.Trace.WriteLine(ex.ToString());
                System.Windows.Forms.MessageBox.Show(this._addinLib.GetString("OdfUnexpectedError"), DialogBoxTitle, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Stop);
                return(false);
            }
        }
示例#6
0
        /// <summary>
        /// Read an ODF file.
        /// </summary>
        public override bool importOdfFile(string odfFile)
        {
            try
            {
                bool isTemplate = Path.GetExtension(odfFile).ToUpper().Equals(".OTS");
                // create a temporary file
                string outputExtension = isTemplate ? ".xltx" : ".xlsx";
                string fileName        = this._addinLib.GetTempFileName(odfFile, outputExtension);

                ConversionOptions options = new ConversionOptions();
                options.InputFullName       = odfFile;
                options.OutputFullName      = fileName;
                options.ConversionDirection = ConversionDirection.OdsToXlsx;
                options.Generator           = this.GetGenerator();
                options.DocumentType        = isTemplate ? DocumentType.Template : DocumentType.Document;
                options.ShowProgress        = true;
                options.ShowUserInterface   = true;

                this._addinLib.OdfToOox(odfFile, fileName, options);

                // open the document
                bool confirmConversions = false;
                bool readOnly           = true;
                bool addToRecentFiles   = false;
                bool isVisible          = true;
                bool openAndRepair      = false;

                // conversion may have been cancelled and file deleted.
                if (File.Exists((string)fileName))
                {
                    // Workaround to excel bug. "Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))"
                    System.Globalization.CultureInfo ci;
                    ci = System.Threading.Thread.CurrentThread.CurrentCulture;
                    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

                    LateBindingObject doc = OpenDocument(fileName, confirmConversions, readOnly, addToRecentFiles, isVisible, openAndRepair);

                    // and activate it
                    doc.Invoke("Activate");

                    System.Threading.Thread.CurrentThread.CurrentCulture = ci;
                }
                return(true);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.ToString());
                System.Windows.Forms.MessageBox.Show(this._addinLib.GetString("OdfUnexpectedError"), DialogBoxTitle, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Stop);
                return(false);
            }
        }
示例#7
0
        private LateBindingObject OpenDocument(string fileName, bool confirmConversions, bool readOnly, bool addToRecentFiles, bool isVisible, bool openAndRepair)
        {
            LateBindingObject doc = null;

            // do not show warning message "Because this file was created in a newer version of Word, it has been converted to a format that you can work with."
            // see: http://support.microsoft.com/kb/936695
            using (RegistryKey key = Registry.CurrentUser.CreateSubKey(Connect.OptionKey))
            {
                int noShowCnvMsg = (key.GetValue(Connect.NoShowCnvMsg, 2) as int?) ?? 2;

                try
                {
                    key.SetValue(Connect.NoShowCnvMsg, 1, RegistryValueKind.DWord);

                    switch (this._officeVersion)
                    {
                    case OfficeVersion.Office2000:
                        doc = _application.Invoke("Documents").
                              Invoke("Open", fileName, confirmConversions, readOnly, addToRecentFiles, Type.Missing,
                                     Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                     isVisible);

                        break;

                    case OfficeVersion.OfficeXP:
                        doc = this._application.Invoke("Documents").
                              Invoke("Open", fileName, confirmConversions, readOnly, addToRecentFiles, Type.Missing,
                                     Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                     isVisible, openAndRepair, Type.Missing, Type.Missing);
                        break;

                    default:
                        doc = this._application.Invoke("Documents").
                              Invoke("Open", fileName, confirmConversions, readOnly, addToRecentFiles, Type.Missing,
                                     Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                     isVisible, openAndRepair, Type.Missing, Type.Missing, Type.Missing);
                        break;
                    }
                }
                finally
                {
                    if (noShowCnvMsg != 1)
                    {
                        // restore default behavior
                        key.SetValue(Connect.NoShowCnvMsg, 2, RegistryValueKind.DWord);
                    }
                }
            }
            return(doc);
        }
示例#8
0
        private LateBindingObject SaveDocumentAs(LateBindingObject doc, string fileName)
        {
            switch (this._officeVersion)
            {
            case OfficeVersion.Office2007:
            case OfficeVersion.Office2010:
                doc.Invoke("SaveAs", fileName, PpSaveAsFileType.ppSaveAsOpenXMLPresentation, MsoTriState.msoFalse);
                break;

            default:
                doc.Invoke("SaveAs", fileName, PpSaveAsFileType.ppSaveAsDefault, Type.Missing);
                break;
            }
            return(doc);
        }
示例#9
0
        private LateBindingObject OpenPresentation(string fileName, object confirmConversions, object readOnly, object addToRecentFiles)
        {
            LateBindingObject doc = null;

            switch (this._officeVersion)
            {
            case OfficeVersion.OfficeXP:
                doc = this._application.Invoke("Presentations").
                      Invoke("Open", fileName, Type.Missing, Type.Missing, Type.Missing);
                break;

            default:
                doc = this._application.Invoke("Presentations").
                      Invoke("Open", fileName, MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoFalse);
                break;
            }
            return(doc);
        }
示例#10
0
        private LateBindingObject OpenDocument(string fileName, bool confirmConversions, bool readOnly, bool addToRecentFiles, bool isVisible, bool openAndRepair)
        {
            LateBindingObject doc = null;

            switch (this._officeVersion)
            {
            case OfficeVersion.OfficeXP:
                doc = this._application.Invoke("Presentations").
                      Invoke("Open", fileName, Type.Missing, Type.Missing, Type.Missing);
                break;

            default:
                doc = this._application.Invoke("Presentations").
                      Invoke("Open", fileName, Type.Missing, Type.Missing, Type.Missing);
                break;
            }
            return(doc);
        }
示例#11
0
        /// <summary>
        /// Read an ODF file.
        /// </summary>
        public override bool importOdfFile(string odfFileName)
        {
            try
            {
                bool isTemplate = Path.GetExtension(odfFileName).ToUpper().Equals(".OTP");
                // create a temporary file
                string outputExtension = isTemplate ? ".potx" : ".pptx";
                string fileName        = this._addinLib.GetTempFileName(odfFileName, outputExtension);

                ConversionOptions options = new ConversionOptions();
                options.InputFullName       = odfFileName;
                options.OutputFullName      = fileName;
                options.ConversionDirection = ConversionDirection.OdpToPptx;
                options.Generator           = this.GetGenerator();
                options.DocumentType        = isTemplate ? DocumentType.Template : DocumentType.Document;
                options.ShowProgress        = true;
                options.ShowUserInterface   = true;

                this._addinLib.OdfToOox(odfFileName, fileName, options);

                // open the document
                bool confirmConversions = false;
                bool readOnly           = true;
                bool addToRecentFiles   = false;
                bool isVisible          = true;
                bool openAndRepair      = false;

                // conversion may have been cancelled and file deleted.
                if (File.Exists((string)fileName))
                {
                    LateBindingObject doc = OpenDocument(fileName, confirmConversions, readOnly, addToRecentFiles, isVisible, openAndRepair);
                }
                return(true);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.ToString());
                System.Windows.Forms.MessageBox.Show(this._addinLib.GetString("OdfUnexpectedError"), DialogBoxTitle, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Stop);
                return(false);
            }
        }
示例#12
0
        public OfficeApplication(string progId, string progIdAddin)
        {
            _progIdAddin = progIdAddin;
            _type        = Type.GetTypeFromProgID(progId);
            object instance = null;

            try
            {
                instance = Marshal.GetActiveObject(progId);
            }
            catch (COMException)
            {
                Trace.WriteLine(string.Format("{0} not running yet.", progId));
            }

            if (instance == null)
            {
                instance = Activator.CreateInstance(_type);
            }
            _application = new LateBindingObject(instance);
            _application.SetBool("Visible", true);
        }
示例#13
0
        /// <summary>
        /// Save as ODF.
        /// </summary>
        public override bool ExportOdf()
        {
            System.Globalization.CultureInfo ci;
            ci = System.Threading.Thread.CurrentThread.CurrentCulture;
            try
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
                LateBindingObject doc = _application.Invoke("ActiveWorkbook");

                // the second test deals with blank documents
                // (which are in a 'saved' state and have no extension yet(?))
                if (!doc.GetBool("Saved") ||
                    doc.GetString("FullName").IndexOf('.') < 0 ||
                    doc.GetString("FullName").IndexOf("http://") == 0 ||
                    doc.GetString("FullName").IndexOf("https://") == 0 ||
                    doc.GetString("FullName").IndexOf("ftp://") == 0
                    )
                {
                    System.Windows.Forms.MessageBox.Show(_addinLib.GetString("OdfSaveDocumentBeforeExport"), DialogBoxTitle, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Stop);
                    return(false);
                }
                else
                {
                    System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();

                    sfd.AddExtension = true;
                    sfd.DefaultExt   = "ods";
                    sfd.Filter       = this._addinLib.GetString(this.OdfFileType) + this.ExportOdfFileFilter
                                       + this._addinLib.GetString(ALL_FILE_TYPE) + this.ExportAllFileFilter;
                    sfd.InitialDirectory = doc.GetString("Path");
                    sfd.OverwritePrompt  = true;
                    sfd.Title            = this._addinLib.GetString(EXPORT_LABEL);
                    string ext = '.' + sfd.DefaultExt;
                    sfd.FileName = doc.GetString("FullName").Substring(0, doc.GetString("FullName").LastIndexOf('.')) + ext;

                    // process the chosen documents
                    if (System.Windows.Forms.DialogResult.OK == sfd.ShowDialog())
                    {
                        // name of the file to create
                        string odfFileName = sfd.FileName;
                        // multi dotted extensions support
                        if (!odfFileName.EndsWith(ext))
                        {
                            odfFileName += ext;
                        }
                        // name of the document to convert
                        string sourceFileName = doc.GetString("FullName");
                        // name of the temporary Word12 file created if current file is not already a Word12 document
                        string tempXlsxName = null;

                        if (!(doc.GetInt32("FileFormat") == (int)XlFileFormat.xlOpenXMLWorkbook ||
                              doc.GetInt32("FileFormat") == (int)XlFileFormat.xlOpenXMLWorkbookMacroEnabled ||
                              doc.GetInt32("FileFormat") == (int)XlFileFormat.xlOpenXMLTemplate ||
                              doc.GetInt32("FileFormat") == (int)XlFileFormat.xlOpenXMLTemplateMacroEnabled ||
                              doc.GetInt32("FileFormat") == (int)XlFileFormat.xlExcel12))
                        {
                            // if file is not currently in Excel12 format
                            // 1. Create a copy
                            // 2. Open it and do a "Save as Excel12" (copy needed not to perturb current openened document
                            // 3. Convert the Excel12 copy to ODF
                            // 4. Remove both temporary created files

                            // duplicate the file to keep current file "as is"
                            string tempCopyName = Path.GetTempFileName() + Path.GetExtension((string)sourceFileName);
                            File.Copy((string)sourceFileName, (string)tempCopyName);

                            //BUG FIX #1743469
                            FileInfo fi = new FileInfo(tempCopyName);
                            if (fi.IsReadOnly)
                            {
                                fi.IsReadOnly = false;
                            }
                            //BUG FIX #1743469

                            // open the duplicated file
                            bool confirmConversions = false;
                            bool readOnly           = false;
                            bool addToRecentFiles   = false;
                            bool isVisible          = false;

                            LateBindingObject newDoc = OpenDocument(tempCopyName, confirmConversions, readOnly, addToRecentFiles, isVisible, false);

                            newDoc.Invoke("Windows").Invoke("Item", 1).SetBool("Visible", false);

                            // generate xlsx file from the duplicated file (under a temporary file)
                            tempXlsxName = this._addinLib.GetTempPath((string)sourceFileName, ".xlsx");

                            SaveDocumentAs(newDoc, tempXlsxName);

                            // close and remove the duplicated file
                            newDoc.Invoke("Close", WdSaveOptions.wdDoNotSaveChanges, WdOriginalFormat.wdOriginalDocumentFormat, Type.Missing);

                            //BUG FIX #1743469
                            try
                            {
                                File.Delete((string)tempCopyName);
                            }
                            catch (Exception ex)
                            {
                                //If delete does not work, don't stop the rest of the process
                                //The tempFile will be deleted by the system
                                System.Diagnostics.Trace.WriteLine(ex.ToString());
                            }
                            //BUG FIX #1743469

                            // Now the file to be converted is
                            sourceFileName = tempXlsxName;
                        }

                        ConversionOptions options = new ConversionOptions();
                        options.InputFullName         = sourceFileName;
                        options.InputFullNameOriginal = doc.GetString("FullName");
                        options.OutputFullName        = odfFileName;
                        options.ConversionDirection   = ConversionDirection.XlsxToOds;
                        options.Generator             = this.GetGenerator();
                        options.DocumentType          = Path.GetExtension(odfFileName).ToUpper().Equals(".OTS") ? DocumentType.Template : DocumentType.Document;
                        options.ShowProgress          = true;
                        options.ShowUserInterface     = true;

                        this._addinLib.OoxToOdf(sourceFileName, odfFileName, options);

                        if (tempXlsxName != null && File.Exists((string)tempXlsxName))
                        {
                            this._addinLib.DeleteTempPath((string)tempXlsxName);
                        }
                    }
                    return(true);
                }
            }
            finally
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = ci;
            }
        }
示例#14
0
        /// <summary>
        /// Save as ODF.
        /// </summary>
        public override bool ExportOdf()
        {
            // check if Word12 converter is installed
            if (_word12SaveFormat == -1)
            {
                System.Windows.Forms.MessageBox.Show(_addinLib.GetString("OdfConverterNotInstalled"), DialogBoxTitle, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Stop);
                return(false);
            }

            LateBindingObject doc = _application.Invoke("ActiveDocument");

            // the second test deals with blank documents
            // (which are in a 'saved' state and have no extension yet(?))
            if (!doc.GetBool("Saved") ||
                doc.GetString("FullName").IndexOf('.') < 0 ||
                doc.GetString("FullName").IndexOf("http://") == 0 ||
                doc.GetString("FullName").IndexOf("https://") == 0 ||
                doc.GetString("FullName").IndexOf("ftp://") == 0
                )
            {
                System.Windows.Forms.MessageBox.Show(_addinLib.GetString("OdfSaveDocumentBeforeExport"), DialogBoxTitle, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Stop);
                return(false);
            }
            else
            {
                System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();

                sfd.AddExtension = true;
                sfd.DefaultExt   = "odt";
                sfd.Filter       = this._addinLib.GetString(this.OdfFileType) + this.ExportOdfFileFilter
                                   + this._addinLib.GetString(ODF_FILE_TYPE_OTT) + EXPORT_ODF_FILE_FILTER_OTT
                                   + this._addinLib.GetString(ALL_FILE_TYPE) + this.ExportAllFileFilter;
                sfd.InitialDirectory             = doc.GetString("Path");
                sfd.OverwritePrompt              = true;
                sfd.SupportMultiDottedExtensions = true;
                sfd.Title = this._addinLib.GetString(EXPORT_LABEL);
                string ext = '.' + sfd.DefaultExt;
                sfd.FileName = doc.GetString("FullName").Substring(0, doc.GetString("FullName").LastIndexOf('.')); // +ext;

                // process the chosen documents
                if (System.Windows.Forms.DialogResult.OK == sfd.ShowDialog())
                {
                    // name of the file to create
                    string odfFileName = sfd.FileName;
                    // multi dotted extensions support
                    // Note: sfd.FilterIndex is 1-based
                    if (!odfFileName.ToLower().EndsWith(".odt") && sfd.FilterIndex == 1)
                    {
                        odfFileName += ".odt";
                    }
                    else if (!odfFileName.ToLower().EndsWith(".ott") && sfd.FilterIndex == 2)
                    {
                        odfFileName += ".ott";
                    }
                    // name of the document to convert
                    string sourceFileName = doc.GetString("FullName");
                    // name of the temporary Word12 file created if current file is not already a Word12 document
                    string tempDocxName = null;
                    // store current cursor
                    WdCursorType currentCursor = (WdCursorType)_application.Invoke("System").GetInt32("Cursor");
                    // display hourglass
                    this._application.Invoke("System").SetInt32("Cursor", (int)WdCursorType.wdCursorWait);


                    if (!(doc.GetInt32("SaveFormat") == (int)WdSaveFormat.wdFormatXMLDocument ||
                          doc.GetInt32("SaveFormat") == (int)WdSaveFormat.wdFormatXMLDocumentMacroEnabled ||
                          doc.GetInt32("SaveFormat") == (int)WdSaveFormat.wdFormatXMLTemplate ||
                          doc.GetInt32("SaveFormat") == (int)WdSaveFormat.wdFormatXMLTemplateMacroEnabled))
                    {
                        try
                        {
                            // if file is not currently in Word12 format
                            // 1. Create a copy
                            // 2. Open it and do a "Save as Word12" (copy needed not to perturb current openened document
                            // 3. Convert the Word12 copy to ODF
                            // 4. Remove both temporary created files

                            // duplicate the file to keep current file "as is"
                            string tempCopyName = Path.GetTempFileName() + Path.GetExtension((string)sourceFileName);
                            File.Copy((string)sourceFileName, (string)tempCopyName);

                            //BUG FIX #1743469
                            FileInfo fi = new FileInfo(tempCopyName);
                            if (fi.IsReadOnly)
                            {
                                fi.IsReadOnly = false;
                            }
                            //BUG FIX #1743469

                            // open the duplicated file
                            bool confirmConversions = false;
                            bool readOnly           = false;
                            bool addToRecentFiles   = false;
                            bool isVisible          = false;

                            LateBindingObject newDoc = OpenDocument(tempCopyName, confirmConversions, readOnly, addToRecentFiles, isVisible, false);

                            newDoc.Invoke("Windows").Invoke("Item", 1).SetBool("Visible", false);

                            // generate docx file from the duplicated file (under a temporary file)
                            string outputExtension =
                                tempDocxName = this._addinLib.GetTempPath((string)sourceFileName, ".docx");

                            SaveDocumentAs(newDoc, tempDocxName);

                            // close and remove the duplicated file
                            newDoc.Invoke("Close", WdSaveOptions.wdDoNotSaveChanges, WdOriginalFormat.wdOriginalDocumentFormat, Type.Missing);

                            //BUG FIX #1743469
                            try
                            {
                                File.Delete((string)tempCopyName);
                            }
                            catch (Exception ex)
                            {
                                //If delete does not work, don't stop the rest of the process
                                //The tempFile will be deleted by the system
                                System.Diagnostics.Trace.WriteLine(ex.ToString());
                            }
                            //BUG FIX #1743469

                            // Now the file to be converted is
                            sourceFileName = tempDocxName;
                        }
                        catch (Exception ex)
                        {
                            _application.Invoke("System").SetInt32("Cursor", (int)currentCursor);

                            String lMsg;

                            lMsg = _addinLib.GetString("OdfExportErrorTryDocxFirst");
                            System.Windows.Forms.MessageBox.Show(lMsg, DialogBoxTitle, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Stop);
                            System.Diagnostics.Trace.WriteLine(ex.ToString());
                            return(false);
                        }
                    }

                    ConversionOptions options = new ConversionOptions();
                    options.InputFullName         = sourceFileName;
                    options.OutputFullName        = odfFileName;
                    options.InputFullNameOriginal = doc.GetString("FullName");
                    options.ConversionDirection   = ConversionDirection.DocxToOdt;
                    options.Generator             = this.GetGenerator();
                    options.DocumentType          = Path.GetExtension(odfFileName).ToUpper().Equals(".OTT") ? DocumentType.Template : DocumentType.Document;
                    options.ShowProgress          = true;
                    options.ShowUserInterface     = true;

                    this._addinLib.OoxToOdf(sourceFileName, odfFileName, options);

                    if (tempDocxName != null && File.Exists((string)tempDocxName))
                    {
                        this._addinLib.DeleteTempPath((string)tempDocxName);
                    }
                    _application.Invoke("System").SetInt32("Cursor", (int)currentCursor);
                }
                return(true);
            }
        }
示例#15
0
        /// <summary>
        /// Save as ODF.
        /// </summary>
        public override bool ExportOdf()
        {
            LateBindingObject doc = _application.Invoke("ActivePresentation");

            // the second test deals with blank documents
            // (which are in a 'saved' state and have no extension yet(?))
            if (!doc.GetBool("Saved") ||
                doc.GetString("FullName").IndexOf('.') < 0 ||
                doc.GetString("FullName").IndexOf("http://") == 0 ||
                doc.GetString("FullName").IndexOf("https://") == 0 ||
                doc.GetString("FullName").IndexOf("ftp://") == 0
                )
            {
                System.Windows.Forms.MessageBox.Show(_addinLib.GetString("OdfSaveDocumentBeforeExport"), DialogBoxTitle, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Stop);
                return(false);
            }
            else
            {
                System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();

                sfd.AddExtension = true;
                sfd.DefaultExt   = "odp";
                sfd.Filter       = this._addinLib.GetString(this.OdfFileType) + this.ExportOdfFileFilter
                                   + this._addinLib.GetString(ALL_FILE_TYPE) + this.ExportAllFileFilter;
                sfd.InitialDirectory = doc.GetString("Path");
                sfd.OverwritePrompt  = true;
                sfd.Title            = this._addinLib.GetString(EXPORT_LABEL);
                string ext = '.' + sfd.DefaultExt;
                sfd.FileName = doc.GetString("FullName").Substring(0, doc.GetString("FullName").LastIndexOf('.')) + ext;
                // process the chosen documents
                if (System.Windows.Forms.DialogResult.OK == sfd.ShowDialog())
                {
                    // name of the file to create
                    string odfFileName = sfd.FileName;
                    // multi dotted extensions support
                    if (!odfFileName.EndsWith(ext))
                    {
                        odfFileName += ext;
                    }
                    // name of the document to convert
                    string sourceFileName = doc.GetString("FullName");

                    object tmpFileName = null;

                    if (!(Path.GetExtension(doc.GetString("FullName")).Equals(".pptx") ||
                          Path.GetExtension(doc.GetString("FullName")).Equals(".pptm") ||
                          Path.GetExtension(doc.GetString("FullName")).Equals(".potx") ||
                          Path.GetExtension(doc.GetString("FullName")).Equals(".potm")))
                    {
                        // open the duplicated file
                        object            confirmConversions = false;
                        object            readOnly           = false;
                        object            addToRecentFiles   = false;
                        object            isVisible          = false;
                        LateBindingObject newDoc;

                        switch (this._officeVersion)
                        {
                        case OfficeVersion.OfficeXP:
                            string path = Microsoft.Win32.Registry.GetValue(@"HKEY_CLASSES_ROOT\Powerpoint.Show.12\Shell\Save As\Command", null, null) as string;
                            if (string.IsNullOrEmpty(path))
                            {
                                System.Windows.Forms.MessageBox.Show(_addinLib.GetString("OdfConverterNotInstalled"), DialogBoxTitle, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Stop);
                            }
                            else
                            {
                                tmpFileName = this._addinLib.GetTempFileName((string)sourceFileName, ".pptx");
                                path        = path.ToLower();
                                int start = path.IndexOf("moc.exe");
                                path = path.Substring(0, start) + "ppcnvcom.exe";
                                string  parms = "-oice \"" + doc.GetString("FullName") + "\" \"" + tmpFileName + "\"";
                                Process p     = Process.Start(path, parms);
                                p.WaitForExit();
                                sourceFileName = (string)tmpFileName;
                            }
                            break;

                        case OfficeVersion.Office2003:
                            // setting defaultformat value in registry
                            int oldFormat = SetPPTXasDefault();

                            tmpFileName = this._addinLib.GetTempPath((string)sourceFileName, ".ppt");

                            SaveCopyAs(doc, (string)tmpFileName);

                            newDoc = OpenPresentation((string)tmpFileName, confirmConversions, readOnly, addToRecentFiles);

                            SaveDocumentAs(newDoc, (string)tmpFileName);

                            sourceFileName = (string)tmpFileName;

                            // removing defaultformat value in registry
                            RestoreDefault(oldFormat);
                            break;

                        case OfficeVersion.Office2007:
                        case OfficeVersion.Office2010:
                            // duplicate the file
                            object newName = Path.GetTempFileName() + Path.GetExtension((string)sourceFileName);
                            File.Copy((string)sourceFileName, (string)newName);

                            newDoc = OpenPresentation((string)newName, confirmConversions, readOnly, addToRecentFiles);

                            // generate openxml file from the duplicated file (under a temporary file)
                            tmpFileName = this._addinLib.GetTempPath((string)sourceFileName, ".pptx");

                            SaveDocumentAs(newDoc, (string)tmpFileName);

                            sourceFileName = (string)tmpFileName;
                            break;
                        }
                    }

                    ConversionOptions options = new ConversionOptions();
                    options.InputFullName         = sourceFileName;
                    options.InputFullNameOriginal = doc.GetString("FullName");
                    options.OutputFullName        = odfFileName;
                    options.ConversionDirection   = ConversionDirection.PptxToOdp;
                    options.Generator             = this.GetGenerator();
                    options.DocumentType          = Path.GetExtension(odfFileName).ToUpper().Equals(".OTP") ? DocumentType.Template : DocumentType.Document;
                    options.ShowProgress          = true;
                    options.ShowUserInterface     = true;
                    this._addinLib.OoxToOdf(sourceFileName, odfFileName, options);

                    if (tmpFileName != null && File.Exists((string)tmpFileName))
                    {
                        this._addinLib.DeleteTempPath((string)tmpFileName);
                    }
                }
                return(true);
            }
        }