namespace MSOfficeRoutines public class MSWordConverterExcep tion Syst

  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
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
namespace MSOfficeRoutines
{
public class MSWordConverterException: System.Exception
{
public MSWordConverterException() { }
public MSWordConverterException(String msg)
{
exceptionMsg = msg;
}
private String exceptionMsg;
public override String Message
{
get
{
String msg = "Exception in MSOfficeRoutines.MSWordConverter: " + Environment.NewLine + base.Message + Environment.NewLine + exceptionMsg;
return msg;
}
}
}
public class MSWordConverter
{
public static int ConvertMSWord2PlainText(String docFilePath, String txtFilePath)
{
int retVal = 0;
FileInfo file = null;
try
{
object readOnly = false;
object isVisible = true;
object missing = System.Reflection.Missing.Value;
object saveChange = false;
object fileFormat = Word.WdSaveFormat.wdFormatText;
string dstFileName = docFilePath + ".txt";
object fileNameForSave = (object)(dstFileName);
Word.ApplicationClass wApp = new Word.ApplicationClass();
wApp.Visible = false;
object fileName = (object)docFilePath;
Word.Document wDoc = wApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
wDoc.Activate();
wDoc.SaveAs(ref fileNameForSave, ref fileFormat, 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, ref missing);
txtFilePath = dstFileName;
wApp.Quit(ref saveChange, ref missing, ref missing);
file = new FileInfo(txtFilePath);
FileAttributes fAtt = FileAttributes.Temporary;
if (file.Exists)
{
file.Attributes = fAtt;
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
retVal = -1;
String errMsg = ex.GetType().ToString() + Environment.NewLine + ex.Message;
try
{
if (file != null)
{
file.Delete();
}
throw new MSWordConverterException(errMsg);
}
catch(System.IO.IOException fEx)
{
throw new MSWordConverterException(errMsg + Environment.NewLine + fEx.Message);
}
}
catch (System.IO.IOException ex) //может возникнуть при изменении атрибутов файла
{
retVal = -1;
//throw new MSWordConverterException(ex.Message);
try
{
if (file != null)
{
file.Delete();
}
throw new MSWordConverterException(ex.Message);
}
catch (System.IO.IOException fEx)
{
throw new MSWordConverterException(ex.Message + Environment.NewLine + fEx.Message);
}
}
catch (Exception ex)
{
retVal = -1;
//throw new MSWordConverterException(ex.Message);
try
{
if (file != null)
{
file.Delete();
}
throw new MSWordConverterException(ex.Message);
}
catch (System.IO.IOException fEx)
{
throw new MSWordConverterException(ex.Message + Environment.NewLine + fEx.Message);
}
}
return retVal;
}
}
}