2010-11-04 82 views
2

我正在嘗試搜索某個特定文本的Word文檔,然後將其替換爲自定義表格。我似乎幾乎有它的工作,但它似乎增加了前一個詞的一半,而不是在它找到文本的位置。在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++; 
      } 
     } 
    } 
} 

我在做什麼錯?

回答

5

你在這裏有很多問題,所以我建議一次完成1步。

1)你必須找到你正在替換的文本。既然你用一個表來代替它,你真的不能使用查找對象的REPLACEALL選項,所以擺脫這一切。將文檔的CONTENT範圍轉換爲Range變量,從中找到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找到你需要的東西,然後用範圍來操作文本。

這裏有一篇關於FIND和REPLACE的真正優秀的文章。

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

我會寫一個程序來查找並選擇後首先是文本。確保在所有情況下都能正常工作,那麼使用找到的文本範圍,使用表格替換它。 一旦你找到你想要的文字

相關問題