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

作者:Regina 时间:2022-08-04 23:33:13 

本文实例讲述了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#程序设计有所帮助。

标签:C#,Word
0
投稿

猜你喜欢

  • java实现学生选课系统

    2022-11-19 14:58:24
  • C#将DataTable转换成list的方法

    2023-03-06 01:20:45
  • Android提醒微技巧你真的了解Dialog、Toast和Snackbar吗

    2023-03-08 14:15:44
  • SpringBootTest单元测试报错的解决方案

    2021-09-08 23:25:47
  • C#端口扫描器的编写方法

    2023-12-17 17:47:24
  • 详解Servlet3.0新特性(从注解配置到websocket编程)

    2023-08-08 14:29:48
  • springboot+vue部署按照及运行方法

    2023-07-15 06:16:09
  • IDEA导入Eclipse项目的方法步骤(图文教程)

    2023-07-17 09:45:09
  • C#使用Gembox.SpreadSheet向Excel写入数据及图表的实例

    2021-09-30 01:58:54
  • springboot使用Logback把日志输出到控制台或输出到文件

    2022-05-30 17:34:30
  • Android OpenCV基础API清晰度亮度识别检测

    2023-01-04 14:10:27
  • C#最简单的字符串加密解密方法

    2022-10-30 12:46:36
  • 基于springboot 配置文件context-path的坑

    2021-07-04 17:37:27
  • 基于java构造方法Vector查找元素源码分析

    2023-11-29 04:33:30
  • JAVASE系统实现抽卡功能

    2023-11-19 19:49:41
  • JavaSE实现猜拳游戏

    2023-12-22 07:21:19
  • 详解用maven将dubbo工程打成jar包运行

    2023-10-20 04:34:42
  • Android SQLite数据库连接实现登录功能

    2022-09-04 01:51:46
  • 教你java面试时如何聊单例模式

    2022-04-14 05:32:57
  • maven 配置多个仓库的方法

    2021-06-09 03:30:24
  • asp之家 软件编程 m.aspxhome.com