C# - 用backspace替换word文档字符串

时间:2014-07-01 11:45:38

标签: c# string

我试图用其他字符串替换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没有在此行中放置退格。该行保持空白但它应该消失。我真的陷入了这个问题。

有什么建议吗?


更新:

我的代码现在看起来像这样:

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 个答案:

答案 0 :(得分: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);
}

我希望我能帮助每个遇到同样问题的人:)