Find text in a word document and replace it with a table
I am trying to search a word document for some specific text and then replace it with a custom table. I seem to almost have it working but it seems to add the table halfway through the previous word rather than right where it found the text.
This is my function
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开发者_StackOverflow社区.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++;
}
}
}
}
What am I doing wrong?
You've got lots of problems here, so I'd recommend working through 1 step at a time.
1) you've got to FIND the text you're replacing. Since you're replacing it with a table, you really can't use the REPLACEALL option of the find object, so get rid of all that. Get the CONTENT range of the document into a Range variable, the FIND from that, and then execute your find. The range that you got the FIND from will be reset to point to the specific text found (if any) and THEN you can manipulate that range.
For instance
Set myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="blue", Forward:=True
If myRange.Find.Found = True Then myRange.Bold = True
2) Next, you're confusing Offsets within a string, with offsets within a document, as in this code
int FirstChr = srng.Text.IndexOf(find);
if (FirstChr != -1)
{
Word.Range ts = Application.ActiveDocument.Range(FirstChr,FirstChr);
That will sometimes, but only rarely, work. It depends on lots of stuff within Word. Better to find what you need using find and then manipulate text only with ranges.
There's a really good article here about FIND and REPLACE in word.
http://msdn.microsoft.com/en-us/library/aa211953%28office.11%29.aspx
I'd write a routine to FIND and SELECT the text you're after first. Make sure that works in all cases, THEN, using the range of the found text, work with replacing it with a table. once you've found the text you're after
精彩评论