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
| class GestionWord
{
#region Déclarations
Microsoft.Office.Interop.Word.Application _ApplicationWord;
Microsoft.Office.Interop.Word._Document _MonDocument;
//Microsoft.Office.Interop.Word.Selection _MaSelection;
Microsoft.Office.Interop.Word.Range _MonRange;
Bookmark _MonSignet;
Bookmarks _MesSignets;
InlineShape _MonImage;
object M = System.Reflection.Missing.Value;
#endregion
#region Initialisation Sauvegarde
/// <summary>
/// Cette classe sert d'interface entre word et une apllication C#.
/// Le constructeur démarre word.
/// </summary>
public GestionWord()
{
//_ApplicationWord = new ApplicationClass();
_ApplicationWord = new Microsoft.Office.Interop.Word.Application();
_ApplicationWord.Visible = true;
}
/// <summary>
/// Ouvre un fichier existant.
/// </summary>
/// <param name="FileName">Chemin du document</param>
public void OuvrirFichier(string FileName)
{
object oFileName = (object)FileName;
object Faux = (object)false;
object Vrai = (object)true;
_MonDocument = _ApplicationWord.Documents.Add(ref oFileName, ref Faux, ref M, ref Vrai);
}
/// <summary>
/// Enregistrer le document sous.
/// </summary>
/// <param name="FileName">Chemin du docmuent</param>
public void Sauver(string FileName)
{
object oFileName = (object)FileName;
object Faux = (object)false;
object Vrai = (object)true;
try
{
_MonDocument.SaveAs(ref oFileName, ref M, ref M, ref M, ref M, ref M, ref M, ref M, ref M, ref M, ref M, ref M, ref M, ref M, ref M, ref M);
}
catch
{
Console.WriteLine("Le fichier Word n'a pas pu être enregistré.", "Erreur d'enregistrement");
}
}
/// <summary>
/// Fermer le document et l'application word.
/// </summary>
public void Fermer()
{
_MonDocument.Close(ref M, ref M, ref M);
_ApplicationWord.Application.Quit(ref M, ref M, ref M);
}
#endregion
/// <summary>
/// Récupérer les données du formulaire
/// </summary>
/// <returns></returns>
public void RecupererFormulaire()
{
//récupére la liste de signets
_MesSignets = _MonDocument.Bookmarks;
foreach (Bookmark a in _MesSignets)
{
string nom = a.Name;
_MonRange = a.Range;
Console.WriteLine(_MonRange.Text.ToString());
Console.WriteLine(nom);
};
Console.ReadLine();
}
} |
Partager