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

C#创建一个Word并打开的方法,第1张

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

本文实例讲述了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);
    worddocument.Paragraphs.Last.Range.Text = allTnC;
    object format = Word.WdSaveFormat.wdFormatdocumentDefault;
    worddocument.SaveAs(wordpath,ref format,
 ref nothing,ref nothing);
    worddocument.Application.documents.Close(ref nothing,ref nothing);
    ((Word.ApplicationClass)wordApplication).Quit(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);
    worddocument = wordApplication.Activedocument;
    wordApplication.Visible = true;
}
vs10-office项目中创建ThisAddIn按钮实现某些功能当打开多个word时便获取不到当前word文档对象(如需要获取打开的第一个文档中的bookmark)

可以在 Startup 中加入:

ViteRibbon viteRibbon = new ViteRibbon(this.Application);

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

currentDoucment = wordApp.Activedocument;

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

总结

以上是内存溢出为你收集整理的C#创建一个Word并打开的方法全部内容,希望文章能够帮你解决C#创建一个Word并打开的方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1254953.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存