private static void SaveInSP(WordSearchObject searchValues)
        {
            try
            {
                var crm = WordConnectToCrm.ConnectToMSCRM();
                if (crm == null)
                {
                    return;
                }

                var entity = WordHandlerCRM.FindRecordByNumber(searchValues, crm);
                if (entity == null)
                {
                    if (searchValues.language == 1044)
                    {
                        MessageBox.Show(Resources.ResourceWordNb.recordnotfound + searchValues.searchnumber + ".",
                                        Resources.ResourceWordNb.docsp, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        MessageBox.Show(Resources.ResourceWordEn.recordnotfound + searchValues.searchnumber + ".",
                                        Resources.ResourceWordEn.docsp, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }

                Save(entity, searchValues, crm);
            }

            catch (Exception ex)
            {
                if (searchValues.language == 1044)
                {
                    MessageBox.Show(Resources.ResourceWordNb.cannotsavetosp, Resources.ResourceWordNb.docsp, MessageBoxButtons.OK,
                                    MessageBoxIcon.Warning);
                }
                else
                {
                    MessageBox.Show(Resources.ResourceWordEn.cannotsavetosp, Resources.ResourceWordEn.docsp, MessageBoxButtons.OK,
                                    MessageBoxIcon.Warning);
                }
            }
        }
        private void buttonSign_Click(object sender, RibbonControlEventArgs e)
        {
            try
            {
                var crm = WordConnectToCrm.ConnectToMSCRM();
                if (crm == null)
                {
                    return;
                }

                WordSearchObject searchValues = new WordSearchObject();
                WordCRMConfig    crmconfig    = WordHandlerCRM.GetCRMConfig(crm);
                searchValues.language = WordHandlerCRM.RetrieveUserUiLanguageCode(crm, crmconfig.Worduser.Id);

                var accountfield  = (!string.IsNullOrWhiteSpace(crmconfig.AccountNrField)) ? crmconfig.AccountNrField.Split(',') : new[] { "accountnumber" };
                var orderfield    = (!string.IsNullOrWhiteSpace(crmconfig.OrderNrField)) ? crmconfig.OrderNrField.Split(',') : new[] { "ordernumber" };
                var quotefield    = (!string.IsNullOrWhiteSpace(crmconfig.QuoteNrField)) ? crmconfig.QuoteNrField.Split(',') : new[] { "quotenumber" };
                var incidentfield = (!string.IsNullOrWhiteSpace(crmconfig.IncidentNrField)) ? crmconfig.IncidentNrField.Split(',') : new[] { "ticketnumber" };
                var contractfield = (!string.IsNullOrWhiteSpace(crmconfig.ContractNrField)) ? crmconfig.ContractNrField.Split(',') : new[] { "contractnumber" };
                var emailfield    = (!string.IsNullOrWhiteSpace(crmconfig.EmailField)) ? crmconfig.EmailField.Split(',') : new[] { "recievermailaddress" };

                var account      = accountfield;  // new[] { "accountnumber", "kundenr" };
                var order        = orderfield;    //new[] { "ordernumber", "ordrenummer"};
                var quote        = quotefield;    //new[] { "quotenumber"};
                var incident     = incidentfield; //new[] { "ticketnumber"};
                var contract     = contractfield; //new[] { "contractnumber"};
                var recievermail = emailfield;    //new[] { "recievermailaddress", "e-post" };


                Document document = Globals.ThisAddIn.Application.ActiveDocument;
                //HeaderFooter objHeader = document.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary];
                Object start = document.Content.Start;
                Object end   = document.Content.End;
                //document.Range(ref start, ref end).Select();

                var selectedText = Globals.ThisAddIn.Application.ActiveDocument.Range(start, end).Text;

                //var headerText = objHeader.Range.Text;
                //headerText = headerText.Replace("\r\a", "").Replace("\r", "").Replace("\a", "");
                //var headerTextSplit = headerText.Split(' ');

                selectedText = selectedText.Replace("\r\a", "").Replace("\r", "").Replace("\a", "");
                var    docText = selectedText.Split(' ');
                string searchnumber, searchentity, searchnumberfield, senderfrom, recieverto;
                searchnumber = searchentity = searchnumberfield = senderfrom = recieverto = "";

                for (int i = 0; i < docText.Length; i++)
                {
                    //if (docText[i].ToLower().Contains(recievermail))
                    if (recievermail.Any(docText[i].Contains))
                    {
                        recieverto = docText[i + 1];
                    }
                }

                for (int i = 0; i < docText.Length; i++)
                {
                    //if (docText[i].ToLower().Contains(account))
                    if (account.Any(docText[i].Contains))
                    {
                        searchnumber      = docText[i + 1];
                        searchentity      = "account";
                        searchnumberfield = "accountnumber";
                        break;
                    }
                    //if (docText[i].ToLower().Contains(order))
                    if (order.Any(docText[i].Contains))
                    {
                        searchnumber      = docText[i + 1];
                        searchentity      = "salesorder";
                        searchnumberfield = "ordernumber";
                        break;
                    }
                    //if (docText[i].ToLower().Contains(quote))
                    if (quote.Any(docText[i].Contains))
                    {
                        searchnumber      = docText[i + 1];
                        searchentity      = "quote";
                        searchnumberfield = "quotenumber";
                        break;
                    }
                    //if (docText[i].ToLower().Contains(incident))
                    if (incident.Any(docText[i].Contains))
                    {
                        searchnumber      = docText[i + 1];
                        searchentity      = "incident";
                        searchnumberfield = "ticketnumber";
                        break;
                    }
                    //if (docText[i].ToLower().Contains(contract))
                    if (contract.Any(docText[i].Contains))
                    {
                        searchnumber      = docText[i + 1];
                        searchentity      = "contract";
                        searchnumberfield = "contractnumber";
                        break;
                    }
                }

                for (int i = 0; i < docText.Length; i++)
                {
                    if (docText[i].ToLower().Contains("saveat"))
                    {
                        searchentity = docText[i + 1];
                        searchnumber = docText[i + 2];
                        if (searchentity == "account")
                        {
                            searchnumberfield = searchentity + "accountnumber";
                        }
                        if (searchentity == "salesorder")
                        {
                            searchnumberfield = "ordernumber";
                        }
                        if (searchentity == "quote")
                        {
                            searchnumberfield = "quotenumber";
                        }
                        if (searchentity == "incident")
                        {
                            searchnumberfield = "ticketnumber";
                        }
                        if (searchentity == "contract")
                        {
                            searchnumberfield = "contractnumber";
                        }
                        break;
                    }
                }

                searchValues.searchentity      = searchentity;
                searchValues.searchnumber      = searchnumber;
                searchValues.searchnumberfield = searchnumberfield;
                searchValues.recievermail      = recieverto;

                if (string.IsNullOrWhiteSpace(searchentity) || string.IsNullOrWhiteSpace(searchnumber) || string.IsNullOrWhiteSpace(searchnumberfield))
                {
                    if (searchValues.language == 1044)
                    {
                        MessageBox.Show(Resources.ResourceWordNb.wordtemplaterror, Resources.ResourceWordNb.documentsinging, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        MessageBox.Show(Resources.ResourceWordEn.wordtemplaterror, Resources.ResourceWordEn.documentsinging, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    return;
                }

                WordHandlerSigning.SendDocumentForSigning(searchValues, crmconfig, crm);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Signicat Sign", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        internal static async Task SendDocumentForSigning(WordSearchObject searchValues, WordCRMConfig crmconfig, IOrganizationService crm)
        {
            Document document = Globals.ThisAddIn.Application.ActiveDocument;

            try
            {
                var fullname = document.Name.Split('.');
                var pdfName  = fullname[0] + ".pdf";
                var fullPath = document.Path + "\\" + pdfName;

                if (File.Exists(fullPath))
                {
                    File.Delete(fullPath);
                }

                document.ExportAsFixedFormat(
                    Path.Combine(document.Path, pdfName),
                    WdExportFormat.wdExportFormatPDF,
                    OpenAfterExport: false);

                var orgName = ConfigurationManager.AppSettings["OrgName"];

                var signicatHandler = new WordHandlerSignicat();
                var responseMessage = await signicatHandler.SendRequest(searchValues, document, pdfName, orgName,
                                                                        crmconfig, crm);

                var stringResult = await responseMessage.Content.ReadAsStringAsync();

                var result = stringResult.Split('"');
                var sdsurl = result[1];

                var entity = WordHandlerCRM.FindRecordByNumber(searchValues, crm);
                if (entity == null)
                {
                    if (searchValues.language == 1044)
                    {
                        MessageBox.Show(Resources.ResourceWordNb.recordnotfound + searchValues.searchnumber + Resources.ResourceWordNb.unassociated, Resources.ResourceWordNb.documentsinging, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        MessageBox.Show(Resources.ResourceWordEn.recordnotfound + searchValues.searchnumber + Resources.ResourceWordEn.unassociated, Resources.ResourceWordEn.documentsinging, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }

                var documentsigningid = WordHandlerCRM.CreatDocumentSigningInCRM(sdsurl, fullPath, fullname[0], entity,
                                                                                 searchValues, crmconfig, crm);
                if (documentsigningid != Guid.Empty)
                {
                    WordHandlerCRM.SendEmail(sdsurl, fullname[0], entity, documentsigningid, searchValues, crmconfig, crm);
                    if (searchValues.language == 1044)
                    {
                        MessageBox.Show(Resources.ResourceWordNb.documentsent, Resources.ResourceWordNb.documentsinging, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show(Resources.ResourceWordEn.documentsent, Resources.ResourceWordEn.documentsinging, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }

                File.Delete(fullPath);
            }
            catch (Exception ex)
            {
                if (searchValues.language == 1044)
                {
                    MessageBox.Show(Resources.ResourceWordNb.cannotcreateinsignicat + ex.Message);
                }
                else
                {
                    MessageBox.Show(Resources.ResourceWordEn.cannotcreateinsignicat + ex.Message);
                }
            }
        }