2014-06-29 45 views
0

我知道有一個關於它的幾個線程它說簡單地使用如何從word內容中刪除html標籤?

Regex.Replace(input, "<.*?>", String.Empty); 

,但我不能在寫在Word文檔文本中使用它。 我的代碼是這樣的:

Microsoft.Office.Interop.Word.Document wBelge = oWord.Documents.Add(ref oMissing, 
    ref oMissing, ref oMissing, ref oMissing); 
Microsoft.Office.Interop.Word.Paragraph paragraf2; 
paragraf2 = wBelge.Paragraphs.Add(ref oMissing); 
paragraf2.Range.Text ="some long text"; 

我可以查找和替換樣改變

Word.Find findObject = oWord.Selection.Find; 
findObject.ClearFormatting(); 
findObject.Text = "<strong>"; 
findObject.Replacement.Text = ""; 
findObject.Replacement.ClearFormatting();    

object replaceAllc = Word.WdReplace.wdReplaceAll; 
findObject.Execute(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
    ref replaceAllc, ref oMissing, ref oMissing, ref oMissing, ref oMissing); 

我需要爲每一個HTML標記做到這一點?

+0

如果正則表達式失敗(我不明白它爲什麼會失敗,你能說明你是如何使用它的?),你可以創建一個所有標籤的列表,並使用for循環刪除所有標籤。 – Transcendent

+0

@Transcendent,他正在處理一個Word.Document對象,而不是純文本字符串... – elgonzo

+0

@elgonzo:是的,但這些段落可以轉換爲基本字符串,然後正則表達式的作品!我不是嗎? – Transcendent

回答

0

隨着評價提供一些幫助,我意識到其使用的搜索模式\<*\>(含有通配符*,因此findObject.MatchWildcards必須設置爲以下工作溶液

findObject.ClearFormatting(); 
findObject.Text = @"\<*\>"; 
findObject.MatchWildcards=true;      
findObject.Replacement.ClearFormatting(); 
findObject.Replacement.Text = "";      

object replaceAll = Word.WdReplace.wdReplaceAll; 
findObject.Execute(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
    ref replaceAll, ref oMissing, ref oMissing, ref oMissing, ref oMissing); 

)。

0

給一個嘗試以下操作:

轉換與HTML addings文本使用

string unFormatted = paragrapf2.ToString(SaveOptions.DisableFormatting)); 

一個簡單的字符串,然後替換爲格式化字符串paragraf2 contect。

+0

謝謝,但是在我強化標籤之間的文本之後,無論我在paragraf2.range.text中更改它都不會更改word文檔 – ruqo

+0

@ruqo,我想正確理解:您已經格式化了要用無格式文本替換的文本? – user3165438