c# -用退格替换word文档字符串

本文关键字:word 文档 字符串 替换 | 更新日期: 2023-09-27 18:11:01

我试图用其他字符串替换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);
}

c# -用退格替换word文档字符串

过了一会儿我就明白了。"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);
}

我希望我能帮助到每一个有同样问题的人。