Exemplo n.º 1
0
        protected override void Execute(CodeActivityContext context)
        {
            var readPassword = ReadPassword.Get(context);

            if (string.IsNullOrEmpty(readPassword))
            {
                readPassword = null;
            }
            var writePassword = WritePassword.Get(context);

            if (string.IsNullOrEmpty(writePassword))
            {
                writePassword = null;
            }
            var removeReadPassword  = RemoveReadPassword.Get(context);
            var removeWritePassword = RemoveWritePassword.Get(context);

            var workbook    = Workbook.Get(context);
            var filename    = Filename.Get(context);
            var saveChanges = SaveChanges.Get(context);

            if (!string.IsNullOrEmpty(filename))
            {
                filename = Environment.ExpandEnvironmentVariables(filename);
            }
            if (string.IsNullOrEmpty(filename))
            {
                workbook.Close(saveChanges);
            }
            else
            {
                int  workbookcount = 0;
                bool foundit       = false;
                foreach (Microsoft.Office.Interop.Excel.Workbook w in officewrap.application.Workbooks)
                {
                    if (w.FullName == filename || string.IsNullOrEmpty(filename))
                    {
                        try
                        {
                            officewrap.application.DisplayAlerts = false;
                            workbook = w;
                            if (!string.IsNullOrEmpty(readPassword))
                            {
                                w.Password = readPassword;
                            }
                            if (removeReadPassword)
                            {
                                w.Password = ""; readPassword = "";
                            }
                            if (!string.IsNullOrEmpty(writePassword))
                            {
                                w.WritePassword = writePassword;
                            }
                            if (removeWritePassword)
                            {
                                w.WritePassword = ""; writePassword = "";
                            }
                            w.SaveAs(Filename: filename, Password: readPassword, WriteResPassword: writePassword);
                            w.Close();
                            officewrap.application.DisplayAlerts = true;
                            foundit = true;
                            //worksheet = workbook.ActiveSheet;
                            break;
                        }
                        catch (Exception)
                        {
                            workbook = null;
                        }
                    }
                }
                Microsoft.Office.Interop.Excel.Workbook tempworkbook = officewrap.application.ActiveWorkbook;
                if (!foundit && tempworkbook != null)
                {
                    officewrap.application.DisplayAlerts = false;
                    if (saveChanges && !string.IsNullOrEmpty(filename))
                    {
                        tempworkbook.SaveAs(Filename: filename, Password: readPassword, WriteResPassword: writePassword);
                        tempworkbook.Close();
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(readPassword))
                        {
                            tempworkbook.Password = readPassword;
                        }
                        if (removeReadPassword)
                        {
                            tempworkbook.Password = "";
                        }
                        if (!string.IsNullOrEmpty(writePassword))
                        {
                            tempworkbook.WritePassword = writePassword;
                        }
                        if (removeWritePassword)
                        {
                            tempworkbook.WritePassword = "";
                        }
                        tempworkbook.Save();
                        tempworkbook.Close(saveChanges);
                    }
                    officewrap.application.DisplayAlerts = true;
                }
                if (workbookcount == 0 || string.IsNullOrEmpty(filename))
                {
                    officewrap.application.Quit();
                }
            }
        }
