oWord = new Application();
            object fileName = filePath;
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
          
            object nothing = Missing.Value;
       
            oDoc = oWord.Documents.Add(ref nothing, ref nothing, ref nothing, ref nothing);
            oDoc.Paragraphs.Last.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
            Paragraph paragHeader = null;            paragHeader = oDoc.Paragraphs.Add(ref nothing);
            paragHeader.Range.Font.Size = 22;
            paragHeader.Range.Text = title;
            paragHeader.Range.Font.Bold = 1;
            paragHeader.Range.Font.Name = "宋体";
            paragHeader.Range.InsertParagraphAfter();为什么在 oDoc = oWord.Documents.Add(ref nothing, ref nothing, ref nothing, ref nothing);出错!
oDoc为null

解决方案 »

  1.   


     private ApplicationClass wapp = new ApplicationClass();        private  Document Dw = new Document();
     object missing = System.Reflection.Missing.Value;
      Dw = wapp.Documents.Open(ref filename, ref missing, ref readOnly, ref missing, ref missing, ref missing,
                                         ref missing, ref missing, ref missing, ref missing, ref missing,
                                         ref missing, ref missing, ref missing, ref missing, ref missing);
    .....
     Dw.SaveAs(ref savename, ref missing,
                          ref missing,
                          ref missing,
                          ref missing,
                          ref missing,
                          ref missing,
                          ref missing,
                          ref missing,
                          ref missing,
                          ref missing,
                          ref missing,
                          ref missing,
                          ref missing,
                          ref missing,
                          ref missing);
      

  2.   

    ////WordToHtml方法
        //public static string WordToHtml(FileUpload fup, string location, string newName)
        //{
        //    string flag;//标记
        //    try
        //    {
        //        string fileName = fup.PostedFile.FileName;
        //        Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
        //        Type wordType = word.GetType();
        //        Microsoft.Office.Interop.Word.Documents docs = word.Documents;
        //        Type docsType = docs.GetType();// 打开文件  
        //        object oFileName = fileName;
        //        Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
        //        doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { oFileName, true, true });
        //        // 转换格式,另存为html
        //        Type docType = doc.GetType();
        //        //被转换的html文档保存的位置
        //        string ConfigPath = HttpContext.Current.Server.MapPath(location + newName + ".html");
        //        object saveFileName = ConfigPath;
        //        docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
        //        wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);// 退出 Word 
        //        return flag = "true";       
        //    }
        //    catch (Exception ex)
        //    {
        //        eturn flag=ex.ToString();
        //    }
        //
    参考