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

 更新时间:2015年04月14日 09:37:38   作者:Regina  
这篇文章主要介绍了C#创建一个Word并打开的方法,实例分析了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, 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#程序设计有所帮助。

相关文章

  • unity 如何判断鼠标是否在哪个UI上(两种方法)

    unity 如何判断鼠标是否在哪个UI上(两种方法)

    这篇文章主要介绍了unity 判断鼠标是否在哪个UI上的两种实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-04-04
  • c#文档图片自动纠偏

    c#文档图片自动纠偏

    最近找到一个不错的文档图片自动纠偏的方法,现在跟大家分享一下,需要的朋友可以参考下
    2014-03-03
  • C#使用LINQ查询文件列表并找出最大文件

    C#使用LINQ查询文件列表并找出最大文件

    在现代 C# 开发中,LINQ (Language Integrated Query) 提供了一种强大而优雅的方式来处理集合数据,本文将详细介绍如何使用 LINQ 查询文件系统中的文件,并找出最大的文件数量,需要的朋友可以参考下
    2024-10-10
  • C#动态加载dll扩展系统功能的方法

    C#动态加载dll扩展系统功能的方法

    这篇文章主要介绍了C#动态加载dll扩展系统功能的方法,涉及C#动态加载dll扩展的相关技巧,需要的朋友可以参考下
    2015-04-04
  • C# 遍历枚举类型的所有元素

    C# 遍历枚举类型的所有元素

    写个小东西,刚好用到枚举类型,需要显示在DropDownList控件中。尝试了下,用如下方法可以实现
    2013-03-03
  • Unity中的Tilemap流程分析

    Unity中的Tilemap流程分析

    这篇文章给大家介绍Unity中的Tilemap流程分析,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2021-07-07
  • C#设计模式之观察者模式实例讲解

    C#设计模式之观察者模式实例讲解

    这篇文章主要介绍了C#设计模式之观察者模式实例讲解,本文详细讲解了观察者模式的定义、优缺点、代码实例等,需要的朋友可以参考下
    2014-10-10
  • java和c#使用hessian通信的方法

    java和c#使用hessian通信的方法

    这篇文章主要介绍了java和c#使用hessian通信的方法,服务器端为Java,客户端为C#实现。是一个非常实用的技巧,需要的朋友可以参考下
    2014-09-09
  • c# 通过代码开启或关闭防火墙

    c# 通过代码开启或关闭防火墙

    这篇文章主要介绍了c# 通过代码开启或关闭防火墙的示例,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下
    2020-10-10
  • 分享WCF文件传输实现方法---WCFFileTransfer

    分享WCF文件传输实现方法---WCFFileTransfer

    这篇文章主要介绍了分享WCF文件传输实现方法---WCFFileTransfer,需要的朋友可以参考下
    2015-11-11

最新评论