문제

I have two bookmarks in a word document. in between these bookmarks there is some text.

Now i want to be able to delete this text by using c# office interop.

I have got this working in VBA, but how can i do this in 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
도움이 되었습니까?

해결책

Try this:

        _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();
        }

You can protect the Bookmark get_Item with Bookmark.Exists

Edit: You should save and close the document and the application

다른 팁

Oki, so i got it working now, thanks to Qsebas for the breakthrough with his last edit. I'm using vs2005 with .net framework 2.0, so i had to modify it a bit, so i'm giving the credits to you Qsebas.

If anyone is interested, this is what i ended up with.

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 ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top