代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MSWord = Microsoft.Office.Interop.Word;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private MSWord.Application m_word;
private MSWord.Document m_doc;
public Form1()
{
InitializeComponent();
m_word = new MSWord.Application();
Object filefullname = "d:\\16_up.doc";
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檔案出錯");
}
}
}
}
卡死一段時間后報
“System.Runtime.InteropServices.COMException”型別的未經處理的例外在 mscorlib.dll 中發生
其他資訊: 檢索 COM 類工廠中 CLSID 為 {000209FF-0000-0000-C000-000000000046} 的組件失敗,原因是出現以下錯誤: 80080005 服務器運行失敗 (例外來自 HRESULT:0x80080005 (CO_E_SERVER_EXEC_FAILURE))。

系統是WINDOWS10,VS2015,office2013
uj5u.com熱心網友回復:
安裝的是完整版的微軟Office么?在VS里面重新參考下Office的庫還不行的話,參考 https://blog.csdn.net/siyusiying/article/details/52346673
uj5u.com熱心網友回復:
換了2019還是不行轉載請註明出處,本文鏈接:https://www.uj5u.com/net/22820.html
標籤:C#
上一篇:vb.net引數傳陣列問題
