Example #1
0
        private void ExecuteEvents(Timeline timeline, TimelineHandler handler)
        {
            try
            {
                foreach (TimelineEvent timelineEvent in handler.TimeLineEvents)
                {
                    try
                    {
                        _log.Trace($"Excel event - {timelineEvent}");
                        WorkingHours.Is(handler);

                        if (timelineEvent.DelayBefore > 0)
                        {
                            Thread.Sleep(timelineEvent.DelayBefore);
                        }

                        if (timeline != null)
                        {
                            var pids = ProcessManager.GetPids(ProcessManager.ProcessNames.Excel).ToList();
                            if (pids.Count > timeline.TimeLineHandlers.Count(o => o.HandlerType == HandlerType.Excel))
                            {
                                return;
                            }
                        }

                        // start excel and turn off msg boxes
                        Excel.Application excelApplication = new Excel.Application
                        {
                            DisplayAlerts = false,
                            Visible       = true
                        };

                        try
                        {
                            excelApplication.WindowState = XlWindowState.xlMinimized;
                            foreach (Excel.Workbook item in excelApplication.Workbooks)
                            {
                                item.Windows[1].WindowState = XlWindowState.xlMinimized;
                            }
                        }
                        catch (Exception e)
                        {
                            _log.Trace($"Could not minimize: {e}");
                        }

                        // create a utils instance, not need for but helpful to keep the lines of code low
                        CommonUtils utils = new CommonUtils(excelApplication);

                        _log.Trace("Excel adding workbook");
                        // add a new workbook
                        Excel.Workbook workBook = excelApplication.Workbooks.Add();
                        _log.Trace("Excel adding worksheet");
                        Excel.Worksheet workSheet = (Excel.Worksheet)workBook.Worksheets[1];

                        // draw back color and perform the BorderAround method
                        workSheet.Range("$B2:$B5").Interior.Color = utils.Color.ToDouble(Color.DarkGreen);
                        workSheet.Range("$B2:$B5").BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlMedium,
                                                                XlColorIndex.xlColorIndexAutomatic);

                        // draw back color and border the range explicitly
                        workSheet.Range("$D2:$D5").Interior.Color = utils.Color.ToDouble(Color.DarkGreen);
                        workSheet.Range("$D2:$D5")
                        .Borders[(Excel.Enums.XlBordersIndex)XlBordersIndex.xlInsideHorizontal]
                        .LineStyle = XlLineStyle.xlDouble;
                        workSheet.Range("$D2:$D5")
                        .Borders[(Excel.Enums.XlBordersIndex)XlBordersIndex.xlInsideHorizontal]
                        .Weight = 4;
                        workSheet.Range("$D2:$D5")
                        .Borders[(Excel.Enums.XlBordersIndex)XlBordersIndex.xlInsideHorizontal]
                        .Color = utils.Color.ToDouble(Color.Black);

                        var writeSleep = ProcessManager.Jitter(100);
                        Thread.Sleep(writeSleep);

                        workSheet.Cells[1, 1].Value = "We have 2 simple shapes created.";

                        string rand = RandomFilename.Generate();

                        string dir = timelineEvent.CommandArgs[0].ToString();
                        if (dir.Contains("%"))
                        {
                            dir = Environment.ExpandEnvironmentVariables(dir);
                        }

                        if (Directory.Exists(dir))
                        {
                            Directory.CreateDirectory(dir);
                        }

                        string path = $"{dir}\\{rand}.xlsx";

                        //if directory does not exist, create!
                        _log.Trace($"Checking directory at {path}");
                        DirectoryInfo f = new FileInfo(path).Directory;
                        if (f == null)
                        {
                            _log.Trace($"Directory does not exist, creating directory at {f.FullName}");
                            Directory.CreateDirectory(f.FullName);
                        }

                        try
                        {
                            if (File.Exists(path))
                            {
                                File.Delete(path);
                            }
                        }
                        catch (Exception e)
                        {
                            _log.Error($"Excel file delete exception: {e}");
                        }

                        _log.Trace($"Excel saving to path - {path}");
                        workBook.SaveAs(path);
                        FileListing.Add(path);
                        Report(handler.HandlerType.ToString(), timelineEvent.Command,
                               timelineEvent.CommandArgs[0].ToString());

                        if (timelineEvent.DelayAfter > 0)
                        {
                            //sleep and leave the app open
                            _log.Trace($"Sleep after for {timelineEvent.DelayAfter}");
                            Thread.Sleep(timelineEvent.DelayAfter - writeSleep);
                        }

                        // close excel and dispose reference
                        excelApplication.Quit();
                        excelApplication.Dispose();

                        try
                        {
                            if (excelApplication != null)
                            {
                                System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApplication);
                            }
                        }
                        catch (Exception e)
                        {
                            _log.Error($"Excel com release exception: {e}");
                        }

                        excelApplication = null;
                        workBook         = null;
                        workSheet        = null;

                        GC.Collect();
                    }
                    catch (Exception e)
                    {
                        _log.Error($"Excel handler exception: {e}");
                    }
                    finally
                    {
                        Thread.Sleep(5000);
                    }
                }
            }
            catch (Exception e)
            {
                _log.Error($"Excel execute events exception: {e}");
            }
            finally
            {
                KillApp();
                FileListing.FlushList();
                _log.Trace($"Excel closing after successfully kill and flush");
            }
        }
 /// <summary>Adds to file list stack.</summary>
 /// <param name="item">The item.</param>
 public void AddToFileListStack(string item) => FileListing.Add(item);
