[C#]利用VSTO操作Office文档而无需安装Office

news/2024/6/18 13:10:59

[C#]利用VSTO操作Office文档而无需安装Office

编写者

日期

关键词

郑昀@ultrapower

2005-8

Vsto office c# Interop word

 

1.1. VSTO

   VSTO,就是Visual Studio Tools for the Microsoft Office System。可以在这里找到更多信息:

       http://msdn.microsoft.com/office/understanding/vsto/default.aspx

首先,必须在系统中安装VSTO(不用安装Office即可使用)

       为了使用VSTO,我们的工程需要引入如下引用:

1-1 引用示范

       其中指的是“Microsoft.Office.Interop.Word”,你可以通过下面的图样了解如何添加这个COM引用:

1-2 添加Word引用

其中指的是“Microsoft Office 11.0 Object Library”,你可以通过下面的图样了解如何添加这个COM引用:

1-3 添加Office.Core引用

 

1.2. Word.ApplicationClass打开文档

   Word打开指定的文档很简单。

代码

// a reference to Word application

private Microsoft.Office.Interop.Word.ApplicationClass m_oWordApp =

new Microsoft.Office.Interop.Word.ApplicationClass();

// a reference to the document

private Microsoft.Office.Interop.Word.Document m_oDoc;

 

object fileName = strDocumentFilePath;             

 

m_oWordApp.Visible = false;

 

m_oDoc =

    m_oWordApp.Documents.Open(ref fileName, ref missing,ref readOnly,

    ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,

    ref missing, ref missing, ref isVisible,ref missing,ref missing,ref missing

    ,ref missing);

 

m_oDoc.Activate();

 

/// http://msdn2.microsoft.com/library/wt26ady8(en-us,vs.80).aspx

///  convert all list numbers and LISTNUM fields in the document to text

object numberType =

    Microsoft.Office.Interop.Word.WdNumberType.wdNumberAllNumbers;

m_oDoc.ConvertNumbersToText(ref numberType);

       记得调用Microsoft.Office.Interop.Word.Document.Activate()将当前打开的文档激活。

ConvertNumbersToText方法是用来把文档中所有的编号符号转换为文本的。

1.3. Word.Range选定文档范围

   还有Word.Range这个接口,可以选定某一段文字,按照指定的方式复制出来。

代码

object rangeStart = begin;

object rangeEnd = (end < nCount)?end:nCount;

Microsoft.Office.Interop.Word.Range rng =

    m_oDoc.Range(ref rangeStart, ref rangeEnd);

rng.Select();

 

/

///

Microsoft.Office.Interop.Word.TextRetrievalMode RetrievalMode =

    rng.FormattedText.TextRetrievalMode;

RetrievalMode.IncludeHiddenText = false;

RetrievalMode.IncludeFieldCodes = false;

/// sets the view for text retrieval to Web view

RetrievalMode.ViewType =

    Microsoft.Office.Interop.Word.WdViewType.wdWebView;

///

/

 

String strYourWord = rng.FormattedText.Text;

  

1.4. 销毁一切

   无论发生了什么事情,都必须保证WinWord.exe实例被释放,这是一个服务的基本要求。

代码

/// 关闭打开的文档:

if(m_oDoc != null)

{

    m_oDoc.Close(ref saveChanges, ref missing, ref missing);

    m_oDoc = null;

}

if(m_oWordApp != null)

{

    //  这里就不要再判断if(m_oWordApp.Application.ActiveDocument != null)

    //  否则会出现“System.Runtime.InteropServices.COMException (0x800A1098): 因为没有打开的文档,所以这一命令无效。”

    //  这样的异常!

    m_oWordApp.Application.Quit(ref saveChanges, ref missing, ref missing);

   

    m_oWordApp = null;

}

  

  

编写者

日期

关键词

郑昀@ultrapower

2005-8

Vsto office c# Interop word





http://www.niftyadmin.cn/n/3649571.html

相关文章

使用john 软件破解CentOS系统用户密码

时隔18天&#xff0c;我的博客终于又更新啦。最近真的是忙的不要不要的&#xff01; 一、新建两个用户用于被破解对象 新建用户Root:useradd Root 设置Root的密码&#xff1a;passwd Root 新建用户admin:useradd admin 设置admin的密码&#xff1a;passwd admin二、在centos系…

Intent 详解(一)

前言&#xff1a;通过重新翻看android入门书籍&#xff0c;才发现原来自己露掉了那么多基础知道。原以为有了C的基础&#xff0c;快速开发应该是没有问题的了&#xff0c;但没有遇到问题的时候还是海搜&#xff0c;只知道这么写能完成这个功能&#xff0c;但为什么要这么写还是…

[ASP.NET]重构Session确实让代码简洁干净了不少

CodeProject的这篇文章确实对我有所启迪&#xff0c;http://www.codeproject.com/useritems/SessionWrapper.asp#xx1208856xx。诚如作者所说&#xff0c;我们经常在ASP.NET用许多类似于下面的代码来检测Session中存储的对象&#xff0c;来防止Session过期后存储的变量丢失问题&…

redis修改配置重启命令_如何从命令行更改Redis的配置

redis修改配置重启命令介绍 (Introduction) Redis is an open-source, in-memory key-value data store. Redis has several commands that allow you to make changes to the Redis server’s configuration settings on the fly. This tutorial will go over some of these c…

SSH连接工具FinalShell的安装与使用

常见的SSH连接工具有好多&#xff0c;我以前常用的是xshellxftp组合。虽然需要付费&#xff0c;但是免费版已经基本满足我的需求。关于xshell和xftp的安装和使用可以参考我以前的博客&#xff1a;Xftp和Xshelll的安装—远程连接linux FinalShell是一体化的的服务器,网络管理软件…

[j2me]利用kSOAP让MIDP设备与WebService之间传递类对象

[j2me]利用kSOAP让MIDP设备与WebService之间传递类对象编写者日期关键词郑昀ultrapower2005-8-14J2me webservice soa ksoap serialization MIDP CLDC中国移动GPRS网络的连接方式有两种类型&#xff0c;一种是WAP&#xff0b;GPRS&#xff0c;接入名称叫CMWAP&#xff0c;一种是…

盖茨比乔布斯_盖茨比中的自定义字体

盖茨比乔布斯Choosing the right font can add great value to a site and enhance the user experience. The right font-loading strategy, however, can be somewhat of a challenge. That’s why Gatsby provides several developer-friendly solutions for all of our fon…

Android中GridView使用

GridView(网格视图)是按照行列的方式来显示内容的&#xff0c;一般用于显示图片&#xff0c;图片等内容&#xff0c;比如实现九宫格图&#xff0c;用GridView是首选&#xff0c;也是最简单的。主要用于设置Adapter。 GridView常用的XML属性&#xff1a; 属性名称 描述 an…