2014-07-01 49 views
0

我想用其他字符串替換word文檔中的某些字符串。我創建了一個方法來做到這一點。C# - 用退格替換word文檔字符串

public static void ReplaceAll(
    word.Document doc, string searchText, string replaceText) 
{ 
    object missing = Missing.Value; 
    word.Find obj = doc.Application.Selection.Find; 
    obj.Text = searchText; 
    obj.Replacement.Text = replaceText; 

    object replaceAll = word.WdReplace.wdReplaceAll; 

    obj.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); 
} 

public static void tagReplace(word.Document doc, Dictionary<string, string> tags) 
{ 
    tags.Keys.ToList().ForEach(key => ReplaceAll(doc, key, tags[key])); 
} 

在我button_Click事件中,我得到這個:

Dictionary<string, string> tags = new Dictionary<string, string>(); 

tags.Add("***company***", "microsoft"); 
tags.Add("***name***", "bill gates"); 
tags.Add("***test***", "\b"); 

tagReplace(doc, tags); 
doc.SaveAs2(path + "testdoc.docx"); 
doc.Close(); 
app.Quit(); 

更換完美的作品。它做我想要它做的事。

對於佔位符***test***它應該插入一個退格符,以便該行將被刪除。問題是\b不會在此行中放置退格。該行保持空白,但應該消失。我真的被困在這個問題上。

有什麼建議嗎?


UPDATE:

我的代碼現在看起來像這樣:

private void button1_Click(object sender, EventArgs e) 
{ 
    Dictionary<string, string> tags = new Dictionary<string, string>(); 

    tags.Add("***company***", "microsoft"); 
    tags.Add("***name***", "bill gates"); 
    tags.Add("***test***", @"\b\b"); 

    tagReplace(doc, tags); 
    doc.SaveAs2(path + "testdoc.docx"); 
    doc.Close(); 
    app.Quit(); 
} 

public static void ReplaceAll(word.Document doc, string searchText, string replaceText, word.Application app) 
{ 
    if (replaceText == @"\b\b") 
    { 
     DeleteCurrentSelectionLine(app); 
    } 

    else 
    { 
     object missing = Missing.Value; 
     word.Find obj = doc.Application.Selection.Find; 
     obj.Text = searchText; 
     obj.Replacement.Text = replaceText; 

     object replaceAll = word.WdReplace.wdReplaceAll; 

     obj.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); 
    } 
} 

public static void tagReplace(word.Document doc, Dictionary<string, string> tags, word.Application app) 
{ 
    tags.Keys.ToList().ForEach(key => ReplaceAll(doc, key, tags[key], app)); 
} 

public static void DeleteCurrentSelectionLine(word.Application application) 
{ 
    object wdLine = word.WdUnits.wdLine; 
    object wdCharacter = word.WdUnits.wdCharacter; 
    object wdExtend = word.WdMovementType.wdExtend; 
    object count = 1; 

    word.Selection selection = application.Selection; 
    selection.HomeKey(ref wdLine, ref missing); 
    selection.MoveDown(ref wdLine, ref count, ref wdExtend); 
    selection.Delete(ref wdCharacter, ref missing); 
} 
+1

參見[我怎樣才能以編程方式從使用c#的Word文檔中刪除一行?](http://stackoverflow.com/questions/1838564/how-can-i-programmatically-delete-a-line-from-a-word-document-使用-C)。嘗試使用兩個退格('\ b \ b'),或者從接受的答案中複製方法,當您的代碼遇到雙退格作爲替換值時您可以執行該方法。 – CodeCaster

+0

謝謝。但是,我應該如何用這種方法替換'*** test ***'佔位符?我不能使用'tags.Add(「*** test ***,theMethod())',因爲我必須用字符串替換它... – Tyler

+1

您可以添加'@」\ b \ b「'而不是'當遇到'\ b \ b'(如果'\ b \ b'本身不起作用!)時,theMethod()'並在'ReplaceAll()'中調用'theMethod()'。 – CodeCaster

回答

0

一段時間,我想通了之後。 「CodeCaster」告訴我的方式正在工作,但並不完美。我現在使用Microsoft.Office.Interop.Word.Range。請參閱此鏈接:http://msdn.microsoft.com/en-us/library/2a9dt54a.aspx

我刪除的方法DeleteCurrentSelectionLine()並添加以下到我的ReplaceAll()方法:

if (replaceText == @"\b\b") 
{ 
    object start = doc.Sentences[2].Start; 
    object end = doc.Sentences[2].End; 

    word.Range rng = app.ActiveDocument.Range(start, end); 
    rng.Delete(ref missing, ref missing); 
} 

我希望我能幫助大家誰就會有同樣的問題:)