2014-11-05 60 views
3

我正在嘗試創建Word插件。我如何以編程方式替換段落中的文字?我需要替換:\r\t\r以編程方式替換段落中的格式化文本(鏈接,內容)

第一次嘗試:

Globals.ThisAddIn.Application.ActiveDocument.Paragraphs[nr].Range.Text = 
    Globals.ThisAddIn.Application.ActiveDocument 
    .Paragraphs[nr].Range.Text.Replace("\r", "\t\r"); 

其良好的只有文字,但如果我有段鏈接或內容及其不起作用。

第二次嘗試:

Globals.ThisAddIn.Application.ActiveDocument.Paragraphs[nr].Range.Find.Execute("\r"); 
Globals.ThisAddIn.Application.Selection.Text = "\t" 

這種失敗像1

第三次嘗試:

Globals.ThisAddIn.Application.Selection.Find 
    .Execute("\r", Wrap: Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue); 

Word.Find findObject = Application.Selection.Find; 
// findObject.ClearFormatting(); 
findObject.Text = "\r"; 
// findObject.Replacement.ClearFormatting(); 
findObject.Replacement.Text = "\t\r" 

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

這種失敗多了一個,因爲刪除1項(用於將文本從第[1]第[2]段至第一段)

+0

花點時間閱讀幫助中心的[編輯幫助](http://stackoverflow.com/editing-help)。堆棧溢出的格式與其他站點不同。您的帖子看起來越好,用戶就越容易幫助您。 – gunr2171 2014-11-05 16:02:37

+0

你能詳細說明你的代碼「不工作」或「失敗」嗎?你在期待什麼,究竟發生了什麼?如果您遇到異常,請發佈它發生的行和異常詳細信息。 – gunr2171 2014-11-05 16:06:27

回答

0

對於未來的讀者:

可以使用OpenXml。 OpenXml can be extracted來自於插件的代碼。這種方法是通用的,它允許更快,更精確地處理文檔。

相關問題