Hi
I am working on a web application in which I am opening the template file which is in temp.Docx finding tags in it and replacing it with its value.
I have achieved this using "Microsoft.Office.Interop.Word". It is working on my local machine.
But on server MS office is not installed on which this application is going to deploy.
Can you please let me know if I can achivethis task without using "Microsoft.Office.Interop.Word" and third party DLL ?
Following is my working code
public void GenerateWordReport(System.Data.DataTable dt, String sTempPath)
{
Microsoft.Office.Interop.Word.Application objWordApp = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document objDoc = new Microsoft.Office.Interop.Word.Document();
Object missing = System.Reflection.Missing.Value;
StringBuilder wordstring = new StringBuilder();
try
{
objWordApp.Documents.Open(sTempPath);
objDoc = objWordApp.ActiveDocument;
//MatchCollection bodyTagColl = ;
foreach (Match tag in Regex.Matches((objDoc.Content).Text, @"[[][[]\s*\w+\s*.*?\s*[]][]]"))
{
String exp = "Parent_NAME='" + tag.Value.Substring(2, tag.Value.Length - 4).ToString() + "'";
DataRow[] foundRows;
foundRows = dt.Select(exp);
//if (foundRows)
if (foundRows.Length > 0)
{
foreach (DataRow dr in foundRows)
{
String Value = dr["Value"].ToString() + dr["Unit of Measure"].ToString().Trim();
if (("[[" + dr["Parent_NAME"].ToString() + "]]").Length <= 255)
FindnReplace("[[" + dr["Parent_NAME"].ToString() + "]]", Value, objDoc);
else
FindLargeChar("[[" + dr["Parent_NAME"].ToString() + "]]", Value, objDoc);
}
}
else
{
if (("[" + tag.Value).Length <= 255)
FindnReplace(tag.Value, "-", objDoc);
else
FindLargeChar(tag.Value, "-", objDoc);
}
}
objDoc.Save();
}
catch (Exception e)
{
}
finally
{
objWordApp.Documents.Close(Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges);
objWordApp.Quit();
objWordApp = null;
KillProcess("winword");
}
}
public void FindLargeChar(string tofind, string toreplace, Microsoft.Office.Interop.Word.Document objDoc)
{
int StartIndex = 0;
int EndIndex =254;
int length = tofind.Length;
if (tofind.Length > 255)
{
Decimal Dec=tofind.Length%255;
FindnReplace(tofind.Substring(StartIndex, EndIndex), toreplace, objDoc);
}
while (EndIndex <= tofind.Length)
{
length = length - 254;
int l = length;
StartIndex = StartIndex + 254;
EndIndex = EndIndex + 254;
if (length > 255)
{
FindnReplace(tofind.Substring(StartIndex, EndIndex), "", objDoc);
}
else
{
FindnReplace(tofind.Substring(StartIndex, l), "", objDoc);
}
}
}
public void KillProcess(string ProcessName)
{
try
{
//Process[] procs = Process.GetProcessesByName(ProcessName);
//foreach (Process process__1 in Process.GetProcessesByName(ProcessName))
//{
// process__1.Kill();
// process__1.WaitForExit();
//}
}
catch (Exception ex)
{
}
finally
{
}
}
public void FindnReplace(string tofind, string toreplace, Microsoft.Office.Interop.Word.Document objDoc)
{
//If (toreplace = "-999") Then
// toreplace = "None"
//End If
// '' '' '' 'EventLog.WriteEntry("In FindnReplace")
// '' '' '' 'EventLog.WriteEntry("To find " + tofind)
int y = 0;
int x = 0;
// Dim replace As Object = toreplace.Replace("\r\n", Chr(10))
object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;
try
{
if ((toreplace.Length < 200))
{
objDoc.Content.Find.Execute(FindText: tofind, ReplaceWith: toreplace, Replace: Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll, Format: format);
}
else
{
int j = 0;
//objDoc.Content.Find.Execute(FindText:=tofind, _
// ReplaceWith:=toreplace, _
// Replace:=Word.WdReplace.wdReplaceAll)
//If (toreplace.Length() > 250) Then
int i = toreplace.Length;
while (j <= toreplace.Length)
{
string str = toreplace.Substring(x, x + 100);
toreplace = toreplace.Substring(x + 100);
objDoc.Content.Find.Execute(FindText: tofind, ReplaceWith: str + " " + tofind, Replace:Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll, Format: format);
}
}
}
catch (Exception ex)
{
string str = toreplace.Substring(x, x + toreplace.Length);
toreplace = toreplace.Substring(x + toreplace.Length);
objDoc.Content.Find.Execute(FindText: tofind, ReplaceWith: str, Replace: Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll, Format: format);
}
// 'EventLog.WriteEntry("To find " + tofind + " ReplacedSuccessfully")
}
Thanks in adavance...
Regards,
Sampada