引用
Microsoft Office 14.0 Object Library
Microsoft Word 14.0 Object Library
using MSWord = Microsoft.Office.Interop.Word;
定义
private MSWord.Application m_word;
private MSWord.Document m_doc;
事件
m_word = new MSWord.Application();
Object filename = "XXX前端手册.docx";
Object filefullname = System.Windows.Forms.Application.StartupPath + "\\XXX前端手册.docx";
Object confirmConversions = Type.Missing;
Object readOnly = Type.Missing;
Object addToRecentFiles = Type.Missing;
Object passwordDocument = Type.Missing;
Object passwordTemplate = Type.Missing;
Object revert = Type.Missing;
Object writePasswordDocument = Type.Missing;
Object writePasswordTemplate = Type.Missing;
Object format = Type.Missing;
Object encoding = Type.Missing;
Object visible = Type.Missing;
Object openConflictDocument = Type.Missing;
Object openAndRepair = Type.Missing;
Object documentDirection = Type.Missing;
Object noEncodingDialog = Type.Missing;
for (int i = 1; i <= m_word.Documents.Count; i++)
{
String str = m_word.Documents[i].FullName.ToString();
if (str == filefullname.ToString())
{
MessageBox.Show("请勿重复打开该文档");
return;
}
}
try
{
m_word.Documents.Open(ref filefullname,
ref confirmConversions, ref readOnly, ref addToRecentFiles,
ref passwordDocument, ref passwordTemplate, ref revert,
ref writePasswordDocument, ref writePasswordTemplate,
ref format, ref encoding, ref visible, ref openConflictDocument,
ref openAndRepair, ref documentDirection, ref noEncodingDialog
);
m_word.Visible = true;
}
catch (System.Exception ex)
{
MessageBox.Show("打开Word文档出错");
}