2012-11-20 64 views
2

我正在做一個單詞關係應用程序,現在我有一個位置的書籤,我怎樣才能得到當前段落並刪除它?謝謝。代碼段如下。如何獲取當前單詞的段落並將其刪除?

/// <summary> 
/// Word Application class 
/// </summary> 
public class WordApplication 
{ 
    object nullobj=Missing.Value; 
    _Application app; 
    _Document doc; 

    public WordApplication() 
    { 
     app=new ApplicationClass(); 
     Config config = new Config(); 
     object file = config.InputFilePath; 
     try { 
      doc=app.Documents.Open(ref file,ref nullobj,ref nullobj, 
       ref nullobj,ref nullobj,ref nullobj, 
       ref nullobj,ref nullobj,ref nullobj, 
       ref nullobj,ref nullobj,ref nullobj, 
       ref nullobj,ref nullobj,ref nullobj,ref nullobj) as _Document; 
      doc.Activate(); 
     } catch(Exception ex) { 
      Logger.Log(ex); 
      throw ex; 
     } 
    } 
    /// <summary> 
    /// Remove current paragraph 
    /// </summary> 
    /// <param name="bookmark"></param> 
    public void RemoveParaghRange(string bookmark) 
    { 
     foreach (Bookmark bm in doc.Bookmarks) { 
      if (bm.Name.Equals(bookmark)) { 
       //TODO:That's the point.How can I do it? 
      } 
     } 
    } 
} 

問題是這樣的:我怎樣才能得到當前段落,然後我可以刪除這段文字。然後

bm.Select(); 

,您可以使用選擇對象與書籤所引用的paragraphs工作:

+2

[你有什麼嘗試](http://whathaveyoutried.com)? – ryadavilli

+0

您的數據結構如何?文本只是一個長字符串,而您的書籤是這個字符串中一個poisition的索引?還是有更復雜的事情呢? – AHM

+0

我已更新我的代碼,請參閱。謝謝。 – WangHongjian

回答

0

您可以將當前選擇移動到書籤與Bookmark.Select

var paragraphs = app.Selection.Paragraphs; 
+0

謝謝,然後,我該如何刪除這一段? – WangHongjian

+0

在VBA中,它是'myParagraph.Range.Delete()',互操作庫可能包含一個類似的方法。 – Heinzi

+0

object oBookmark = bookmark; doc.Bookmarks.get_Item(ref oBookmark).Select(); 物體單位; unit = WdUnits.wdParagraph; object oExtend; oExtend = WdMovementType.wdExtend; int start = app.Selection.MoveUp(ref unit,ref nullobj,ref nullobj); int end = app.Selection.MoveDown(ref unit,ref nullobj,ref oExtend); object oStart = start; object oEnd = end; 範圍範圍= doc.Range(ref oStart,ref oEnd); unit = WdUnits.wdCharacter; range.Delete(ref unit,ref nullobj); – WangHongjian