Esempio n. 1
0
        private void doOpen(CodeActivityContext context)
        {
            var readPassword = ReadPassword.Get(context);

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

            if (string.IsNullOrEmpty(writePassword))
            {
                writePassword = null;
            }
            if (!string.IsNullOrEmpty(filename))
            {
                foreach (Microsoft.Office.Interop.Excel.Workbook w in officewrap.application.Workbooks)
                {
                    if (w.FullName == filename)
                    {
                        try
                        {
                            workbook  = w;
                            worksheet = workbook.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet;
                            break;
                        }
                        catch (Exception)
                        {
                            workbook = null;
                        }
                    }
                }
                if (workbook == null)
                {
                    officewrap.application.DisplayAlerts = false;
                    //application.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityLow;
                    if (System.IO.File.Exists(filename))
                    {
                        workbook = officewrap.application.Workbooks.Open(filename, ReadOnly: false,
                                                                         Password: readPassword, WriteResPassword: writePassword);
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(filename))
                        {
                            workbook = officewrap.application.Workbooks.Add();
                            workbook.Activate();
                            //workbook.SaveCopyAs(filename);
                            workbook.SaveAs(Filename: filename);
                        }
                    }
                    officewrap.application.DisplayAlerts = true;
                }
            }
            if (workbook == null)
            {
                workbook = officewrap.application.ActiveWorkbook;
            }
        }
Esempio n. 2
0
        protected override void Execute(NativeActivityContext context)
        {
            var readPassword = ReadPassword.Get(context);

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

            if (string.IsNullOrEmpty(writePassword))
            {
                writePassword = null;
            }
            filename = Filename.Get(context);
            officewrap.application.Visible = true;
            // officewrap.application.Visible = Visible.Get(context);
            if (!string.IsNullOrEmpty(filename))
            {
                filename = Environment.ExpandEnvironmentVariables(filename);
            }
            workbook = (Workbook != null ? Workbook.Get(context) : null);
            if (!string.IsNullOrEmpty(filename) && workbook != null)
            {
                if (workbook.FullName.ToLower() != filename.ToLower())
                {
                    try
                    {
                        workbook.Close(true);
                        workbook = null;
                        Task.Run(() => { GC.Collect(); });
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
            }
            if (!string.IsNullOrEmpty(filename) && workbook == null)
            {
                foreach (Microsoft.Office.Interop.Excel.Workbook w in officewrap.application.Workbooks)
                {
                    if (w.FullName == filename)
                    {
                        workbook = w;
                        break;
                    }
                }
                if (workbook == null)
                {
                    officewrap.application.DisplayAlerts = false;
                    //application.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityLow;
                    if (System.IO.File.Exists(filename))
                    {
                        //workbook = officewrap.application.Workbooks.Open(filename, ReadOnly: false);
                        workbook = officewrap.application.Workbooks.Open(filename, ReadOnly: false,
                                                                         Password: readPassword, WriteResPassword: writePassword);
                    }
                    else
                    {
                        workbook = officewrap.application.Workbooks.Add();
                        workbook.Activate();
                        //workbook.SaveCopyAs(filename);
                        workbook.SaveAs(Filename: filename);
                    }
                    officewrap.application.DisplayAlerts = true;
                }
            }
            if (workbook == null)
            {
                workbook = officewrap.application.ActiveWorkbook;
            }
            if (workbook == null)
            {
            }
            var _worksheet = (Worksheet != null ? Worksheet.Get(context) : null);

            worksheet = workbook.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet;
            if (!string.IsNullOrEmpty(_worksheet))
            {
                foreach (object obj in workbook.Sheets)
                {
                    Worksheet s = obj as Worksheet;
                    if (s != null && s.Name == _worksheet)
                    {
                        s.Activate();
                        worksheet = s;
                    }
                }
            }
            var sheetPassword = SheetPassword.Get(context);

            if (string.IsNullOrEmpty(sheetPassword))
            {
                sheetPassword = null;
            }
            if (!string.IsNullOrEmpty(sheetPassword) && worksheet != null)
            {
                worksheet.Unprotect(sheetPassword);
            }
            //Application.Set(context, application);
            Workbook.Set(context, workbook);
        }
Esempio 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 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();
                }
            }
        }
Esempio n. 4
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;
            }
        }
Esempio n. 5
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();
            }
        }