【问题标题】:Find text in a word document and replace it with a table在 word 文档中查找文本并将其替换为表格
【发布时间】:2013-07-26 03:13:05
【问题描述】:

我正在尝试在 Word 文档中搜索某些特定文本,然后将其替换为自定义表格。我似乎几乎可以使用它,但它似乎在前一个单词的中间添加了表格,而不是在它找到文本的位置。

这是我的功能

public void AddTableAtCursor(string tabledata,
                             string find,
                             Boolean flh = true,
                             string name = "Table")
{
    object replaceAll = Word.WdReplace.wdReplaceAll;

    Word.Range srng = Application.ActiveDocument.Content;
    srng.WholeStory();

    srng.Find.ClearFormatting();
    srng.Find.Text = find;

    srng.Find.Replacement.ClearFormatting();
    srng.Find.Replacement.Text = "";

    int FirstChr = srng.Text.IndexOf(find);

    if (FirstChr != -1)
    {
        Word.Range ts = 
            Application.ActiveDocument.Range(FirstChr, FirstChr);

        this.Application.Selection.TypeParagraph();

        srng.Find.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);

        string[] rows = tabledata.Split('|');
        string[] c = rows[0].Split('^');
        rows[0] = null;

        object styleName = "Light List - Accent 1";
        Word.Table tbl = null;

        Word.Range currentSelection = srng;

        int hclen = c.Length;
        if (TomData.IndPrices != "on") { hclen = hclen - 2; }

        tbl = Application.ActiveDocument.Content.Tables.Add(
            ts, rows.Length + 1, hclen);
        tbl.set_Style(ref styleName);
        tbl.PreferredWidth = 90;
        tbl.PreferredWidthType =
            Word.WdPreferredWidthType.wdPreferredWidthPercent;

        // First Row, Put the name into the first cell

        for (int i = 1; i <= hclen; i++)
        {
            if (i == 1)
            {
                tbl.Cell(1, i).Range.InsertBefore(name);
            }
            else
            {
                tbl.Cell(1, i).Range.InsertBefore("");
            }
        }

        // 2nd row, put the headers in
        for (int i = 1; i <= hclen; i++)
        {
            int t = i - 1;
            tbl.Cell(2, i).Range.InsertBefore(c[t]);
        }

        int tblrow = 3;

        // after that just put the data
        for (int rc = 0; rc < rows.Length; rc++)
        {
            if (rows[rc] != null)
            {
                string[] coldata = rows[rc].Split('^');
                int tblcol = 1;
                int clen = coldata.Length;
                if (TomData.IndPrices != "on") { clen = clen - 2; }
                for (int nc = 0; nc < clen; nc++)
                {
                    tbl.Cell(tblrow, tblcol).Range.InsertBefore(
                        coldata[nc]);
                    tblcol++;
                }
                tblrow++;
            }
        }
    }
}

我做错了什么?

【问题讨论】:

    标签: c# ms-word ms-office


    【解决方案1】:

    这里有很多问题,所以我建议一次完成一个步骤。

    1) 您必须找到要替换的文本。由于您要用表格替换它,因此您确实不能使用查找对象的 REPLACEALL 选项,因此请摆脱所有这些。将文档的 CONTENT 范围获取到 Range 变量中,从中查找 FIND,然后执行查找。您从中获得 FIND 的范围将被重置为指向找到的特定文本(如果有),然后您可以操纵该范围。

    例如

    Set myRange = ActiveDocument.Content
    myRange.Find.Execute FindText:="blue", Forward:=True
    If myRange.Find.Found = True Then myRange.Bold = True
    

    2) 接下来,您将字符串中的偏移量与文档中的偏移量混淆,如这段代码所示

        int FirstChr = srng.Text.IndexOf(find);
    
        if (FirstChr != -1)
        {
            Word.Range ts = Application.ActiveDocument.Range(FirstChr,FirstChr);
    

    这有时会奏效,但很少会奏效。这取决于 Word 中的很多内容。最好使用 find 找到您需要的内容,然后仅使用范围操作文本。

    这里有一篇关于查找和替换的非常好的文章。

    http://msdn.microsoft.com/en-us/library/aa211953%28office.11%29.aspx

    我会编写一个例程来查找和选择您首先要查找的文本。确保在所有情况下都有效,然后使用找到的文本范围,用表格替换它。 一旦你找到了你所追求的文字

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-06
      • 2013-09-07
      • 2021-10-13
      • 2011-03-22
      • 2017-11-07
      • 2021-08-11
      • 1970-01-01
      相关资源
      最近更新 更多