开发者

Word Interop Delete results in Wrong Parameter

I have the pleasure to write some code that moves around stuff in an Office XP environment. I've referenced the OfficeXP Interop Assemblies and written code to Search/Replace stuff. That wor开发者_运维技巧ks fine. Now I need to delete Text around a Bookmark and i keep getting Exceptions thrown at me.

Here is some of the code:

            object units = WdUnits.wdLine;
            object lines = 2;
            object extend = WdMovementType.wdExtend;

            object bookmarkName = "Bank1";
            var bm = doc.Bookmarks;
            var bm1 = doc.Bookmarks.get_Item(bookmarkName);
            var ra = bm1.Range;

            ra.Delete(ref units, ref lines);

The last line is where i get a "Wrong Parameter" Exception. Looking at the Definition in the MSDN I kind of think I'm right. But obviously I'm not. Hope you guys can help me out here.

Update: ok, i see. Using the Delete method on the Range object i can only use wdWord as a Parameter. I'd like to change my question now: what i do want to do is delete two lines starting from the bookmark. How would i do this?


Range objects in Word are not "line oriented", they don't allow line operations, only paragraph operations. However, selections allow line operations. The current selection is not a property of the word document, but of the word application object. Here is some VBA code which does essentially what you try, I think you can easily port this to C#:

Dim rng As Range
Dim doc As Document
Set doc = ActiveDocument
Set rng = doc.Bookmarks("BM").Range

Dim s As Long, e As Long
rng.Select
s = Application.Selection.Start
e = Application.Selection.Next(wdLine, 1).End

Application.Selection.SetRange s, e
Application.Selection.Delete  


Ok, i found a way to do what i had to do. Here is the Code:

            if (doc.Bookmarks.Exists("Bank1"))
            {
                object bookmarkName = "Bank1";
                object units = WdUnits.wdLine;
                object lines = 2;
                object extend = WdMovementType.wdExtend;
                doc.Bookmarks.get_Item(bookmarkName).Select();

                app.Selection.MoveDown(units, lines, extend);
                app.Selection.Delete();
            }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