Beispiel #1
0
        private static bool GetOutlook()
        {
            try
            {
                // If GetObject throws an exception, then Outlook is
                // either not running or is not available.
                outlook = AutomationFactory.GetObject("Outlook.Application");
                return(true);
            }
            catch
            {
                try
                {
                    // Start Outlook and display the Inbox, but minimize
                    // it to avoid hiding the running application.

                    outlook = AutomationFactory.CreateObject("Outlook.Application");
                    outlook.Session.GetDefaultFolder(6).Display();
                    // 6 = Inbox
                    outlook.ActiveWindow.WindowState = 1;
                    // minimized
                    return(true);
                }
                catch
                {
                    // Outlook is unavailable.
                    return(false);
                }
            }
        }
        public override void Execute()
        {
            var cmdlet = (ConvertToUiaSearchCriteriaCommand)Cmdlet;

            var converter = AutomationFactory.GetObject <ElementToSearchCriteriaConverter>();

//            foreach (IUiElement element in cmdlet.InputObject) {
//                WriteObject(this, ConvertElementToSearchCriteria(element));
//            }
        }
Beispiel #3
0
        public static void CreateEmail(
            string toAddress, string subject, string body)
        {
            try
            {
                dynamic outlook = null;

                if (AutomationFactory.IsAvailable)
                {
                    try
                    {
                        //Get the reference to the open Outlook App
                        outlook =
                            AutomationFactory.GetObject("Outlook.Application");
                    }
                    catch (Exception ex)
                    {
                        //Outlook isn't open, therefore try and open it
                        outlook =
                            AutomationFactory.CreateObject("Outlook.Application");
                    }

                    if (outlook != null)
                    {
                        //Create the email
                        dynamic mail = outlook.CreateItem(olMailItem);
                        if (body.ToLower().Contains("<html>"))
                        {
                            mail.BodyFormat = olFormatHTML;
                            mail.HTMLBody   = body;
                        }
                        else
                        {
                            mail.BodyFormat = olFormatPlain;
                            mail.Body       = body;
                        }
                        mail.Recipients.Add(toAddress);
                        mail.Subject = subject;

                        mail.Save();
                        mail.Display();
                        //uncomment this code to send the email immediately
                        //mail.Send()
                    }
                }
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(
                          "Failed to create email.", ex);
            }
        }
Beispiel #4
0
 // Use this method to retrieve active Word session
 public bool GetWord()
 {
     try
     {
         // If GetObject throws an exception, then Word is
         // either not running or is not available.
         _word = AutomationFactory.GetObject("Word.Application");
         return(true);
     }
     catch
     {
         return(false);
     }
 }
        private bool InitializeOutlook()
        {
            string outlookName = "Outlook.Application";

            try {
                outlook = AutomationFactory.GetObject(outlookName);
                return(true);
            }
            catch (Exception) {
                try {
                    outlook = AutomationFactory.CreateObject(outlookName);
                    return(true);
                }
                catch (Exception) {
                    return(false);
                }
            }
        }
