Skip to content

Instantly share code, notes, and snippets.

@Olbergx
Created August 27, 2019 09:28
Show Gist options
  • Save Olbergx/2d59d70528b3be5d0e460b340943dad9 to your computer and use it in GitHub Desktop.
Save Olbergx/2d59d70528b3be5d0e460b340943dad9 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button2_Click(object sender, EventArgs e)
{
Close();
}
private void Button1_Click(object sender, EventArgs e)
{
int inta = (Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text));
label1.Text = inta.ToString();
try
{
//запускаем Word и открываем шаблон файла
object fileName = Application.StartupPath + "\\reports\\Doc1.doc";
//object fileName = "C:\VS\reports\\Doc1.doc";
Word.Application WordApplication = new Word.Application();
WordApplication.Visible = true; //выводим документ на экран
Word.Document WordDocument = WordApplication.Documents.Open(fileName);
//присваиваем значение полю
WordDocument.Variables["DOCDATE"].Value = label1.Text;
// DateTime.Now.ToShortDateString();
//обновляем поля документа
WordDocument.Fields.Update();
//Word.Document.SaveAs("\\reports\\Doc2.doc");
WordDocument = WordApplication.Application.ActiveDocument;
WordDocument.SaveAs(@"C:\VS\reports\Doc2.doc");
WordApplication.Quit();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment