Exemple #1
0
        static void Main(string[] args)
        {
            Application app = new Application();
            LogOnStatus status = app.LogOn("TRAINING", "Demidov", "123456");
            Console.WriteLine(status);
            if (status != LogOnStatus.LoggedOn)
            {
                throw new UnauthorizedAccessException();
            }

            Record rec = new Record(app);
            Guid recId;
            Guid.TryParse("46e678e9-24af-4364-8926-a54600dcf188", out recId);
            rec.Load(recId);
            string s = rec.Files.LatestMaster.Path;
            Console.WriteLine(s);
            Console.ReadLine();
        }
        public void DoWork(object source, ElapsedEventArgs evt)
        {
            this._timer.Enabled = false;
            Console.WriteLine("It is {0} and all is well", DateTime.Now);

            foreach (string file in Directory.GetFiles(@"C:\Temp\", "*.*"))
            {
                if (ignoreList.Contains(file))
                {
                    continue;
                }

                if (file.EndsWith(".pdf"))
                {
                    FileInfo f = new FileInfo(file);
                    var email = f.Name.Replace(".pdf", "");
                    if (utils.IsValidEmail(email))
                    {
                        Console.WriteLine("One day we will email this...");
                    }
                }
                else if (file.EndsWith(".json"))
                {
                    try
                    {
                        Console.WriteLine("Processing File : " + file);

                        Document document;
                        var wordApp = new Application();
                        var sr = new StreamReader(file);
                        document = wordApp.Documents.Add(Environment.CurrentDirectory + @"\hallo2.dotx");

                        var letterInfo = JsonConvert.DeserializeObject<LetterInfo>(sr.ReadToEnd());
                        var mailMergeDoc = document.MailMerge;
                        wordApp.Visible = false;
                        foreach (MailMergeField f in mailMergeDoc.Fields)
                        {
                            Console.WriteLine("MERGEFIELD " + f.Code.Text + "");
                            foreach (PropertyInfo fld in letterInfo.GetType().GetProperties())
                            {
                                if (HasField(f, fld.Name))
                                {
                                    f.Select();
                                    wordApp.Selection.TypeText(fld.GetValue(letterInfo).ToString());
                                    break;
                                }
                            }
                        }
                        Console.WriteLine("Sleep Thread");
                        System.Threading.Thread.Sleep(8000);
                        document.SaveAs(ConfigurationManager.AppSettings["DumpingDirectory"] + letterInfo.EmailAddress + ".pdf", WdSaveFormat.wdFormatPDF);
                        Console.WriteLine("Document Saved");
                        sr.Close();
                        sr.Dispose();
                        Console.WriteLine("Sending Email - " + letterInfo.EmailAddress);
                        EmailHelper.Send(letterInfo.EmailAddress, letterInfo.RaceName + " - " + letterInfo.RaceDate, letterInfo.EmailAddress, ConfigurationManager.AppSettings["DumpingDirectory"] + letterInfo.EmailAddress + ".pdf");
                        File.Delete(file);
                        Console.WriteLine("File Deleted");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
                else
                {
                    ignoreList.Add(file);
                    Console.WriteLine("Skip " + file);
                    continue;
                }
                Console.WriteLine("_________________Searching for pending letters_______________________");
                LetterReminder.DoWork();
                Console.WriteLine("____________________________________________________________________");
                this._timer.Enabled = true;
            }
        }