Beispiel #6
0
        public override void Execute()
        {
            var cmdlet =
                (GetUiaWindowCommand)Cmdlet;

            try {
                // 20140312
                // List<IUiElement> returnedWindows = new List<IUiElement>();
                var returnedWindows = new List <IUiElement>();

                try {
                    if (null == cmdlet.ProcessName &&
                        (null == cmdlet.Name && null == cmdlet.AutomationId && null == cmdlet.Class) &&
                        null == cmdlet.ProcessId &&
                        null == cmdlet.InputObject)
                    {
                        cmdlet.WriteVerbose(
                            cmdlet,
                            Resource.GetWindowCommand_Verbose_no_processName__name__processid_or_process_was_supplied);

                        cmdlet.WriteError(
                            cmdlet,
                            Resource.GetWindowCommand_Error_wrong_input,
                            "NoParametersInGetWindow",
                            ErrorCategory.InvalidArgument,
                            true);
                    } // describe
                }
                catch (Exception eCheckParameters) {
                    cmdlet.WriteVerbose(cmdlet, eCheckParameters.Message);

                    cmdlet.WriteError(
                        cmdlet,
                        // "Unknown error in '" + cmdlet.CmdletName(cmdlet) + "' ProcessRecord",
                        string.Format(Resource.GetWindowCommand_Error_Unknown_error, cmdlet.CmdletName(cmdlet)),
                        "UnknownInGetWindow",
                        ErrorCategory.InvalidResult,
                        true);
                } // describe

                try {
                    var windowSearcher = AutomationFactory.GetSearcherImpl <WindowSearcher>();

                    //                var windowSearcherData =
                    //                    new WindowSearcherData {
                    //                    Win32 = cmdlet.Win32,
                    //                    InputObject = cmdlet.InputObject,
                    //                    ProcessNames = cmdlet.ProcessName,
                    //                    ProcessIds = cmdlet.ProcessId,
                    //                    Name = cmdlet.Name,
                    //                    AutomationId = cmdlet.AutomationId,
                    //                    Class = cmdlet.Class,
                    //                    WithControl = cmdlet.WithControl,
                    //                    TestMode = cmdlet.TestMode,
                    //                    SearchCriteria = cmdlet.SearchCriteria,
                    //                    First = cmdlet.First,
                    //                    Recurse = cmdlet.Recurse,
                    //                    WaitNoWindow = cmdlet.WaitNoWindow
                    //                };

                    var windowSearcherData = AutomationFactory.GetObject <WindowSearcherData>();
                    windowSearcherData.Win32          = cmdlet.Win32;
                    windowSearcherData.InputObject    = cmdlet.InputObject;
                    windowSearcherData.ProcessNames   = cmdlet.ProcessName;
                    windowSearcherData.ProcessIds     = cmdlet.ProcessId;
                    windowSearcherData.Name           = cmdlet.Name;
                    windowSearcherData.AutomationId   = cmdlet.AutomationId;
                    windowSearcherData.Class          = cmdlet.Class;
                    windowSearcherData.WithControl    = cmdlet.WithControl;
                    windowSearcherData.TestMode       = cmdlet.TestMode;
                    windowSearcherData.SearchCriteria = cmdlet.SearchCriteria;
                    windowSearcherData.First          = cmdlet.First;
                    windowSearcherData.Recurse        = cmdlet.Recurse;
                    windowSearcherData.WaitNoWindow   = cmdlet.WaitNoWindow;

                    returnedWindows = windowSearcher.GetElements(
                        windowSearcherData,
                        cmdlet.Timeout);

                    windowSearcherData = null;
                }
                catch {}

                try {
                    if (null != returnedWindows && returnedWindows.Count > 0)
                    {
                        if (cmdlet.TestMode)
                        {
                            cmdlet.WriteObject(cmdlet, !cmdlet.WaitNoWindow);
                        }
                        else
                        {
                            cmdlet.WriteObject(cmdlet, returnedWindows);
                        }

                        // 20140121
                        returnedWindows.Clear();
                        returnedWindows = null;
                    }
                    else
                    {
                        if (cmdlet.TestMode)
                        {
                            cmdlet.WriteObject(cmdlet, cmdlet.WaitNoWindow);
                        }
                        else
                        {
                            string name     = string.Empty;
                            string procName = string.Empty;
                            string procId   = string.Empty;

                            try{
                                foreach (string n in cmdlet.Name)
                                {
                                    name += n; name += ",";
                                }
                                name = name.Substring(0, name.Length - 1);
                            }
                            catch {}

                            try{
                                foreach (string s in cmdlet.ProcessName)
                                {
                                    procName += s; procName += ",";
                                }
                                procName = procName.Substring(0, procName.Length - 1);
                            }
                            catch {}

                            try {
                                foreach (int i in cmdlet.ProcessId)
                                {
                                    procId += i.ToString();
                                    procId += ",";
                                }
                                procId = procId.Substring(0, procId.Length - 1);
                            }
                            catch {}

                            cmdlet.WriteError(
                                cmdlet,
                                string.Format(Resource.GetWindowCommand_Error_Timeout, cmdlet.Timeout, procName, procId, name, cmdlet.AutomationId, cmdlet.Class),
                                "FailedToGetWindow",
                                ErrorCategory.InvalidResult,
                                true);
                        }
                    }
                }
                catch (Exception eOuter) {
                    cmdlet.WriteVerbose(
                        cmdlet,
                        eOuter.Message);
                }
            }
            catch (Exception eTheOutest) {
                cmdlet.WriteVerbose(
                    cmdlet,
                    eTheOutest.Message);
            }
        }
        /// <summary>
        /// Generates a new document based on the document name and associated properties.
        /// Uses entitysets for the documents/tasks/reviews required.
        /// TO DO:
        /// 1) Check whether files exist in the Vault and prompt user to check out/update as necessary.
        /// 2)
        /// </summary>
        /// <param name="DocumentName">The name of the document we are creating.</param>
        /// <param name="LSProp">The Properties we wish to publish</param>
        /// <param name="LSDocs">The list of documents/tasks/reviews we need to publish</param>
        /// <param name="IsProjectDoc">Defines whether the document is using project data or independant</param>
        /// <param name="ListByProjectOnly">Defines whether the document (Timesheet mainly) is using project data.</param>
        public void GenerateFromAddNew(string DocumentName, dynamic LSProp, dynamic LSDocs, Boolean IsProjectDoc = true, Boolean ListByProjectOnly = false)
        {
            List <ColumnMapping> mapContent = new List <ColumnMapping>();
            List <OfficeIntegration.ColumnMapping> mappings = new List <ColumnMapping>();
            string  wordDoc  = "";
            dynamic doc      = "";
            string  filename = "";

            if (IsProjectDoc) // project-specific!
            {
                mapContent.Add(new ColumnMapping("Customer", "Customer"));
                mapContent.Add(new ColumnMapping("ProjectTitle", "Project"));
                mapContent.Add(new ColumnMapping("MatrixSalesOrderNo", "MatrixSalesOrderNo"));
                mapContent.Add(new ColumnMapping("MatrixProjectCode", "MatrixProjectCode"));
                mapContent.Add(new ColumnMapping("CustomerProjectCode", "CustomerProjectCode"));
                mapContent.Add(new ColumnMapping("CustomerOrderNo", "CustomerOrderNo"));
                mapContent.Add(new ColumnMapping("UserName", "UserName"));
                mapContent.Add(new ColumnMapping("UserPosition", "UserPosition"));
            }
            else //non project-specific!
            {
                mapContent.Add(new ColumnMapping("Customer", "Customer"));
                mapContent.Add(new ColumnMapping("MatrixSalesOrderNo", "MatrixSalesOrderNo"));
                mapContent.Add(new ColumnMapping("UserName", "UserName"));
                mapContent.Add(new ColumnMapping("UserPosition", "UserPosition"));
            }
            dynamic word = null;

            #region "Document-specific"

            switch (DocumentName)
            {
            case "CofC":
                #region "CofC --WORKING! 2013-10-01 AF"
                LightSwitchApplication.CofC cofC = LSProp;
                //template name
                wordDoc = matrixDocs + "CofC QMF 10 Issue 06.dotx";
                //template-specifics
                mapContent.Add(new ColumnMapping("DeliveryNoteNo", "DeliveryNoteNo"));
                mapContent.Add(new ColumnMapping("CertificateNo", "CertificateNo"));
                mapContent.Add(new ColumnMapping("CofCDate", "CofCDate"));
                doc = OfficeIntegration.Word.GenerateDocument(wordDoc, cofC, mapContent);
                //template datatable-specific columns
                mappings.Add(new OfficeIntegration.ColumnMapping("", "Quantity"));
                mappings.Add(new OfficeIntegration.ColumnMapping("", "DrawingNumber"));
                mappings.Add(new OfficeIntegration.ColumnMapping("", "DrawingRevision"));
                mappings.Add(new OfficeIntegration.ColumnMapping("", "DrawingTitle"));
                word = OfficeIntegration.Word.GetWord();
                Word.GetDocument(word, wordDoc);
                //export our list of documents:
                Microsoft.LightSwitch.Framework.EntitySet <CofCDoc> docs = LSDocs;
                var cofcresults = from a in docs
                                  where a.CofC.CertificateNo == cofC.CertificateNo
                                  select a;
                Word.Export(doc, "DataTable", 2, false, cofcresults, mappings);

                filename = @"\\MATRIXSERVER\Data\01 - CAD SUPPORT FILES\CofCs\" + cofC.CertificateNo + ".docx";
                break;

                #endregion
            case "DT":
                #region "Transmittal --WORKING! 2013-12-03 AF"
                LightSwitchApplication.Transmittal trans = LSProp;     //this transimittal!
                //template name
                wordDoc = matrixDocs + "DT QMF39 Issue 02.dotx";
                //template-specifics
                mapContent.Add(new ColumnMapping("TransId", "TransmittalNumber"));
                mapContent.Add(new ColumnMapping("Month", "Month"));
                mapContent.Add(new ColumnMapping("UserName", "UserName"));
                mapContent.Add(new ColumnMapping("Recipient", "Recipient"));
                mapContent.Add(new ColumnMapping("QueryDate", "QueryDate"));
                doc = OfficeIntegration.Word.GenerateDocument(wordDoc, trans, mapContent);
                //template datatable-specific columns
                mappings.Add(new OfficeIntegration.ColumnMapping("", "DocNo"));
                mappings.Add(new OfficeIntegration.ColumnMapping("", "DocRevision"));
                mappings.Add(new OfficeIntegration.ColumnMapping("", "SheetSize"));
                mappings.Add(new OfficeIntegration.ColumnMapping("", "MPENumber"));
                mappings.Add(new OfficeIntegration.ColumnMapping("", "DocTitle"));
                mappings.Add(new OfficeIntegration.ColumnMapping("", "Copies"));
                mappings.Add(new OfficeIntegration.ColumnMapping("", "TransmittalDocReasonsForIssue"));
                word = OfficeIntegration.Word.GetWord();
                Word.GetDocument(word, wordDoc);
                Microsoft.LightSwitch.Framework.EntitySet <TransmittalDoc> transDocs = LSDocs;    //all the documents!
                var transresults = from TransmittalDoc a in transDocs
                                   where a.Transmittal.TransmittalNumber == trans.TransmittalNumber
                                   select a;
                Word.Export(doc, "DataTable", 2, false, transresults, mappings);

                filename = @"\\MATRIXSERVER\Data\01 - CAD SUPPORT FILES\Transmittals\" + trans.TransmittalNumber + ".docx";
                break;

                #endregion
            case "DDA":
                #region "Drawing and Document Approval Form --WORKING! 2013-10-02 AF"
                LightSwitchApplication.DandDA dandDA = LSProp;     //this DandDA!
                //template name
                wordDoc = matrixDocs + "DDA QMF47 ISsue 02.dotx";
                //template-specifics
                mapContent.Add(new ColumnMapping("DDAId", "DDANumber"));
                mapContent.Add(new ColumnMapping("DDADate", "DDADate"));
                doc = OfficeIntegration.Word.GenerateDocument(wordDoc, dandDA, mapContent);
                //template datatable-specific columns
                mappings.Add(new OfficeIntegration.ColumnMapping("", "DrawingNo"));
                mappings.Add(new OfficeIntegration.ColumnMapping("", "Title"));
                mappings.Add(new OfficeIntegration.ColumnMapping("", "Revision"));
                word = OfficeIntegration.Word.GetWord();
                Word.GetDocument(word, wordDoc);
                Microsoft.LightSwitch.Framework.EntitySet <DandADoc> DandDADocs = LSDocs;    //all the documents!
                var dandDAResults = from DandADoc a in DandDADocs
                                    where a.DandDA.DDANumber == dandDA.DDANumber
                                    select a;
                Word.Export(doc, "DataTable", 2, false, dandDAResults, mappings);

                filename = @"\\MATRIXSERVER\Data\01 - CAD SUPPORT FILES\DDAs\" + dandDA.DDANumber + ".docx";
                break;

                #endregion
            case "DDC":
                #region "Drawing and Document Control Form --WORKING! 2013-10-11 AF"
                LightSwitchApplication.DandDC dandDC = LSProp; //this DandDC!
                filename = @"\\MATRIXSERVER\Data\01 - CAD SUPPORT FILES\DDCs\" + dandDC.DDCNumber + ".docx";
                if (!File.Exists(filename))                    //new file
                {
                    //template name
                    wordDoc = matrixDocs + "DDC QMF25 Issue 02.dotx";
                    mapContent.Add(new ColumnMapping("DesignId", "DDCId"));
                    mapContent.Add(new ColumnMapping("Month", "Month"));
                    mapContent.Add(new ColumnMapping("BasedOn", "BasedOn"));
                    mapContent.Add(new ColumnMapping("Essentials", "Essentials"));
                    mapContent.Add(new ColumnMapping("StatsRegs", "StatsRegs"));
                    mapContent.Add(new ColumnMapping("SpecReqs", "SpecReqs"));
                    mapContent.Add(new ColumnMapping("DandDCStartDate", "DandDCStartDate"));
                    doc = OfficeIntegration.Word.GenerateDocument(wordDoc, dandDC, mapContent);
                }
                else     //updating the existing file.
                {
                    wordDoc = filename;
                    mapContent.Add(new ColumnMapping("Verification", "OutputsVerification"));
                    mapContent.Add(new ColumnMapping("Validation", "OutputsValidation"));
                    mapContent.Add(new ColumnMapping("OutputsValiMDate", "OutputsValiMDate"));
                    mapContent.Add(new ColumnMapping("OutputsVeriMDate", "OutputsVeriMDate"));
                    mapContent.Add(new ColumnMapping("OutputsVeriMMatrixAttendees", "MatrixVerificationMeetingAttendees"));
                    mapContent.Add(new ColumnMapping("OutputsVeriMClientAttendees", "ClientVerificationMeetingAttendees"));
                    mapContent.Add(new ColumnMapping("OutputsValiMMatrixAttendees", "MatrixValidationMeetingAttendees"));
                    mapContent.Add(new ColumnMapping("OutputsValiMClientAttendees", "ClientValidationMeetingAttendees"));
                    doc = OfficeIntegration.Word.GenerateDocument(wordDoc, dandDC, mapContent);
                    mappings.Add(new OfficeIntegration.ColumnMapping("", "ReviewDateStr"));
                    mappings.Add(new OfficeIntegration.ColumnMapping("", "Reviewer"));
                    mappings.Add(new OfficeIntegration.ColumnMapping("", "ReviewLocation"));
                    mappings.Add(new OfficeIntegration.ColumnMapping("", "ReviewComment"));

                    word = OfficeIntegration.Word.GetWord();
                    Word.GetDocument(word, wordDoc);
                    Microsoft.LightSwitch.Framework.EntitySet <Review> reviews = LSDocs;    //all the documents!
                    var dandDCResults = from Review a in reviews
                                        where a.Project.ProjectTitle == dandDC.Project.ProjectTitle
                                        select a;
                    Word.Export(doc, "Reviews", 3, false, dandDCResults, mappings);

                    filename = @"\\MATRIXSERVER\Data\01 - CAD SUPPORT FILES\DDCs\" + dandDC.DDCNumber + ".docx";
                }
                #endregion
                break;

            case "COPT":
                #region "CAD Office Project Report --WORKING! 2013-12-03 AF"
                LightSwitchApplication.CADOfficeProjT copt = LSProp;
                //var myDocs = Environment.GetFolderPath(Environment.SpecialFolder.Templates);
                wordDoc = matrixDocs + "COPT QMF41 Issue 02.dotx";
                //template-specifics
                mapContent.Add(new ColumnMapping("COPTId", "COPTNumber"));
                mapContent.Add(new ColumnMapping("Month", "Month"));
                mapContent.Add(new ColumnMapping("Year", "Year"));
                mapContent.Add(new ColumnMapping("UserName", "UserName"));
                doc = OfficeIntegration.Word.GenerateDocument(wordDoc, copt, mapContent);
                //template datatable-specific columns
                mappings.Add(new OfficeIntegration.ColumnMapping("", "TaskDate"));
                mappings.Add(new OfficeIntegration.ColumnMapping("", "TaskName"));
                mappings.Add(new OfficeIntegration.ColumnMapping("", "Hours"));
                word = OfficeIntegration.Word.GetWord();
                Word.GetDocument(word, wordDoc);
                //Project or month-specifics:
                Microsoft.LightSwitch.Framework.EntitySet <Task> tasks = LSDocs;
                //dynamic sortedGroups = getGroupedTasks(tasks);
                if (IsProjectDoc)     //project tasks
                {
                    var filteredTasks = from Task t in tasks
                                        where t.Project != null
                                        select t;
                    if (copt.EndDate == null)
                    {
                        if (!ListByProjectOnly)
                        {
                            coptresults = from Task b in filteredTasks
                                          where b.Project.ProjectTitle == copt.Project.ProjectTitle &&
                                          b.Day >= copt.StartDate &&
                                          b.User == copt.UserName
                                          select b;
                        }
                        else
                        {
                            coptresults = from Task b in filteredTasks
                                          where b.Project.ProjectTitle == copt.Project.ProjectTitle &&
                                          b.Day >= copt.StartDate
                                          select b;
                        }
                    }
                    else
                    {
                        if (!ListByProjectOnly)
                        {
                            coptresults = from Task b in filteredTasks
                                          where b.Project.ProjectTitle == copt.Project.ProjectTitle &&
                                          b.Day >= copt.StartDate &&
                                          b.Day <= copt.EndDate &&
                                          b.User == copt.UserName
                                          select b;
                        }
                        else
                        {
                            coptresults = from Task b in filteredTasks
                                          where b.Project.ProjectTitle == copt.Project.ProjectTitle &&
                                          b.Day >= copt.StartDate &&
                                          b.Day <= copt.EndDate
                                          select b;
                        }
                    }
                    filename = @"\\MATRIXSERVER\Data\01 - CAD SUPPORT FILES\Project Timesheets\"
                               + copt.COPTNumber
                               + ".docx";
                }
                else     //monthly tasks
                {
                    if (copt.EndDate == null)
                    {
                        coptresults = from Task b in tasks
                                      where b.Day >= copt.StartDate &&
                                      b.User == copt.UserName
                                      orderby b.Day
                                      select b;
                    }
                    else
                    {
                        coptresults = from Task b in tasks
                                      where b.Day >= copt.StartDate &&
                                      b.Day <= copt.EndDate &&
                                      b.User == copt.UserName
                                      orderby b.Day
                                      select b;
                    }
                    filename = @"\\MATRIXSERVER\Data\01 - CAD SUPPORT FILES\Project Timesheets\"
                               + copt.COPTNumber
                               + ".docx";
                }

                //var coptresults = from Task t in tasks
                //                  select t;

                Word.Export(doc, "DataTable", 2, false, coptresults, mappings);


                break;

                #endregion
            case "":
                break;
            }
            #endregion

            dynamic _word = AutomationFactory.GetObject("Word.Application");
            var     _doc  = _word.ActiveDocument;
            if (DocumentName == "DDC")
            {
                #region "checkboxes"
                if (LSProp.ProtoTypeProduced == true)
                {
                    dynamic ccs = _doc.SelectContentControlsByTitle("PrototypeProducedYes");
                    if (ccs != null)
                    {
                        dynamic cc = ccs[1];
                        cc.Checked = true;
                    }
                    ccs = _doc.SelectContentControlsByTitle("PrototypeProducedNo");
                    if (ccs != null)
                    {
                        dynamic cc = ccs[1];
                        cc.Checked = false;
                    }
                }
                else
                {
                    dynamic ccs = _doc.SelectContentControlsByTitle("PrototypeProducedNo");
                    if (ccs != null)
                    {
                        dynamic cc = ccs[1];
                        cc.Checked = true;
                    }
                }
                if (LSProp.MaterialsCosted == true)
                {
                    dynamic ccs = _doc.SelectContentControlsByTitle("MaterialsYes");
                    if (ccs != null)
                    {
                        dynamic cc = ccs[1];
                        cc.Checked = true;
                    }
                    ccs = _doc.SelectContentControlsByTitle("MaterialsNo");
                    if (ccs != null)
                    {
                        dynamic cc = ccs[1];
                        cc.Checked = false;
                    }
                }
                else
                {
                    dynamic ccs = _doc.SelectContentControlsByTitle("MaterialsNo");
                    if (ccs != null)
                    {
                        dynamic cc = ccs[1];
                        cc.Checked = true;
                    }
                }
                if (LSProp.LabourCosted == true)
                {
                    dynamic ccs = _doc.SelectContentControlsByTitle("LabourCostedYes");
                    if (ccs != null)
                    {
                        dynamic cc = ccs[1];
                        cc.Checked = true;
                    }
                    ccs = _doc.SelectContentControlsByTitle("LabourCostedNo");
                    if (ccs != null)
                    {
                        dynamic cc = ccs[1];
                        cc.Checked = false;
                    }
                }
                else
                {
                    dynamic ccs = _doc.SelectContentControlsByTitle("LabourCostedNo");
                    if (ccs != null)
                    {
                        dynamic cc = ccs[1];
                        cc.Checked = true;
                    }
                }
                if (LSProp.ClientApproved == true)
                {
                    dynamic ccs = _doc.SelectContentControlsByTitle("ClientApprovedYes");
                    if (ccs != null)
                    {
                        dynamic cc = ccs[1];
                        cc.Checked = true;
                    }
                    ccs = _doc.SelectContentControlsByTitle("ClientApprovedNo");
                    if (ccs != null)
                    {
                        dynamic cc = ccs[1];
                        cc.Checked = false;
                    }
                }
                else
                {
                    dynamic ccs = _doc.SelectContentControlsByTitle("ClientApprovedNo");
                    if (ccs != null)
                    {
                        dynamic cc = ccs[1];
                        cc.Checked = true;
                    }
                }
                #endregion
            }
            _doc.SaveAs(filename, 12);
            if (!File.Exists(filename))
            {
                MessageBox.Show("The created Word File didn't save for some reason, please do a manual saveas to preserve our template!");
            }
            mapContent = null;
            mappings   = null;
        }
        /// <summary>
        /// Generates a new document based on the document name and associated properties.
        /// </summary>
        /// <param name="DocumentName">The name of the document we are creating.</param>
        /// <param name="LSProp">The Properties we wish to publish.</param>
        /// <param name="IsProjectDoc">Boolean controlling whether the document is project-specific.</param>
        public void GenerateFromAddNew(string DocumentName, dynamic LSProp, Boolean IsProjectDoc = true)
        {
            List <ColumnMapping> mapContent = new List <ColumnMapping>();
            //List<OfficeIntegration.ColumnMapping> mappings = new List<ColumnMapping>();
            string  wordDoc  = "";
            dynamic doc      = "";
            string  filename = "";

            //object doc = null;
            if (IsProjectDoc) // project-specific!
            {
                mapContent.Add(new ColumnMapping("Customer", "Customer"));
                mapContent.Add(new ColumnMapping("ProjectTitle", "Project"));
                mapContent.Add(new ColumnMapping("MatrixSalesOrderNo", "MatrixSalesOrderNo"));
                mapContent.Add(new ColumnMapping("MatrixProjectCode", "MatrixProjectCode"));
                mapContent.Add(new ColumnMapping("CustomerProjectCode", "CustomerProjectCode"));
                mapContent.Add(new ColumnMapping("CustomerOrderNo", "CustomerOrderNo"));
                mapContent.Add(new ColumnMapping("UserName", "UserName"));
                mapContent.Add(new ColumnMapping("UserPosition", "UserPosition"));
            }
            else //non project-specific!
            {
                mapContent.Add(new ColumnMapping("Customer", "Customer"));
                mapContent.Add(new ColumnMapping("MatrixSalesOrderNo", "MatrixSalesOrderNo"));
                mapContent.Add(new ColumnMapping("UserName", "UserName"));
                mapContent.Add(new ColumnMapping("UserPosition", "UserPosition"));
                mapContent.Add(new ColumnMapping("CustomerOrderNo", "CustomerOrderNo"));
            }

            #region "Document-specific"
            switch (DocumentName)
            {
            case "TQ":
                #region "TQ --WORKING! 2013-10-04 AF"
                try
                {
                    LightSwitchApplication.TQDetail tq = LSProp;
                    //template name
                    String tmpcustomerName = tq.Customer;
                    String customerName    = tmpcustomerName.ToUpper();
                    if (customerName.Contains("CAVENDISH"))
                    {
                        wordDoc = matrixDocs + @"\Cavendish Nuclear\Supplier TQ.docx";
                        mapContent.Add(new ColumnMapping("QueryReturnDate", "TQReturnDate"));
                    }
                    else
                    {
                        wordDoc = matrixDocs + "TQ QMF33 Issue 06.dotx";
                    }
                    //template-specifics
                    mapContent.Add(new ColumnMapping("TQId", "TQNumber"));
                    mapContent.Add(new ColumnMapping("DrawingNumber", "DrawingNumber"));
                    mapContent.Add(new ColumnMapping("DrawingRevision", "DrawingRevision"));
                    mapContent.Add(new ColumnMapping("QueryText", "QueryText"));
                    mapContent.Add(new ColumnMapping("QueryDate", "QueryDate"));
                    mapContent.Add(new ColumnMapping("ResponseSatisfactoryYesNo", "ResponseSatisfactory"));
                    doc      = OfficeIntegration.Word.GenerateDocument(wordDoc, tq, mapContent);
                    filename = @"\\MATRIXSERVER\Data\01 - CAD SUPPORT FILES\TQs\" + tq.TQNumber + ".docx";
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                break;

                #endregion
            case "RFI":
                #region "RFI --WORKING! 2013-10-07 AF-- changed to collect list of recipients-- see table code for more..."
                LightSwitchApplication.RFI rfi = LSProp;
                //this file may need to be updated, so we have to see if the file for this rfi already exists:
                filename = @"\\MATRIXSERVER\Data\01 - CAD SUPPORT FILES\RFIs\" + rfi.RFINumber + ".docx";
                if (!File.Exists(filename))     //new file.
                {
                    //template name
                    String tmpcustomerName = rfi.Customer;
                    String customerName    = tmpcustomerName.ToUpper();
                    if (customerName.Contains("CAVENDISH"))
                    {
                        wordDoc = matrixDocs + @"\Cavendish Nuclear\Supplier RFI.dotx";
                        mapContent.Add(new ColumnMapping("QueryReturnDate", "TQReturnDate"));
                    }
                    else
                    {
                        wordDoc = matrixDocs + "RFI QMF40 Issue 03.dotx";
                    }
                    //template-specifics
                    mapContent.Add(new ColumnMapping("RFIId", "RFINumber"));
                    mapContent.Add(new ColumnMapping("RFIRecipient", "RFIRecipient"));
                    mapContent.Add(new ColumnMapping("DrawingNumber", "DrawingNumber"));
                    mapContent.Add(new ColumnMapping("DrawingRevision", "DrawingRevision"));
                    mapContent.Add(new ColumnMapping("RequestText", "RequestText"));
                    mapContent.Add(new ColumnMapping("RFIDate", "RFIDate"));
                    doc = OfficeIntegration.Word.GenerateDocument(wordDoc, rfi, mapContent);
                }
                else     //updating the existing file.
                {
                    wordDoc = filename;
                    //template name
                    //wordDoc = matrixDocs + "RFI QMF40 Issue 03.dotx";
                    //template-specifics
                    mapContent.Add(new ColumnMapping("RFIId", "RFINumber"));
                    mapContent.Add(new ColumnMapping("RFIRecipient", "RFIRecipient"));
                    mapContent.Add(new ColumnMapping("DrawingNumber", "DrawingNumber"));
                    mapContent.Add(new ColumnMapping("DrawingRevision", "DrawingRevision"));
                    mapContent.Add(new ColumnMapping("RequestText", "RequestText"));
                    mapContent.Add(new ColumnMapping("RFIDate", "RFIDate"));
                    mapContent.Add(new ColumnMapping("ResponseToRFI", "ResponseToRFI"));
                    mapContent.Add(new ColumnMapping("RespondentPosition", "RespondentPosition"));
                    mapContent.Add(new ColumnMapping("ResponseDate", "ResponseDate"));
                    mapContent.Add(new ColumnMapping("ResponseImplications", "ResponseImplications"));
                    mapContent.Add(new ColumnMapping("Respondent", "Respondent"));
                    mapContent.Add(new ColumnMapping("MatrixRespondent", "MatrixRespondent"));
                    mapContent.Add(new ColumnMapping("MatrixRespondentPosition", "MatrixRespondentPosition"));
                    mapContent.Add(new ColumnMapping("MatrixRespondentDate", "MatrixRespondentDate"));
                    //word = OfficeIntegration.Word.GetWord();
                    //Word.GetDocument(word, wordDoc);
                    doc = OfficeIntegration.Word.GenerateDocument(wordDoc, rfi, mapContent);
                }
                break;

                #endregion
            case "":
                break;
            }
            #endregion

            dynamic _word = AutomationFactory.GetObject("Word.Application");
            var     _doc  = _word.ActiveDocument;

            if (File.Exists(filename))
            {
                _doc.SaveAs(filename, 12);
            }
            else
            {
                _doc.SaveAs(filename, 12);
            }
            if (!File.Exists(filename))
            {
                MessageBox.Show("The created Word File didn't save for some reason, please do a manual saveas to preserve our template!");
            }
            mapContent = null;
            //mappings = null;
        }