C#操作word文档

       这里给大家介绍下如何运用VBA来操作word文档内容,打开关闭word文档。

一、什么是VBA

        Visual Basic for Applications(VBA)是Visual Basic的一种语言,是微软开发出来在其桌面应用程序中执行通用的自动化(OLE)任务的编程语言。主要能用来扩展Windows的应用程式功能,特别是Microsoft Office软件。也可说是一种应用程式视觉化的Basic 脚本。最近要做一个计算机一级考试系统,需要进行对操作题进行判分。

二、C#打开word操作

            //定义word对象
            Word.Application W = new Word.Application();
            Word.Document D = new Word.Document();
 
            //找到文件
            Object filename = "1.docx";
            Object filefullname = @"F:\work\计算机一级考试系统\1\1.docx";

            Object confirmConversions = Type.Missing;
            Object readOnly = Type.Missing;
            Object addToRecentFiles = Type.Missing;
            Object passwordDocument = Type.Missing;
            Object passwordTemplate = Type.Missing;
            Object revert = Type.Missing;
            Object writePasswordDocument = Type.Missing;
            Object writePasswordTemplate = Type.Missing;
            Object format = Type.Missing;
            Object encoding = Type.Missing;
            Object visible = Type.Missing;
            Object openConflictDocument = Type.Missing;
            Object openAndRepair = Type.Missing;
            Object documentDirection = Type.Missing;
            Object noEncodingDialog = Type.Missing;

            for (int i = 1; i <= W.Documents.Count; i++)
            {
                String str = W.Documents[i].FullName.ToString();
                if (str == filefullname.ToString())
                {
                    MessageBox.Show("请勿重复打开该文档");
                    return;
                }
            }
            try
            {
                W.Documents.Open(ref filefullname);
                W.Visible = true;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("打开Word文档出错");
            }
通过以上过程用C#将word类型的文档打开,以上就是全部操作。

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值