ITPub博客

首页 > Linux操作系统 > Linux操作系统 > C#操作Word之 根据标签名清除标签内容

C#操作Word之 根据标签名清除标签内容

原创 Linux操作系统 作者:iDotNetSpace 时间:2009-02-05 17:22:55 0 删除 编辑

代码已经测试过了,try catch语句你自己加,记得要保证出错之后能够释放资源

执行此操作后,被操作标签会被去掉

 

Code

        private void button1_Click(object sender, EventArgs e)
        {
            ClearBookMarkFromDoc(@"C:\test.doc","a");
        }


        public void ClearBookMarkFromDoc(string FilePath, string BkName)
        {
            object filepath = FilePath as object;
            object missing = Type.Missing;
            object bjTrue = true as object;

            //创建word.application应用
            Microsoft.Office.Interop.Word.Application myWordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
            Microsoft.Office.Interop.Word.Document myWordDoc = myWordApp.Documents.Open(ref filepath, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing);

            Microsoft.Office.Interop.Word.Range bmRange = default(Microsoft.Office.Interop.Word.Range);

            Microsoft.Office.Interop.Word.Bookmarks bkmrs = myWordDoc.Bookmarks;

            object bkIndex = BkName as object;
            Microsoft.Office.Interop.Word.Bookmark bkm = bkmrs.get_Item(ref bkIndex);

            textBox1.Text = bkm.Name;
            bkm.Range.Text = string.Empty;

            myWordDoc.Save();
            myWordDoc.Close(ref objTrue, ref missing, ref missing);
            myWordApp.Quit(ref objTrue, ref missing, ref missing);
        }

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-545380/,如需转载,请注明出处,否则将追究法律责任。

上一篇: .net2.0无刷新页面
请登录后发表评论 登录
全部评论

注册时间:2008-01-04

  • 博文量
    2376
  • 访问量
    5354942