Word Automation RRS feed

  • Question

  • 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 
        /// <summary> 
        /// Description of MainForm. 
        /// </summary> 
        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. 
                // 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"); 
                    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); 
                    wordDocument.Close(ref wordFalse, ref wordMissing, ref wordMissing);//Close the document no saving 
                    wordApp.Quit(ref wordMissing, ref wordMissing, ref wordMissing);//Remove the instance opened 
                }catch(MarshalDirectiveException mw){ 
                }catch(Exception ex){ 

    • Moved by jack 321 Thursday, September 11, 2008 7:49 AM off topic (Moved from Visual C# General to Off-Topic Posts (Do Not Post Here))
    Tuesday, September 9, 2008 3:37 PM