繁体   English   中英

如何使用C#删除Word中两个书签之间的文本

[英]How to delete text between two bookmarks in word with c#

我在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

尝试这个:

        _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保护Bookmark get_Item。

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

Oki,所以我现在开始工作了,这要感谢Qsebas在上次编辑中取得的突破。 我将vs2005与.net framework 2.0一起使用,因此我不得不对其进行一些修改,因此,我将功劳归于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();
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM