希望打开一个DOC,代码为:
using Word=*****(忘了)
Word.Application m_app;
Word.DocumentClass m_doc=new Word.DocumentClass();
m_app=m_doc.Application;
object fileName="D:/123.doc";
object missing=Type.Missing;
Word.Document tempDoc=m_app.Documents.Open(ref fileName,ref missing.......(剩下的都是ref missing));
m_app.Visable=true;
tempDoc.Active();//方法名好像是这样,反正是为了DOC显示出来用的。
每次运行都是会出现两个文档,一个是新建的,一个是123.doc,
应该新建的那个文档是m_doc=new DocumentClass()的时候的实例,可是没有实例又如何赋值给m_app呢?
目的:只用打开我想用的123.doc,不要新建的那个文档。
入门问题,大家别笑话。先谢谢了

解决方案 »

  1.   

    这个是你操作了一个word对象 它有自己的构造函数。
      

  2.   

    (1)创建一个Windows控制台应用程序,命名为CreateWordXDemo。(2)添加对Microsoft Word 12.0 Object Library的引用。(3)在“Program.cs”文件中添加如下引用。using MSWord = Microsoft.Office.Interop.Word;using System.IO;using System.Reflection;(4)直接修改“Program.cs”文件的代码如下。class Program{    static void Main(string[] args)    {        object path;                               //文件路径变量        string strContent;                         //文本内容变量        MSWord.Application wordApp;                    //Word应用程序变量        MSWord.Document wordDoc;                   //Word文档变量                path = @"C:\MyWord.docx";                      //路径        wordApp = new MSWord.ApplicationClass();  //初始化        //如果已存在,则删除        if (File.Exists((string)path))        {            File.Delete((string)path);        }        //由于使用的是COM库,因此有许多变量需要用Missing.Value代替        Object Nothing = Missing.Value;        wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);        strContent = "使用C#向Word文档中写入文本\n";        wordDoc.Paragraphs.Last.Range.Text = strContent;        strContent = "写入第二行文本";        wordDoc.Paragraphs.Last.Range.Text = strContent;        //WdSaveFormat为Word 2007文档的保存格式        object format =MSWord.WdSaveFormat.wdFormatDocumentDefault;        //将wordDoc文档对象的内容保存为DOCX文档         wordDoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);        //关闭wordDoc文档对象         wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);        //关闭wordApp组件对象         wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);        Console.WriteLine(path + " 创建完毕!");    }}
      

  3.   

    真快。我刚写完就想明白了,回去试了下。
    同时也谢谢你了。
    对了,你知道怎么操作word中的表格吧。有没有例子看看啊。
    这样的,我有word中的一个表的内容需要导入sqlserver2000。
    本来想通过word07转换为文本再导入数据库的,后来发现表格中有部分单元格中的内容是有段落符和制表符等等,所以转换为的文本可以通过特殊字符区分列,可是不可以区分行的内容。所以一气之下想写个程序了处理word的表格。
    先给分你,不过希望可以再给我一个答案。谢谢。
      

  4.   

    我用上述代码为什么会有下述错误:“错误 1 命名空间“Microsoft”中不存在类型或命名空间名称“Office”(是缺少程序集引用吗?) ”