My task is to open a word file, on a button1 click, and add an image to that word file at cursor position on button2 click so on button_click events I write:
private void button1_Click(object sender, EventArgs e){
Microsoft.Office.Interop.Word.Application WordApp = null;
WordApp = new Microsoft.Office.Interop.Word.Application(); //make the word application
documents = WordApp.Documents;
document = documents.Add(); //open a document in my created application
button2.visible=true;
}
private void button2_Click(object sender, EventArgs e){
button1.visible=false;
selection = WordApp.Selection;
inlineShapes = selection.InlineShapes;
inlineShape = inlineShapes.AddPicture(@"C:\MyPicture1.Jpeg"); // add picture to my created application document
}
every thing works file, But
problem is, that if i manually open a new word application while my program is running (after button1_click one document is already open), the image gets added any of the 2 document, where the cursor is.
I want that every time on button 2 click, the image should get added to WordApp = new Microsoft.Office.Interop.Word.Application() only, no mater on which document the cursor is. Please help....
I think I am making some mistake at creating the "selection" object , I think i am not properly refering it to wordApp. pls help :)
What I have tried:
I Tried the debugging steps but did't find the loophole. In my opinion I am correctly linking the objects but still, some problem is there. And I think when i open a new word file manually and put the cursor there ,
WordApp = new Microsoft.Office.Interop.Word.Application();
takes new system's document in place of my WordApp and my all process gets shifted there. I am unaware of where I am doing worng, please help.