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

Vba 如何用c语言删除word中两个书签之间的文本#,vba,c#-2.0,office-interop,bookmarks,Vba,C# 2.0,Office Interop,Bookmarks,我在word文档中有两个书签。 在这些书签之间有一些文本 现在,我希望能够使用c#office互操作删除此文本 我已经在VBA中实现了这一点,但是如何在c中实现这一点呢# 试试这个: _Application app = new Application(); try { _Document doc = app.Documents.Open("c:\\xxxx\\doc.doc"); try

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

现在,我希望能够使用c#office互操作删除此文本

我已经在VBA中实现了这一点,但是如何在c中实现这一点呢#

试试这个:

        _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\u项目


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

Oki,所以我现在可以工作了,多亏Qsebas上次编辑的突破。我将vs2005与.NETFramework2.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();
        }
    }
}

很抱歉,我知道你在使用VBNetthanks,这看起来可能有用。让我试试。我添加了一些注释,你能检查一下吗,谢谢。没有任何VBA到C的转换。仅VB.NET到C#
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();
        }
    }
}