Three button click events in c# accessing the same docx

Jul 24 2017 9:41 AM
How do I write my code for three buttons on a user form such that:
  • button 7 loads a word document and opens it,
  • button 2 is a find button that searches the open document also highlight found matches one after the other as the user chooses which ones to replace,
  • button 3 is a replace button that does the replacement while the document is open and visible to the user.
Here is what I have so far but I'm unable to get the other two buttons "button 2" and "button 3" to work without the word app opening over and over every time I hit the find button or the replace button.
 
here is what I have so far:
  1. private void button7_Click(object sender, EventArgs e)   
  2. {   
  3. if (of.ShowDialog() == DialogResult.OK)   
  4. {   
  5. textBox7.Text = of.FileName;   
  6. object fileName = Path.Combine(System.Windows.Forms.Application.StartupPath, of.FileName);   
  7. Microsoft.Office.Interop.Word.Application wordApp1 = new Microsoft.Office.Interop.Word.Application { Visible = true };   
  8. Microsoft.Office.Interop.Word.Document aDoc = wordApp1.Documents.Open(ref fileName, ReadOnly: false, Visible: true);   
  9. Microsoft.Office.Interop.Word.Find fnd = wordApp1.ActiveWindow.Selection.Find;   
  10. }   
  11. }   
  12. private void button2_Click(object sender, EventArgs e) { }   
  13. public void button3_Click(object sender, EventArgs e) { }   
  14. private void comboBox3_SelectedIndexChanged(object sender, EventArgs e) { }   
  15. }  

Answers (1)