首页 > 代码库 > C#打开word 编辑后保存时自动化响应

C#打开word 编辑后保存时自动化响应

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using MSWord=Microsoft.Office.Interop.Word;

 

namespace word
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
MSWord.Application wordApp;

private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog P_GetFile = new OpenFileDialog(); //创建打开文件对话框对象
DialogResult P_dr = P_GetFile.ShowDialog(); //显示打开文件对话框
object oMissing = System.Reflection.Missing.Value;
if (P_dr == DialogResult.OK) //是否单击确定
{
wordApp = new MSWord.Application();
wordApp.Visible = true;
object obj = (object)(P_GetFile.FileName);
wordApp.Documents.Open(ref obj, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
}
wordApp.DocumentBeforeSave += new MSWord.ApplicationEvents4_DocumentBeforeSaveEventHandler(wordApp_DocumentBeforeSave);
}
private void wordApp_DocumentBeforeSave(MSWord.Document doc, ref bool SaveAsUI, ref bool Cancel)
{
MessageBox.Show("hello office");
}
}
}

C#打开word 编辑后保存时自动化响应