Hi! Why its not worked ?
How to save word file after modify and run it
File cashdocrep.docx contain:
Pay:#Pr
Number:#n
Name:#nm
Birthday:#brth
object fileName = @"D:\cashdocrep.docx";
Word.Application word = new Word.Application();
Word.Document doc = new Word.Document();
object missing = System.Type.Missing;
try
{
doc = word.Documents.Open(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
doc.Activate();
foreach (Microsoft.Office.Interop.Word.Range docRange in doc.Words)
{
if (docRange.Text.Trim() == "#")
{
docRange.Text = docRange.Text.Replace("#", "");
}
else if (docRange.Text.Trim() == "Pr")
{
docRange.Text = docRange.Text.Replace("Pr", "5.0");
}
else if (docRange.Text.Trim() == "n")
{
docRange.Text = docRange.Text.Replace("n", "0001");
}
else if (docRange.Text.Trim() == "nm")
{
docRange.Text = docRange.Text.Replace("nm", "Rustam");
}
else if (docRange.Text.Trim() == "brth")
{
docRange.Text = docRange.Text.Replace("brth", "22.12.1987");
}
}
string filePath = @"C:\cashdirection.docx";
if (File.Exists(filePath))
{
File.Delete(filePath);
}
doc.SaveAs(filePath , missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
Process.Start("WINWORD.EXE", "\"" + filePath + "\"");
}
catch { }
finally
{
doc.Close(ref missing, ref missing, ref missing);
((Word._Application)word).Quit();
}
Output file cashdirection.docx contain:
Pay:5.0
Number:0001
Name:Rustam
Birthday:22.12.1987