Example #3
0
        private void ExecuteEvents(Timeline timeline, TimelineHandler handler)
        {
            try
            {
                foreach (TimelineEvent timelineEvent in handler.TimeLineEvents)
                {
                    try
                    {
                        WorkingHours.Is(handler);

                        if (timelineEvent.DelayBefore > 0)
                        {
                            Thread.Sleep(timelineEvent.DelayBefore);
                        }

                        if (timeline != null)
                        {
                            var pids = ProcessManager.GetPids(ProcessManager.ProcessNames.Word).ToList();
                            if (pids.Count > timeline.TimeLineHandlers.Count(o => o.HandlerType == HandlerType.Word))
                            {
                                return;
                            }
                        }

                        // start word and turn off msg boxes
                        Word.Application wordApplication = new Word.Application
                        {
                            DisplayAlerts = WdAlertLevel.wdAlertsNone,
                            Visible       = true
                        };

                        // add a new document
                        Word.Document newDocument = wordApplication.Documents.Add();

                        try
                        {
                            wordApplication.WindowState = WdWindowState.wdWindowStateMinimize;
                            foreach (Word.Document item in wordApplication.Documents)
                            {
                                item.Windows[1].WindowState = WdWindowState.wdWindowStateMinimize;
                            }
                        }
                        catch (Exception e)
                        {
                            _log.Trace($"Could not minimize: {e}");
                        }

                        // insert some text
                        System.Collections.Generic.List <string> list = RandomText.GetDictionary.GetDictionaryList();
                        RandomText rt = new RandomText(list.ToArray());
                        rt.AddContentParagraphs(1, 1, 1, 10, 50);
                        wordApplication.Selection.TypeText(rt.Content);

                        var writeSleep = ProcessManager.Jitter(100);
                        Thread.Sleep(writeSleep);

                        wordApplication.Selection.HomeKey(WdUnits.wdLine, WdMovementType.wdExtend);
                        wordApplication.Selection.Font.Color = WdColor.wdColorSeaGreen;
                        wordApplication.Selection.Font.Bold  = 1;
                        wordApplication.Selection.Font.Size  = 18;

                        string rand = RandomFilename.Generate();

                        string dir = timelineEvent.CommandArgs[0].ToString();
                        if (dir.Contains("%"))
                        {
                            dir = Environment.ExpandEnvironmentVariables(dir);
                        }

                        if (Directory.Exists(dir))
                        {
                            Directory.CreateDirectory(dir);
                        }

                        string path = $"{dir}\\{rand}.docx";

                        //if directory does not exist, create!
                        _log.Trace($"Checking directory at {path}");
                        DirectoryInfo f = new FileInfo(path).Directory;
                        if (f == null)
                        {
                            _log.Trace($"Directory does not exist, creating directory at {f.FullName}");
                            Directory.CreateDirectory(f.FullName);
                        }

                        try
                        {
                            if (File.Exists(path))
                            {
                                File.Delete(path);
                            }
                        }
                        catch (Exception e)
                        {
                            _log.Debug(e);
                        }

                        newDocument.SaveAs(path);
                        Report(handler.HandlerType.ToString(), timelineEvent.Command, timelineEvent.CommandArgs[0].ToString());

                        FileListing.Add(path);

                        if (timelineEvent.DelayAfter > 0)
                        {
                            //sleep and leave the app open
                            _log.Trace($"Sleep after for {timelineEvent.DelayAfter}");
                            Thread.Sleep(timelineEvent.DelayAfter - writeSleep);
                        }

                        wordApplication.Quit();
                        wordApplication.Dispose();

                        if (wordApplication != null)
                        {
                            try
                            {
                                Marshal.ReleaseComObject(wordApplication);
                            }
                            catch
                            {
                            }

                            try
                            {
                                Marshal.FinalReleaseComObject(wordApplication);
                            }
                            catch
                            {
                            }
                        }

                        wordApplication = null;
                        GC.Collect();
                    }
                    catch (Exception e)
                    {
                        _log.Debug(e);
                    }
                    finally
                    {
                        Thread.Sleep(5000);
                    }
                }
            }
            catch (Exception e)
            {
                _log.Error(e);
            }
            finally
            {
                KillApp();
                FileListing.FlushList();
            }
        }
Example #4
0
        private void ExecuteEvents(Timeline timeline, TimelineHandler handler)
        {
            try
            {
                foreach (TimelineEvent timelineEvent in handler.TimeLineEvents)
                {
                    try
                    {
                        _log.Trace($"PowerPoint event - {timelineEvent}");
                        WorkingHours.Is(handler);

                        if (timelineEvent.DelayBefore > 0)
                        {
                            Thread.Sleep(timelineEvent.DelayBefore);
                        }

                        if (timeline != null)
                        {
                            var pids = ProcessManager.GetPids(ProcessManager.ProcessNames.PowerPoint).ToList();
                            if (pids.Count > timeline.TimeLineHandlers.Count(o => o.HandlerType == HandlerType.PowerPoint))
                            {
                                return;
                            }
                        }

                        PowerPoint.Application powerApplication = new PowerPoint.Application
                        {
                            DisplayAlerts = PpAlertLevel.ppAlertsNone,
                            Visible       = MsoTriState.msoTrue
                        };

                        try
                        {
                            powerApplication.WindowState = PpWindowState.ppWindowMinimized;
                        }
                        catch (Exception e)
                        {
                            _log.Trace($"Could not minimize: {e}");
                        }

                        // add a new presentation with one new slide
                        PowerPoint.Presentation presentation = powerApplication.Presentations.Add(MsoTriState.msoTrue);
                        presentation.Slides.Add(1, PpSlideLayout.ppLayoutClipArtAndVerticalText);

                        var writeSleep = ProcessManager.Jitter(100);
                        Thread.Sleep(writeSleep);

                        // save the document
                        string rand = RandomFilename.Generate();

                        string dir = timelineEvent.CommandArgs[0].ToString();
                        if (dir.Contains("%"))
                        {
                            dir = Environment.ExpandEnvironmentVariables(dir);
                        }

                        if (Directory.Exists(dir))
                        {
                            Directory.CreateDirectory(dir);
                        }

                        string path = $"{dir}\\{rand}.pptx";

                        //if directory does not exist, create!
                        _log.Trace($"Checking directory at {path}");
                        DirectoryInfo f = new FileInfo(path).Directory;
                        if (f == null)
                        {
                            _log.Trace($"Directory does not exist, creating directory at {f.FullName}");
                            Directory.CreateDirectory(f.FullName);
                        }

                        try
                        {
                            if (File.Exists(path))
                            {
                                File.Delete(path);
                            }
                        }
                        catch (Exception e)
                        {
                            _log.Debug(e);
                        }

                        presentation.SaveAs(path);
                        FileListing.Add(path);
                        Report(handler.HandlerType.ToString(), timelineEvent.Command,
                               timelineEvent.CommandArgs[0].ToString());

                        if (timelineEvent.DelayAfter > 0)
                        {
                            //sleep and leave the app open
                            _log.Trace($"Sleep after for {timelineEvent.DelayAfter}");
                            Thread.Sleep(timelineEvent.DelayAfter - writeSleep);
                        }

                        // close power point and dispose reference
                        powerApplication.Quit();
                        powerApplication.Dispose();

                        try
                        {
                            if (powerApplication != null)
                            {
                                System.Runtime.InteropServices.Marshal.ReleaseComObject(powerApplication);
                            }
                        }
                        catch
                        {
                        }

                        powerApplication = null;
                        presentation     = null;
                        GC.Collect();
                    }
                    catch (Exception e)
                    {
                        _log.Debug(e);
                    }
                    finally
                    {
                        Thread.Sleep(5000);
                    }
                }
            }
            catch (Exception e)
            {
                _log.Debug(e);
            }
            finally
            {
                KillApp();
                FileListing.FlushList();
            }
        }