Beispiel #1
0
        public override void RunCommand(object sender)
        {
            var engine    = (AutomationEngineInstance)sender;
            var vFilePath = v_FilePath.ConvertUserVariableToString(engine);

            if (v_CloseAllInstances == "Yes")
            {
                var processes = Process.GetProcessesByName("excel");
                foreach (var prc in processes)
                {
                    prc.Kill();
                }
            }

            var newExcelSession = new Application();

            if (v_Visible == "Yes")
            {
                newExcelSession.Visible = true;
            }
            else
            {
                newExcelSession.Visible = false;
            }

            newExcelSession.AddAppInstance(engine, v_InstanceName);

            if (v_NewOpenWorkbook == "New Workbook")
            {
                if (!string.IsNullOrEmpty(vFilePath))
                {
                    throw new InvalidDataException("File path should not be provided for a new Excel Workbook");
                }
                else
                {
                    newExcelSession.Workbooks.Add();
                }
            }
            else if (v_NewOpenWorkbook == "Open Workbook")
            {
                if (string.IsNullOrEmpty(vFilePath))
                {
                    throw new NullReferenceException("File path for Excel Workbook not provided");
                }
                else
                {
                    newExcelSession.Workbooks.Open(vFilePath);
                }
            }
        }