WOrd Automation
-
Hi I'm writing an app that will have some Word automation, but I can not be certain if the user has already got an instance of word open, if they do i wish to use there unstance do my automation or else start a new instance. My problem is if they have it open my code makes a new instance and will close down the both instance when i finish can any one help.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Word=Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices;
using core= Microsoft.Office.Core;
using System.Diagnostics;
namespace WordProcess
{
/// /// Description of MainForm.
///
public partial class MainForm : Form
{Word.Application wordApp; Word.\_Document wordDocument; object document; object wordFalse = false; object wordTrue = true; object dynamic = 2; object wordMissing = System.Reflection.Missing.Value; public MainForm() { // // The InitializeComponent() call is required for Windows Forms designer support. // InitializeComponent(); // // TODO: Add constructor code after the InitializeComponent() call. // } void Button1Click(object sender, EventArgs e) { System.Diagnostics.Process\[\] wordPro = System.Diagnostics.Process.GetProcessesByName("winword"); if (wordPro.Length==0){ //No word Open start a new word process. Process p= Process.Start("WINWORD.EXE"); p.WaitForInputIdle(); } try{ object filePath =@"C:\\test.doc"; object filePath1 =@"C:\\test1.doc"; wordDocument = new Word.DocumentClass(); wordApp = (Word.Application)Marshal.GetActiveObject("Word.Application"); wordApp.Documents.Open(ref filePath, ref wordMissing, ref wordFalse, ref wordMissing, ref wordMissing, ref wordMissing, ref wordMissing, ref wordMissing, ref wordMissing, ref wordMissing, ref wordMissing, ref wordTrue, ref wordMissing, ref wordMissing, ref wordMissing, ref wordMissing); wordDocument.SaveAs(ref filePath1, ref wordMissing, ref wordFalse, ref wordMissing, ref wordMissing, ref wordMissing, ref wordMissing, ref wordMissing, ref wordMissing, ref wordMissing, ref wordMissing, ref wordTrue, ref wordMissing, ref wordMissing, ref wordMissing, ref wordMissing);
-
Hi I'm writing an app that will have some Word automation, but I can not be certain if the user has already got an instance of word open, if they do i wish to use there unstance do my automation or else start a new instance. My problem is if they have it open my code makes a new instance and will close down the both instance when i finish can any one help.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Word=Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices;
using core= Microsoft.Office.Core;
using System.Diagnostics;
namespace WordProcess
{
/// /// Description of MainForm.
///
public partial class MainForm : Form
{Word.Application wordApp; Word.\_Document wordDocument; object document; object wordFalse = false; object wordTrue = true; object dynamic = 2; object wordMissing = System.Reflection.Missing.Value; public MainForm() { // // The InitializeComponent() call is required for Windows Forms designer support. // InitializeComponent(); // // TODO: Add constructor code after the InitializeComponent() call. // } void Button1Click(object sender, EventArgs e) { System.Diagnostics.Process\[\] wordPro = System.Diagnostics.Process.GetProcessesByName("winword"); if (wordPro.Length==0){ //No word Open start a new word process. Process p= Process.Start("WINWORD.EXE"); p.WaitForInputIdle(); } try{ object filePath =@"C:\\test.doc"; object filePath1 =@"C:\\test1.doc"; wordDocument = new Word.DocumentClass(); wordApp = (Word.Application)Marshal.GetActiveObject("Word.Application"); wordApp.Documents.Open(ref filePath, ref wordMissing, ref wordFalse, ref wordMissing, ref wordMissing, ref wordMissing, ref wordMissing, ref wordMissing, ref wordMissing, ref wordMissing, ref wordMissing, ref wordTrue, ref wordMissing, ref wordMissing, ref wordMissing, ref wordMissing); wordDocument.SaveAs(ref filePath1, ref wordMissing, ref wordFalse, ref wordMissing, ref wordMissing, ref wordMissing, ref wordMissing, ref wordMissing, ref wordMissing, ref wordMissing, ref wordMissing, ref wordTrue, ref wordMissing, ref wordMissing, ref wordMissing, ref wordMissing);
Instead of quitting if you would just kill the process that your application started, then it should work fine.
"If you had to identify, in one word, the reason why the human race has not achieved, and never will achieve, its full potential, that word would be 'meetings'." - Dave Barry
-
Instead of quitting if you would just kill the process that your application started, then it should work fine.
"If you had to identify, in one word, the reason why the human race has not achieved, and never will achieve, its full potential, that word would be 'meetings'." - Dave Barry
the problem is it checks for a process and will start one fine but if there is one running the application will start a second instance of Word up that is ties to the first
-
the problem is it checks for a process and will start one fine but if there is one running the application will start a second instance of Word up that is ties to the first
In that case easiest way I can think of is start new winword process through your application and then kill it without harming any other word document.
"If you had to identify, in one word, the reason why the human race has not achieved, and never will achieve, its full potential, that word would be 'meetings'." - Dave Barry
-
In that case easiest way I can think of is start new winword process through your application and then kill it without harming any other word document.
"If you had to identify, in one word, the reason why the human race has not achieved, and never will achieve, its full potential, that word would be 'meetings'." - Dave Barry
thats something I'm looking at present myself main problem is making sure i only close down the new process any ides would be great thanks.
-
thats something I'm looking at present myself main problem is making sure i only close down the new process any ides would be great thanks.
AFAIK that if you use the
System.Diagnostics.Process
class and persist the object you can use it to close the specific instance of word that you opened.Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall
-
thats something I'm looking at present myself main problem is making sure i only close down the new process any ides would be great thanks.
check this how to kill a app http://www.codeproject.com/KB/cs/cskillapp.aspx[^]
Vikas Amin
My First Article on CP" Virtual Serial Port "[^]
modified on Thursday, July 24, 2008 5:33 PM
-
AFAIK that if you use the
System.Diagnostics.Process
class and persist the object you can use it to close the specific instance of word that you opened.Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall
-
dan neely wrote:
AFAIK
I am sorry but I dont know what this means.
"If you had to identify, in one word, the reason why the human race has not achieved, and never will achieve, its full potential, that word would be 'meetings'." - Dave Barry