Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. WOrd Automation

WOrd Automation

Scheduled Pinned Locked Moved C#
helpasp-netwinformscomgraphics
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    burrows stephen
    wrote on last edited by
    #1

    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);
    
    D 1 Reply Last reply
    0
    • B burrows stephen

      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);
      
      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #2

      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

      B 1 Reply Last reply
      0
      • D dan sh

        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

        B Offline
        B Offline
        burrows stephen
        wrote on last edited by
        #3

        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

        D 1 Reply Last reply
        0
        • B burrows stephen

          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

          D Offline
          D Offline
          dan sh
          wrote on last edited by
          #4

          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

          B 1 Reply Last reply
          0
          • D dan sh

            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

            B Offline
            B Offline
            burrows stephen
            wrote on last edited by
            #5

            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.

            D V 2 Replies Last reply
            0
            • B burrows stephen

              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.

              D Offline
              D Offline
              Dan Neely
              wrote on last edited by
              #6

              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

              D 1 Reply Last reply
              0
              • B burrows stephen

                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.

                V Offline
                V Offline
                vikas amin
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                • D Dan Neely

                  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

                  D Offline
                  D Offline
                  dan sh
                  wrote on last edited by
                  #8

                  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

                  D 1 Reply Last reply
                  0
                  • D dan sh

                    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

                    D Offline
                    D Offline
                    Dan Neely
                    wrote on last edited by
                    #9

                    As Far As I Know.

                    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

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups