1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Word;
using System.IO;
using System.Runtime.InteropServices;
namespace StudyPlanReader
{
public abstract class AWordReader
{
private String filePath;
private _Application word;
private _Document document;
private object missing = System.Reflection.Missing.Value;
private object readOnly = false;
protected AWordReader(String filePath)
{
this.FilePath = filePath;
word = new Microsoft.Office.Interop.Word.Application();
word.Visible = false;
}
public String FilePath
{
get
{
return this.filePath;
}
private set
{
this.filePath = value;
}
}
public _Document Document
{
get
{
return this.document;
}
}
public void Open()
{
try
{
object name = this.FilePath;
this.document = this.word.Documents.Open(ref name, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
}
catch (Exception e)
{
throw new IOException(e.Message);
}
}
public void Close()
{
try
{
if (this.document != null)
{
this.document.Close(missing, missing, missing);
}
if (this.word != null)
{
this.word.Quit(missing, missing, missing);
}
Marshal.FinalReleaseComObject(this.document);
Marshal.FinalReleaseComObject(this.word);
GC.Collect();
GC.WaitForPendingFinalizers();
}
catch (COMException) { }
catch (Exception e)
{
throw new IOException(e.Message);
}
}
}
} |
Partager