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
|
using System.Threading;
public class WordUtils {
private object missing = System.Reflection.Missing.Value;
private Microsoft.Office.Interop.Word.Application application;
private Document document;
private static TimeSpan waitTime = new TimeSpan (0, 0, 3);
private string resultat;
public string Resultat {
get { return resultat; }
set { resultat = value; }
}
public void CreateApplication () {
application = new Microsoft.Office.Interop.Word.Application ();
application.DisplayAlerts = WdAlertLevel.wdAlertsNone;
application.Visible = false;
}
...............
public string recupereObjetDoc (object filePath) {
try {
ThreadParametre ExempleThread = new ThreadParametre (this, application, document, filePath);
Thread t = new Thread (new ThreadStart (ExempleThread.ExecuteThread));
t.Start ();
if (t.Join (waitTime)) {
Console.WriteLine ("New thread terminated.");
} else {
Console.WriteLine ("Join timed out.");
}
return resultat;
} catch (Exception ex) {
return ex.StackTrace;
}
}
}
public class ThreadParametre {
private WordUtils wu;
private Application application;
private Document document;
private object filePath;
// Constructeur
public ThreadParametre (WordUtils wu, Application application, Document document, object filePath) {
this.wu = wu;
this.application = application;
this.document = document;
this.filePath = filePath;
}
// Exécution de la méthode du thread qui retourne le paragraphe n°2 du doc Word passé en paramètre
public void ExecuteThread () {
object confirmConversion = false;
object readOnly = true;
object visibleWindow = false;
object openAndRepair = false;
object revert = false;
object noEncodingDialog = true;
bool docClosed = false;
object missing = System.Reflection.Missing.Value;
wu.Resultat = "";
try {
document = application.Documents.Open (ref filePath, ref confirmConversion, ref missing, ref missing, ref missing, ref missing, ref revert,
ref missing, ref missing, ref missing, ref missing, ref visibleWindow, ref openAndRepair, ref missing,
ref noEncodingDialog, ref missing);
if (document.Content.Paragraphs.Count > 3) {
Microsoft.Office.Interop.Word.Paragraph paraNo2 = document.Content.Paragraphs[3];
if ((paraNo2.Range.End - paraNo2.Range.Start) > 1) {
string result = paraNo2.Range.Text;
if (document != null) {
((_Document) document).Close (ref missing, ref missing, ref missing);
docClosed = true;
}
wu.Resultat = result;
} else {
if (document != null) {
((_Document) document).Close (ref missing, ref missing, ref missing);
docClosed = true;
}
wu.Resultat = "";
}
} else {
if (document != null) {
((_Document) document).Close (ref missing, ref missing, ref missing);
docClosed = true;
}
wu.Resultat = "";
}
} catch (Exception ex) {
wu.Resultat = ex.Message + " - " + filePath;
}
}
} |
Partager