简体   繁体   中英

C# - Replace word document string with backspace

I'm trying to replace some strings in a word document with other strings. I created a method to do that.

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]));
}

And in my button_Click event I got this:

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();

The replacement works perfectly. It does what I want it to do.

For the placeholder ***test*** it should insert a backspace so that the line will be deleted. The problem is that \\b doesn't put a backspace in this line. The line stays empty but it should disappear. I really get stuck on this problem.

Any suggestions?


UPDATE:

My code looks now like this:

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

After a while I figured it out. The way "CodeCaster" told me was working but not perfectly. I now use Microsoft.Office.Interop.Word.Range . See this link: http://msdn.microsoft.com/en-us/library/2a9dt54a.aspx

I removed the method DeleteCurrentSelectionLine() and added the following to my ReplaceAll() method:

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

I hope I could help everyone who'll have the same problem :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM