C#操作Word文档(加密、解密、对应书签插入分页符)

本文转载自http://www.cnblogs.com/wucan/p/3806401.html

写代码之前,需要引用对应的DLL文件:

1、Interop.Microsoft.Office.Interop.Word.dll (网上可以下载)

2、mscorlib.dll (添加引用—>.NET中即可找到)

using Microsoft.Office.Interop.Word;
using MSWord = Microsoft.Office.Interop.Word;
using System.Reflection;

private void button1_Click(object sender, System.EventArgs e)
 {
            //Word文档保护密码
            string Pass = "ITIS@997168";
            object PassWord = Pass;
            MSWord.Application wordApp;  //Word应用程序变量
            MSWord.Document wordDoc;    //Word文档变量
            try
            {
                object Nothing = Missing.Value;  //初始化
                wordApp = new MSWord.ApplicationClass();

                // 打开已存在的Word
                object FileName = @"E:\archive\CMPLatest_2117_230614-1053.Rtf";
                object readOnly = false;
                object isVisible = true;
                object objFalse = false;

                wordDoc = wordApp.Documents.Open(ref FileName, ref Nothing, ref readOnly, ref Nothing, ref PassWord, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref isVisible, ref Nothing, ref Nothing, ref Nothing, ref Nothing);

                //激活Word文档
                wordDoc.Activate();
                //判断是否有密码
                if (wordDoc.HasPassword)
                {
                    wordDoc.Password = null;
                }

                //检查是否为Word文档设置保护功能,没有设置保护功能,就解除密码保护
                if (wordDoc.ProtectionType != WdProtectionType.wdNoProtection)
                {
                    wordDoc.Unprotect(ref PassWord);
                }

                //跳转到指定书签
                object toMark = MSWord.WdGoToItem.wdGoToBookmark;
                //分页符
                object oPageBreak = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;  

                //定义书签名称  PartB
                object BookMarkName_b = "bmf_b";
                wordDoc.ActiveWindow.Selection.GoTo(ref toMark, ref Nothing, ref Nothing, ref BookMarkName_b);
                //插入分页符
                wordDoc.ActiveWindow.Selection.InsertBreak(ref oPageBreak);

                //定义书签名称  PartC1
                object BookMarkName_c1 = "bmf_c1";
                wordDoc.ActiveWindow.Selection.GoTo(ref toMark, ref Nothing, ref Nothing, ref BookMarkName_c1);
                //插入分页符
                wordDoc.ActiveWindow.Selection.InsertBreak(ref oPageBreak);

                //定义书签名称  PartC2
                object BookMarkName_c2 = "bmf_c2";
                wordDoc.ActiveWindow.Selection.GoTo(ref toMark, ref Nothing, ref Nothing, ref BookMarkName_c2);
                //插入分页符
                wordDoc.ActiveWindow.Selection.InsertBreak(ref oPageBreak);

                //对Word文档进行加密保护
                if(PassWord.ToString() != null)
                {
                    wordDoc.Protect(WdProtectionType.wdAllowOnlyReading, ref objFalse, ref PassWord, ref Nothing, ref Nothing);
                }


                //将插入分页符后的Word文档保存一下
                wordDoc.SaveAs(ref FileName, ref Nothing, ref Nothing, ref Nothing, ref objFalse, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref isVisible, ref Nothing, ref Nothing, ref Nothing, ref Nothing);

                //标记为最终状态,禁止弹出对话框
                //wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
                //标记为最终状态
                //wordDoc.Final = true;

                //关闭Word文档
                wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
                wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
            }
            catch(Exception ex)
            {

            }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值