如何用c#删除word中两个书签之间的文本

时间:2014-01-29 13:16:50

标签: vba c#-2.0 office-interop bookmarks

我在word文档中有两个书签。 在这些书签之间有一些文字。

现在我希望能够使用c#office interop删除此文本。

我在VBA中有这个工作,但我怎么能在c#

中做到这一点
Dim delRange As Range
Set delRange = ActiveDocument.Range

delRange.Start = delRange.Bookmarks("HTML_SECTION_START").Range.End
delRange.End = delRange.Bookmarks("HTML_SECTION_END").Range.Start
delRange.Delete

2 个答案:

答案 0 :(得分:1)

试试这个:

        _Application app = new Application();
        try
        {
            _Document doc = app.Documents.Open("c:\\xxxx\\doc.doc");
            try
            {
                Range delRange = doc.Range();
                delRange.Start = doc.Bookmarks.get_Item("HTML_SECTION_START").Range.End;
                delRange.End = delRange.Bookmarks.get_Item("HTML_SECTION_END").Range.Start;
                delRange.Delete();
                doc.Save();
            }
            finally
            {
                doc.Close();
            }
        }
        finally
        {
            app.Quit();
        }

您可以使用Bookmark.Exists保护书签get_Item

编辑:您应该保存并关闭文档和应用程序

答案 1 :(得分:0)

Oki,所以我现在就开始工作了,感谢Qsebas在上一次编辑方面的突破。我正在使用带有.net framework 2.0的vs2005,所以我不得不修改它,所以我给你Qsebas的信用。

如果有人有兴趣,这就是我最终的结果。

using Word = Microsoft.Office.Interop.Word;

public class user
{
    public string Convert(string input, string output)
    {
        object oMissing = System.Reflection.Missing.Value;
        object readOnly = false;
        object oInput = input;
        object oOutput = output;
        object oFormat = Word.WdSaveFormat.wdFormatFilteredHTML;

        object html_start = "HTML_SECTION_START";
        object html_end = "HTML_SECTION_END";
        object move = -1;
        object charUnit = Word.WdUnits.wdCharacter;


        Word._Application app = new Word.Application();
        try
        {
            Word._Document doc = app.Documents.Open(ref oInput, ref oMissing, ref readOnly, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref isVisible, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

            try
            {
                Word.Range dRange = doc.Range(ref oMissing, ref oMissing);
                dRange.Start = doc.Bookmarks.get_Item(ref html_start).Range.End;
                dRange.End = doc.Bookmarks.get_Item(ref html_end).Range.Start;
                dRange.Delete(ref charUnit, ref move);
                doc.Save();

                app.Quit(ref oMissing, ref oMissing, ref oMissing);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(app);

                return "";
            }
            catch (Exception e)
            {
                app.Quit(ref oMissing, ref oMissing, ref oMissing);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(app);

                return e.ToString();
            }
        }
        catch (Exception e)
        {
            app.Quit(ref oMissing, ref oMissing, ref oMissing);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(app);

            return e.ToString();
        }
    }
}