object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined book *///Start Word and create a new document.
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;

object oTemplate = "d:\\f\\word\\MyTemplate.dot";
                
oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing,
ref oMissing, ref oMissing);object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined book *///Start Word and create a new document.
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;

object oTemplate = "d:\\f\\word\\MyTemplate.dot";
                
oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing,
ref oMissing, ref oMissing);
object oBookMark = "MyBook";
Word.Table oTable;
Word.Range wrdRng = oDoc.Books.Item(ref MyBook).Range;//(错误在这句)

解决方案 »

  1.   

    Word.Range wrdRng = oDoc.Books.Item(ref MyBook).Range;//(错误在这句)
    ref MyBook--MyBook你没有定义啊!
      

  2.   

    MyBook 应该是 oBookMark 吧?
      

  3.   

    改过来了,还是同样的错误。
    单独编译slope为windows应用程序运行是没有问题的。只是编译为DLL,然后被work项目引用就出现问题了。
      

  4.   

    if ( oWord != null )
    {
        oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing,
    ref oMissing, ref oMissing);
    }
    else
    {
        MessageBox.Show("Null");
    }
      

  5.   

    if ( !Object.ReferenceEquals(oWord,null) )
    {
        oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing,
    ref oMissing, ref oMissing);
    }
    else
    {
        MessageBox.Show("Null");
    }
      

  6.   

    可能是oDoc.Books.Item(ref MyBook)为null
      

  7.   

    !Object.ReferenceEquals(oWord,null) 返回是真的。
    应该是oDoc.Books.Item(ref MyBook)为null,在slope编译的windows应用程序,执行那段没有问题。把他编译为dll被work引用的时候,slope作为work窗体的一个子窗体,这种情况下就不行了。
    调用slope.dll的代码是:Assembly asm = Assembly.LoadFile(System.Windows.Forms.Application.StartupPath+"\\slope.dll");
    Type formType = asm.GetType("slope.Form1");
    Form[] charr = this.MdiChildren;
    Form frm = OpenForm(formType);Form OpenForm(Type formType)
    {
    Form frm = formType.InvokeMember(null, 
    BindingFlags.DeclaredOnly 
    | BindingFlags.Public 
    | BindingFlags.NonPublic
    | BindingFlags.Instance 
    | BindingFlags.CreateInstance,
    null, null, null) as Form;
    frm.MdiParent = this;
    frm.Show();
    return frm;
    }
      

  8.   

    谢谢各位了,object oTemplate = "d:\\f\\word\\MyTemplate.dot";
    是这句的路径不对。