NET操作Word(傻瓜型)

一.介绍理论

现在讲的“宏”是只是针对OFFICE中的。

 :能自动执行某种操作的命令统称为。

VBA:是指Visual Basic for Application。它是在Office中使用的宏语言,主要为了增强Word、Excel等软件的自动化能力。VBA的语法类似VB,但提供了很多VB中没有的函数和对象,这些函数、对象都是针对Office应用的。

二.在OFFICE中利用宏

宏在Office(Word)中的应用

                

具――宏――录制新宏

然后进行一个操作。比如:查入一张图片 然后“停止宏的录制”

工具――宏――宏…--查看
宏代码:Sub Macro1()
 Macro1 Macro
'
宏在 2004-11-26 用户46 录制'
 Selection.InlineShapes.AddPicture FileName:= _
       "
C:"Documents and Settings"Administrator"桌面"2003121512223366481.bmp", LinkToFile:=False, SaveWithDocument:=True
End Sub
代码

以上只是介绍了宏的最简单的用法。宏还可以操作工具栏。小菜鸟本身也只是会一些简单的。

三.在.NET中使用宏的要求(系统和Web.config)

如果要正常操作Word Com组件的话,必须要给用户赋上足够的权限的,以下是我找问高手,和上网上查到的。如果不这样的话,应该不能操作Word,会出现异常

.运行Dcomcnfg.exe
2.组件服务――计算机――我的电脑――DCOM配置――找到microsoft word 文档
.点击属性
.选择安全性
.选定使用自定义访问权限使用自定义启动权限
.分别编辑权限,添加ASPNET,VS Developers,Debugger User
.选择身份标识,在选定交互式用户即可
.在Web.config里加 <identityimpersonate="true"/>
四.一个在.NET使用宏的例子

先引入Microsft Word 11.0 Object Library

private void button2_Click(object sender, System.EventArgs e)
        
{
            
object filename = @"C:"Inetpub"wwwroot"TestWebApp"test.doc";//文件名
            Word.Application a =  new Word.ApplicationClass();//建立一个Word程序对像
            object Nothing = System.Reflection.Missing.Value;//空值
            Word.Document b = a.Documents.Open(ref filename,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);//建立一个Word文档对像            

            
//其实这步就是执行了这个宏
            a.Selection.InlineShapes.AddPicture(@"C:"Documents and Settings"Administrator"桌面"2003121512223366481.jpg",ref Nothing,ref Nothing,ref Nothing);
            
//Selection.InlineShapes.AddPicture FileName:= "C:"Documents and Settings"Administrator"桌面"2003121512223366481.bmp", LinkToFile:=False, SaveWithDocument:=True   End Sub    b.Save();//保存
            b.Close(ref Nothing,ref Nothing,ref Nothing);//关闭Word文档
            a.Quit(ref Nothing,ref Nothing,ref Nothing);//退出Word程序
        }

OK~~~~~~去看一下Word文件中,是不是多了一张图片呀~!!

posted @ 2008-01-22 00:21  小罗  阅读(343)  评论(0)    收藏  举报