How do I replace text inside MS-Word textbox?

  • Thread starter Thread starter james513
  • Start date Start date
J

james513

Guest
I wrote a Windows Forms program using C#. It contains textboxes and a data-grid.

I want to export the data into a Word file to use the template. I added the library of Microsoft Word into my code.

Also I created textboxes "shapes" inside the Word file and putted inside them text to be find from my c# program

For now everything seems okay, But I'm stuck at the C# side, I can replace the normal text, but not the text inside textboxes "Inside the word file"

And here is my code:

private void FindAndReplace(Word.Application wordapp, object ToFindText, object replaceWithText)

{
object matchCase = true;
object matchWholeWord = true;
object matchWildCards = false;
object matchSoundLike = false;
object matchAllforms = false;
object forward = true;
object format = false;
object matchKashida = false;
object matchDiactitics = false;
object matchAlefHamza = false;
object matchControl = false;
object matchread_only = false;
object visible = true;
object replace = 2;
object wrap = 1;

wordapp.Selection.Find.Execute(ref ToFindText,
ref matchCase, ref matchWholeWord,
ref matchWildCards, ref matchSoundLike,
ref matchAllforms, ref forward,
ref wrap, ref format, ref replaceWithText,
ref replace, ref matchKashida,
ref matchDiactitics, ref matchAlefHamza,
ref matchControl);
}


private void CreateWordDocument(object filename, object SaveAs)
{
Word.Application wordApp = new Word.Application();
object missing = Missing.Value;
Word.Document myWordDoc = null;

if (File.Exists((string)filename))

{
object readOnly = false;
object isVisible = false;
wordApp.Visible = false;

myWordDoc = wordApp.Documents.Open(ref filename, ref missing, ref readOnly,
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);

myWordDoc.Activate();

//find and replave
this.FindAndReplace(wordApp, "<date1>", textBox2.Text);
this.FindAndReplace(wordApp, "<date2>", textBox3.Text);

}

else
{
MessageBox.Show("File not found!");
}

//Svae as
myWordDoc.SaveAs2(ref SaveAs, 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);

myWordDoc.Close();
wordApp.Quit();
MessageBox.Show("File Created!");


}

Continue reading...
 
Back
Top