在word文档中查找文本并将其替换为表格

时间:2010-11-04 01:15:34

标签: c# ms-word ms-office

我正在尝试在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++;
            }
        }
    }
}

我做错了什么?

1 个答案:

答案 0 :(得分:5)

你在这里遇到很多问题,所以我建议你一步完成一步。

1)你必须找到你要替换的文本。由于您要用表替换它,您实际上无法使用find对象的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)接下来,你在一个字符串中混淆了Offsets,在文档中有偏移,就像在这段代码中一样

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

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

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

这里有关于FIND和REPLACE的非常好的文章。

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

我会在FIND中编写一个例程并选择你之后的文本。确保它适用于所有情况,然后,使用找到的文本范围,使用表替换它。 一旦你找到了你想要的文本

相关问题