Quantcast
Channel: Web Forms
Viewing all articles
Browse latest Browse all 23244

about replacement in a word document using Interop.Word.dll

$
0
0

Hi. I use Interop.Word.dll for reading in a web project. I try to make replacement process. I made this for words. Now I need to change a word with a table.

For example I have a keyword named ##table1##.

When I see this keyword on documnet, I replace it with a ready table. I try to make this. Can somenone give me an idea? Thanks. I tried a code lkie below:

doc.ActiveWindow.Selection.WholeStory();
doc.ActiveWindow.Selection.Copy();
bool isexist = false;
foreach (Microsoft.Office.Interop.Word.Range tmpRange in doc.StoryRanges)
{
string[] vals = ReturnReplacementValues(item);

object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
tmpRange.Application.Selection.Find.ClearFormatting();
tmpRange.Application.Selection.Find.Text = (string)findme;

int indexx = tmpRange.Text.IndexOf(findme.ToString());

int lastindexx = indexx + findme.ToString().Length;

Microsoft.Office.Interop.Word.Range rng = doc.Application.ActiveDocument.Range(indexx, lastindexx);
string asdf = rng.Text;
rng.Select();

if (indexx != -1)
{
isexist = true;
}
else
isexist = false;
object start = indexx;
object end = indexx + findme.ToString().Length;
doc.Application.Selection.SetRange((int)start,(int)end);

tmpRange.Application.Selection.Find.Replacement.ClearFormatting();


if (isexist)
{
List<User> users = GetDummyUserData();
int noOfRows = users.Count + 1;
int noOfColumns = 5;
//Microsoft.Office.Interop.Word.Table table = doc.Tables.Add(tmpRange.Application.Selection.Range(ref start, nullobj), noOfRows, noOfColumns, ref nullobj, ref nullobj);
Microsoft.Office.Interop.Word.Table table = doc.Tables.Add(doc.Range(ref start, nullobj), noOfRows, noOfColumns, ref nullobj, ref nullobj);
AddColumnHeaders(ref table);
for (int i = 1; i <= users.Count; i++)
{
table.Rows[i + 1].Cells[1].Range.Text = users[i - 1].FirstName;
table.Rows[i + 1].Cells[2].Range.Text = users[i - 1].LastName;
table.Rows[i + 1].Cells[3].Range.Text = users[i - 1].Email;
table.Rows[i + 1].Cells[4].Range.Text = users[i - 1].Address;
table.Rows[i + 1].Cells[5].Range.Text = users[i - 1].Pincode.ToString();
}
}

doc.Save();
}


Viewing all articles
Browse latest Browse all 23244

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>