实现VSTO操作WORD之一 --------简单HELLO WORD!

本文介绍如何使用Visual Studio 2005通过VSTO(Visual Studio Tools for Office)创建带有交互功能的Word文档。具体步骤包括创建项目、添加按钮并编写简单代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于本人的语文水平有限,再加上是上班时候编写的,所以有什么问题请大家多多提出来。

首先让我们启动VS2005, 创建项目--C#--OFFICE--WORD文档

点击确定后我们选择创建新文档,有的朋友可能第一次创建会出现一个提示,大概是询问是否创建宏,

点击“是”即可。接下来就是等待创建文档,好了!文档创建好了。看了一看是不是和WINFORM差不多?

就是工作区变成了WORD文档了,哈哈!下面就来编写一个简单的代码吧,现在往左面看,把

按钮直接拖到WORD文档上,放手!哈哈!一个按钮出现了,接下来我们双击这个按钮,哇!

到了后台代码区了,这和以前的编程没什么两样嘛,对了,这也是VSTO的一大特点,同时可以

体验OFFICE和.NET编程的乐趣。。。下面我们将代码添加进去。

 

 MessageBox.Show("Hello world!!!!");

然后我们F5运行,呵呵!出来一个带按钮的文档,点击以后出现一个对话框 "Hello world!!!!"

 

public class MyAddin : IDTExtensibility2, IRibbonExtensibility { public static Application wpp; public static Application app = null; // 插件加载时触发 public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom) { wpp = (Application)application; wpp.DocumentOpen += OnDocumentOpen; app = wpp as Word.Application; } // 文档打开时插入“Hello World” private void OnDocumentOpen(Document doc) { //doc.Content.Text = "Hello World from COM Add-in!"; } // 其他接口方法(保持空实现) public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom) { } public void OnAddInsUpdate(ref Array custom) { } public void OnStartupComplete(ref Array custom) { } public void OnBeginShutdown(ref Array custom) { } public string GetCustomUI(string RibbonID) { return @" <customUI xmlns='http://schemas.microsoft.com/office/2019/07/customui' onLoad='Ribbon_Load'> <ribbon> <tabs> <tab id='MainTab' label='AI助手'> <!-- 新写功能 --> <group id='GroupNew' label='新创作'> <button id='btnNewWrite' label='新写' size='large' getImage='GetButtonImage' onAction='OnButtonClick' getEnabled='GetButtonEnabled' showItemLabel='true' itemWidth='200' itemHeight='80' screentip='新建文档' supertip='创建新的智能文档'/> <!-- 内容续写 --> <button id='btnContinueWrite' label='续写' size='large' getImage='GetButtonImage' onAction='OnButtonClick' getEnabled='GetButtonEnabled' screentip='继续写作' supertip='根据现有内容继续扩展写作'/> <!-- 内容扩展 --> <button id='btnExpandWrite' label='扩写' size='large' getImage='GetButtonImage' onAction='OnButtonClick' getEnabled='GetButtonEnabled' screentip='扩展内容' supertip='对现有内容进行详细扩展'/> <!-- 优化改写 --> <button id='btnRewrite' label='重写' 这是我现在的代码.我要在点击"新写"按钮时,在文档右侧打开一个窗格
最新发布
03-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值