欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#创建一个Word并打开的方法

程序员文章站 2023-11-13 13:24:58
本文实例讲述了c#创建一个word并打开的方法。分享给大家供大家参考。具体实现方法如下: 复制代码 代码如下:private static string _createn...

本文实例讲述了c#创建一个word并打开的方法。分享给大家供大家参考。具体实现方法如下:

复制代码 代码如下:
private static string _createnewword(string alltnc)
{
    microsoft.office.interop.word.document worddocument = null;
    microsoft.office.interop.word.application wordapplication = null;
    string datetimenow = datetime.now.tostring();
    string wordpath = path.gettempfilename();
    wordapplication = new word.applicationclass();
    object nothing = missing.value;
    worddocument = wordapplication.documents.add(ref nothing, ref nothing, ref nothing, ref nothing);
    worddocument.paragraphs.last.range.text = alltnc;
    object format = word.wdsaveformat.wdformatdocumentdefault;
    worddocument.saveas(wordpath, 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);
    worddocument.application.documents.close(ref nothing, ref nothing, ref nothing);
    ((word.applicationclass)wordapplication).quit(ref nothing, ref nothing, ref nothing);
    return wordpath;
}
private static void _importtnctoactivedocument(string wordpath)
{
    word.application wordapplication = new word.application();
    word.document worddocument = new word.document();
    object nothing = system.reflection.missing.value;
    object filepath = wordpath;
    wordapplication.documents.open(ref filepath, 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, ref   nothing);
    worddocument = wordapplication.activedocument;
    wordapplication.visible = true;
}

vs10-office项目中创建thisaddin按钮实现某些功能当打开多个word时便获取不到当前word文档对象(如需要获取打开的第一个文档中的bookmark)

可以在 startup 中加入:

viteribbon viteribbon = new viteribbon(this.application);

构造函数传递该参数并赋值:

currentdoucment = wordapp.activedocument;

希望本文所述对大家的c#程序设计有所帮助。