Exemplo n.º 2
0
        protected override void Execute(CodeActivityContext context)
        {
            var readPassword = ReadPassword.Get(context);

            if (string.IsNullOrEmpty(readPassword))
            {
                readPassword = null;
            }
            var writePassword = WritePassword.Get(context);

            if (string.IsNullOrEmpty(writePassword))
            {
                writePassword = null;
            }
            var removeReadPassword  = RemoveReadPassword.Get(context);
            var removeWritePassword = RemoveWritePassword.Get(context);

            var formattype = Type.Get(context);
            var document   = Document.Get(context);
            var filename   = Filename.Get(context);

            filename = Environment.ExpandEnvironmentVariables(filename);
            var saveChanges = SaveChanges.Get(context);

            Application activeObject = null;

            try
            {
                activeObject = (Application)Marshal.GetActiveObject("Word.Application");
                if (!string.IsNullOrEmpty(filename))
                {
                    foreach (Document current in activeObject.Documents)
                    {
                        if (current.FullName == filename)
                        {
                            document = current;
                            document.Close(saveChanges);
                            break;
                        }
                    }
                }
            }
            catch
            {
                activeObject = null;
            }
            finally
            {
                if (activeObject == null)
                {
                    activeObject = (Application)Activator.CreateInstance(Marshal.GetTypeFromCLSID(new Guid("000209FF-0000-0000-C000-000000000046")));
                }
                activeObject.Visible = true;
            }
            int doccount = 0;

            foreach (Document current in activeObject.Documents)
            {
                if (current.FullName == filename)
                {
                    doccount++;
                    break;
                }
            }
            object missing   = System.Type.Missing;
            object ofilename = filename;

            if (document == null)
            {
                document = activeObject.Documents.Open(ref ofilename, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            }
            if (document != null)
            {
                if (string.IsNullOrEmpty(filename))
                {
                    filename = document.FullName;
                }
                officewrap.application.DisplayAlerts = false;
                if (!string.IsNullOrEmpty(readPassword))
                {
                    document.Password = readPassword; saveChanges = true;
                }
                if (removeReadPassword)
                {
                    document.Password = ""; saveChanges = true;
                }
                if (!string.IsNullOrEmpty(writePassword))
                {
                    document.WritePassword = writePassword; saveChanges = true;
                }
                if (removeWritePassword)
                {
                    document.WritePassword = ""; saveChanges = true;
                }
                var ext = System.IO.Path.GetExtension(filename);
                if (ext.ToLower() != ".xps" && formattype == "1")
                {
                    filename = System.IO.Path.ChangeExtension(filename, "xps");
                }
                else if (ext.ToLower() != ".pdf" && formattype == "0")
                {
                    filename = System.IO.Path.ChangeExtension(filename, "pdf");
                }
                if (System.IO.File.Exists(filename))
                {
                    System.IO.File.Delete(filename);
                }
                if (formattype == "1")
                {
                    document.ExportAsFixedFormat(filename, WdExportFormat.wdExportFormatXPS);
                }
                else
                {
                    document.ExportAsFixedFormat(filename, WdExportFormat.wdExportFormatPDF);
                }
                document.Close(saveChanges);
                officewrap.application.DisplayAlerts = true;
            }
        }
Exemplo n.º 3
0
        protected override void Execute(CodeActivityContext context)
        {
            var readPassword = ReadPassword.Get(context);

            if (string.IsNullOrEmpty(readPassword))
            {
                readPassword = null;
            }
            var writePassword = WritePassword.Get(context);

            if (string.IsNullOrEmpty(writePassword))
            {
                writePassword = null;
            }
            var removeReadPassword  = RemoveReadPassword.Get(context);
            var removeWritePassword = RemoveWritePassword.Get(context);

            var formattype  = Type.Get(context);
            var workbook    = Workbook.Get(context);
            var filename    = Filename.Get(context);
            var saveChanges = SaveChanges.Get(context);

            if (!string.IsNullOrEmpty(filename))
            {
                filename = Environment.ExpandEnvironmentVariables(filename);
            }
            if (!string.IsNullOrEmpty(filename))
            {
                bool foundit = false;
                foreach (Microsoft.Office.Interop.Excel.Workbook w in officewrap.application.Workbooks)
                {
                    if (w.FullName == filename || string.IsNullOrEmpty(filename))
                    {
                        try
                        {
                            workbook = w;
                            foundit  = true;
                            //worksheet = workbook.ActiveSheet;
                            break;
                        }
                        catch (Exception)
                        {
                            workbook = null;
                        }
                    }
                }
                if (!foundit)
                {
                    Workbook tempworkbook = officewrap.application.ActiveWorkbook;
                    if (saveChanges && tempworkbook != null && (System.IO.Path.GetExtension(filename) != ".pdf" && System.IO.Path.GetExtension(filename) != ".xps"))
                    {
                        tempworkbook.SaveAs(Filename: filename, Password: readPassword, WriteResPassword: writePassword);
                        workbook = tempworkbook;
                    }
                }
            }
            if (workbook == null)
            {
                workbook = officewrap.application.ActiveWorkbook;
            }
            if (workbook != null)
            {
                if (string.IsNullOrEmpty(filename))
                {
                    filename = workbook.FullName;
                }
                officewrap.application.DisplayAlerts = false;
                if (!string.IsNullOrEmpty(readPassword))
                {
                    workbook.Password = readPassword; saveChanges = true;
                }
                if (removeReadPassword)
                {
                    workbook.Password = ""; saveChanges = true;
                }
                if (!string.IsNullOrEmpty(writePassword))
                {
                    workbook.WritePassword = writePassword; saveChanges = true;
                }
                if (removeWritePassword)
                {
                    workbook.WritePassword = ""; saveChanges = true;
                }
                var ext = System.IO.Path.GetExtension(filename);
                if (ext.ToLower() != ".xps" && formattype == "1")
                {
                    filename = System.IO.Path.ChangeExtension(filename, "xps");
                }
                else if (ext.ToLower() != ".pdf" && formattype == "0")
                {
                    filename = System.IO.Path.ChangeExtension(filename, "pdf");
                }
                if (System.IO.File.Exists(filename))
                {
                    System.IO.File.Delete(filename);
                }
                if (formattype == "1")
                {
                    workbook.ExportAsFixedFormat(XlFixedFormatType.xlTypeXPS, filename);
                }
                else
                {
                    workbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, filename);
                }
                workbook.Close(saveChanges);
                officewrap.application.DisplayAlerts = true;
            }

            if (officewrap.application.Workbooks.Count == 0)
            {
                officewrap.application.Quit();
            }
        }