2つのWordファイルの内容が一致しているかどうかを比較するC#解決方法
6946 ワード
using System;
using System.Windows.Forms;
using System.Diagnostics;
using Microsoft.Office.Interop.Word;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// WORD.EXE , , ,
KillProcess();
MessageBox.Show(CompareWordFile(@"C:\1.DOC", @"C:\2.DOC").ToString());
}
public bool CompareWordFile(String source, String target)
{
object filename = source;
var targetFileName = target;
object missing = System.Reflection.Missing.Value;
object readonlyobj = false;
var app = new ApplicationClass { Visible = false };
var doc = app.Documents.Open(ref filename, ref missing, ref readonlyobj, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
doc.TrackRevisions = true;
doc.ShowRevisions = true;
doc.PrintRevisions = true;
object comparetarget = WdCompareTarget.wdCompareTargetNew;
doc.Compare(targetFileName, ref missing, ref comparetarget, ref missing, ref missing, ref missing, ref missing, ref missing);
var changeCount = app.ActiveDocument.Revisions.Count;
Object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
doc.Close(ref saveChanges, ref missing, ref missing);
app.Quit(ref saveChanges, ref missing, ref missing);
return changeCount == 0;
}
public void KillProcess()
{
const string processName = "WINWORD";
var process = Process.GetProcessesByName(processName);
try
{
foreach (var p in process)
{
p.Kill();
}
}
catch (Exception)
{
MessageBox.Show(" WINWORD.EXE !", " ", MessageBoxButtons.OK);
return;
}
}
}
}