2017-06-21 37 views
1

如何刪除單詞中的說明文字?如何刪除單詞中的說明文字?

我從早上起來。請任何人幫助我。這是我的代碼。

Microsoft.Office.Interop.Word.Application app = Globals.ThisAddIn.Application; 

Microsoft.Office.Interop.Word.Document doc1 = app.ActiveDocument; 
Selection wordSelection = Globals.ThisAddIn.Application.Selection; 
wordSelection.HomeKey(WdUnits.wdStory); 

doc1.Content.Find.ClearFormatting(); 
doc1.Content.Find.Replacement.ClearFormatting(); 
doc1.Content.Find.set_Style(doc1.Styles["Instructions"]); 
doc1.Content.Find.Text = ""; 
doc1.Content.Find.Replacement.Text = ""; 
doc1.Content.Find.Forward = true; 

bInstructions = doc1.Content.Find.Execute(WdReplace.wdReplaceAll); 
+1

嘗試用'doc1.Content.Find.Text =「*」;' –

+0

替換'doc1.Content.Find.Text =「」;'確實發生了什麼 - 是否拋出異常 - 如果是的話在哪裏以及什麼是例外? – PaulF

+1

@ S.Petrosov:爲了工作Find.MatchWildcards需要設置。 – PaulF

回答

1

嘗試使用基於選擇對象的查找對象:

Microsoft.Office.Interop.Word.Selection wordSelection = app.ActiveWindow.Selection; 
    wordSelection.HomeKey(WdUnits.wdStory); 
    Find fnd = wordSelection.Find; 

    fnd.ClearFormatting(); 
    fnd.Replacement.ClearFormatting(); 
    fnd.Forward = true; 
    fnd.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue; 
    fnd.Text = ""; 
    fnd.Replacement.Text = ""; 
    fnd.set_Style("Instructions"); 
    fnd.Execute(Replace: WdReplace.wdReplaceAll); 

注意,對於執行命令的簽名是:

bool Execute(
ref Object FindText, ref Object MatchCase, ref Object MatchWholeWord, 
ref Object MatchWildcards, ref Object MatchSoundsLike, ref Object MatchAllWordForms, ref Object Forward, ref Object Wrap, ref Object Format, 
ref Object ReplaceWith, ref Object Replace, ref Object MatchKashida, 
ref Object MatchDiacritics, ref Object MatchAlefHamza, ref Object MatchControl) 

缺失時爲每個默認值 - 在你的代碼中,你傳遞WdReplace.wdReplaceAll作爲FindText參數,而不是傳遞多個Missing參數,我的代碼使用named參數選項。即使使用named參數,您的代碼也不會進行替換,因此我使用了當前文檔的選擇。