前回はエクセルファイルだったので今回はワードの紹介です。
以下ソースコード
■前提
参照の追加で「Microsoft Word ~~~ Object Library」を追加する。
usingに「Microsoft.Office.Interop.Word」と「System.Runtime.InteropServices」いれる
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 |
using System; using System.Windows.Forms; using Microsoft.Office.Interop.Word; using System.Runtime.InteropServices; namespace Sample { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Button1_Click(object sender, EventArgs e) { string Folderpath = @" (ファイルパス) "; Microsoft.Office.Interop.Word.Application WordApp = null; Documents documents = null; Document document = null; WordApp = new Microsoft.Office.Interop.Word.Application(); WordApp.Visible = true; documents = WordApp.Documents; document = documents.Open(Folderpath); Marshal.ReleaseComObject(documents); Marshal.ReleaseComObject(document); //app解放 Marshal.ReleaseComObject(WordApp); GC.Collect(); } } } |
エクセルのときとあまり変わらないですね。
取り敢えずCOM参照の解放処理だけやっとけばいいと思います。
コメントを残す