-3
你能不能幫我解決以下錯誤消息:需要幫助C#錯誤:未將對象引用設置到對象的實例
"Object reference not set to an instance of an object."
我標誌着區域我得到的錯誤意見和**
請參閱下文。
private static void _GenerateWord(string fname, string reportStartDate, string reportEndDate)
{
var word = new Microsoft.Office.Interop.Word.Application();
var doc = new Microsoft.Office.Interop.Word.Document();
word.Visible = false;
object missing = Type.Missing;
object fileName = (@LetterTemplateLocation + LetterName);
doc = word.Documents.Open(ref fileName);
doc.Activate();//**Error message here "Object reference not set to an instance of an object."**
string dateWithFormat = DateTime.Now.ToString("MMMM d, yyyy");
//**Error message here "Object reference not set to an instance of an object."**
foreach (Microsoft.Office.Interop.Word.Range tmpRange in doc.StoryRanges)
{
_FindAndReplace("<date>", dateWithFormat, tmpRange, missing);
_FindAndReplace("<filename>", fname, tmpRange, missing);
_FindAndReplace("<startdate>", reportStartDate, tmpRange, missing);
_FindAndReplace("<enddate>", reportEndDate, tmpRange, missing);
}
if (doc != null)
{
doc.Close(ref missing, ref missing, ref missing);
word.Application.Quit(ref missing, ref missing, ref missing);
}
}
謝謝。
可能重複[什麼是NullReferenceException,我該如何解決它?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-doi-i-fix-它) –
看起來像'doc' get null。你確定'fileName'是一個現有的word文檔文件的路徑嗎? –
我會檢查fileName是否先存在.. bool found = File.Exist(fileName) – PhillyNJ