Esempio n. 1
0
 //ВСТАВЛЯЕМ ДОКУМЕНТ WORD ИЗ ФАЙЛА
 public void InsertFile(string pathToFile)
 {
     if (_currentRange == null)
     {
         throw new Exception("Ничего не выбрано");
     }
     _currentRange.InsertFile(pathToFile);
 }
Esempio n. 2
0
        public static void ReplaceBookmark(Word.Range rng, string html)
        {
            // var val = string.Format("Version:0.9\nStartHTML:80\nEndHTML:{0,8}\nStartFragment:80\nEndFragment:{0,8}\n", 80 + html.Length) + html + "<";
            // Clipboard.SetData(DataFormats.Html, val);
            //// Clipboard.SetText(val, TextDataFormat.Html);
            // rng.PasteSpecial(DataType: Word.WdPasteDataType.wdPasteHTML);

            rng.Font.Name = "Segoe UI";
            rng.Font.Size = 11;
            var file = "temp.html".GetFullPath();

            File.WriteAllText(file, html);
            rng.InsertFile(file);
        }
Esempio n. 3
0
        private void VacationDoc(List <vacParam> VacatorsDay)
        {
            Object missingObj = System.Reflection.Missing.Value;
            Object falseObj   = false;

            //создаем обьект приложения word
            application = new Word.Application();
            // создаем путь к файлу
            string path            = Path.GetFullPath(@"Данные\Шаблоны\Отпускники (шаблон).docx");
            Object templatePathObj = path;

            // если вылетим не этом этапе, приложение останется открытым
            try
            {
                document = application.Documents.Add(ref templatePathObj, ref missingObj, ref missingObj, ref missingObj);
            }
            catch (Exception error)
            {
                document.Close(ref falseObj, ref missingObj, ref missingObj);
                application.Quit(ref missingObj, ref missingObj, ref missingObj);
                document    = null;
                application = null;
                throw error;
            }

            int i = 0;

            //заполняем таблицу в документе
            foreach (vacParam vp in VacatorsDay)
            {
                if (i != 0)
                {
                    document.Tables[1].Rows.Add(ref missingObj);
                }

                for (int j = 0; j < tableLSDataGridView1.RowCount - 1; j++)
                {
                    if (tableLSDataGridView1[0, j].Value.ToString() == vp.korshId.ToString())
                    {
                        document.Tables[1].Cell(i + 2, 2).Select(); //+2 потому что заполнение начинается со 2 строки таблицы
                        application.Selection.TypeText(allKorshuns[1, j]);

                        document.Tables[1].Cell(i + 2, 3).Select();
                        application.Selection.TypeText(allKorshuns[3, j] + allKorshuns[4, j].Substring(0, 1) + "." + allKorshuns[5, j].Substring(0, 1) + ".");

                        break;
                    }
                }

                document.Tables[1].Cell(i + 2, 4).Select();
                application.Selection.TypeText(vp.endDate.ToShortDateString().ToString() + " 20:00");

                if (!vp.otmetka)
                {
                    document.Tables[1].Cell(i + 2, 5).Select();
                    application.Selection.TypeText("Без отметки");
                }

                i++;
            }

            //выбираем закладки и вставляем туда текст
            document.Bookmarks["dateStart"].Select();
            string date = VacatorsDay[0].startDate.ToLongDateString();

            date = '\u00ab' + VacatorsDay[0].startDate.Day.ToString() + '\u00bb' + date.Substring(date.IndexOf(' '));
            application.Selection.TypeText(date);



            document.Bookmarks["what"].Select();
            if (VacatorsDay[0].startDate.ToShortDateString() == monthCalendar1.SelectionStart.ToShortDateString())
            {
                application.Selection.TypeText("убывающего");
                document.Bookmarks["otmetka"].Select();
                application.Selection.TypeText(monthCalendar1.SelectionStart.AddDays(1).ToShortDateString());
            }
            else
            {
                application.Selection.TypeText("убывшего");
                document.Bookmarks["otmetka"].Select();
                application.Selection.TypeText(monthCalendar1.SelectionStart.ToShortDateString());
            }


            object missing = Missing.Value;
            object what    = Word.WdGoToItem.wdGoToLine;
            object which   = Word.WdGoToDirection.wdGoToLast;

            Word.Range endRange = document.GoTo(ref what, ref which, ref missing, ref missing);

            //составляем путь для сохранения временного файла
            string strVr           = Environment.CurrentDirectory + @"\Данные\Шаблоны\temp.doc";
            Object pathToSaveObjVr = strVr;

            //Вставка в другой файл
            endRange.InsertFile(strVr);
            //сохраняем во временный файл
            document.SaveAs(ref pathToSaveObjVr, Word.WdSaveFormat.wdFormatDocument, ref missingObj, ref missingObj, ref missingObj, ref missingObj, ref missingObj, ref missingObj, ref missingObj, ref missingObj, ref missingObj, ref missingObj, ref missingObj, ref missingObj, ref missingObj, ref missingObj);

            //закрытие файла
            document.Close(ref falseObj, ref missingObj, ref missingObj);
            application.Quit(ref missingObj, ref missingObj, ref missingObj);
            document    = null;
            application = null;
        }