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. Help about Office word In C#

Help about Office word In C#

Scheduled Pinned Locked Moved C#
csharphelpquestion
8 Posts 3 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.
  • K Offline
    K Offline
    kcynic
    wrote on last edited by
    #1

    I want to control the Office Word's behavior using C#. i.e. Create a new word document in local PC with specified path. And if I open a remote word document I could control that when user close the document,Word should not quest a saving path but save the change into the remote region path(if the user selected save the change). Becides these,I also want to know whcich behaviors that the Office Word have and I can retrieve these messages? Thanks. GOOD LUCK.

    T M 2 Replies Last reply
    0
    • K kcynic

      I want to control the Office Word's behavior using C#. i.e. Create a new word document in local PC with specified path. And if I open a remote word document I could control that when user close the document,Word should not quest a saving path but save the change into the remote region path(if the user selected save the change). Becides these,I also want to know whcich behaviors that the Office Word have and I can retrieve these messages? Thanks. GOOD LUCK.

      T Offline
      T Offline
      tker
      wrote on last edited by
      #2

      Hi, Do you wish to open the Word doc from another process? Do you wish to populate it? Would you like to automate the creation of documents, either before Word opens or by helping the user after it has opened? Basically to open Word with a new document you need to do the following: First create a new Windows Forms project. Add a button to the default form. Next add a reference to the Microsoft Office 12.0 Object Library & Microsoft Word 12.0 Object Library (for Office 2007) so you have access to the word objects. The Office 12.0 Object library is not strictly necessary, but you may find you need it to complete other operations further down the track. Next use the following as the code behind for Form1: //START CODE using System.Windows.Forms; using Word = Microsoft.Office.Interop.Word; namespace WordSample { public partial class Form1 : Form { private object m_Missing = Type.Missing; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //create word application Word.Application word = new Word.Application(); //create word document Word.Document wordDoc = new Word.Document(); //make visible word.Visible = true; //add document to application wordDoc.Application.DocumentBeforeSave += new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentBeforeSaveEventHandler(Application_DocumentBeforeSave); wordDoc = word.Documents.Add(ref m_Missing, ref m_Missing, ref m_Missing, ref m_Missing); } void Application_DocumentBeforeSave(Microsoft.Office.Interop.Word.Document Doc, ref bool SaveAsUI, ref bool Cancel) { //PUT YOUR PATH CODE HERE; } } } //END CODE That will create a new instance of word. To modify the closing behaviour there are a number of events available to you including the Application.DocumentBeforeSave or Application.DocumentBeforeClose events. I have included a dummy ApplicationBeforeSave event for you. There is extensive documentation on MSDN regarding the Word ObjectModel that lists all events (http://msdn2.microsoft.com/en-us/library/kw65a0we(vs.80).aspx[^];

      K 1 Reply Last reply
      0
      • K kcynic

        I want to control the Office Word's behavior using C#. i.e. Create a new word document in local PC with specified path. And if I open a remote word document I could control that when user close the document,Word should not quest a saving path but save the change into the remote region path(if the user selected save the change). Becides these,I also want to know whcich behaviors that the Office Word have and I can retrieve these messages? Thanks. GOOD LUCK.

        M Offline
        M Offline
        MarkB777
        wrote on last edited by
        #3

        I had a similar project, except it involved Excel. You'll be amazed how easy MS Office is to use in C#. Microsoft has good examples, try this one http://support.microsoft.com/kb/316384[^]

        K 1 Reply Last reply
        0
        • T tker

          Hi, Do you wish to open the Word doc from another process? Do you wish to populate it? Would you like to automate the creation of documents, either before Word opens or by helping the user after it has opened? Basically to open Word with a new document you need to do the following: First create a new Windows Forms project. Add a button to the default form. Next add a reference to the Microsoft Office 12.0 Object Library & Microsoft Word 12.0 Object Library (for Office 2007) so you have access to the word objects. The Office 12.0 Object library is not strictly necessary, but you may find you need it to complete other operations further down the track. Next use the following as the code behind for Form1: //START CODE using System.Windows.Forms; using Word = Microsoft.Office.Interop.Word; namespace WordSample { public partial class Form1 : Form { private object m_Missing = Type.Missing; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //create word application Word.Application word = new Word.Application(); //create word document Word.Document wordDoc = new Word.Document(); //make visible word.Visible = true; //add document to application wordDoc.Application.DocumentBeforeSave += new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentBeforeSaveEventHandler(Application_DocumentBeforeSave); wordDoc = word.Documents.Add(ref m_Missing, ref m_Missing, ref m_Missing, ref m_Missing); } void Application_DocumentBeforeSave(Microsoft.Office.Interop.Word.Document Doc, ref bool SaveAsUI, ref bool Cancel) { //PUT YOUR PATH CODE HERE; } } } //END CODE That will create a new instance of word. To modify the closing behaviour there are a number of events available to you including the Application.DocumentBeforeSave or Application.DocumentBeforeClose events. I have included a dummy ApplicationBeforeSave event for you. There is extensive documentation on MSDN regarding the Word ObjectModel that lists all events (http://msdn2.microsoft.com/en-us/library/kw65a0we(vs.80).aspx[^];

          K Offline
          K Offline
          kcynic
          wrote on last edited by
          #4

          Thanks. Although I am a beginer of C#,but I will glad to have a try. Best wishes.

          T 1 Reply Last reply
          0
          • M MarkB777

            I had a similar project, except it involved Excel. You'll be amazed how easy MS Office is to use in C#. Microsoft has good examples, try this one http://support.microsoft.com/kb/316384[^]

            K Offline
            K Offline
            kcynic
            wrote on last edited by
            #5

            I will glad to read it carefully. Thank you very much.

            1 Reply Last reply
            0
            • K kcynic

              Thanks. Although I am a beginer of C#,but I will glad to have a try. Best wishes.

              T Offline
              T Offline
              tker
              wrote on last edited by
              #6

              Hi kcynic, The code I gave you will work straight out to open word from a windows form. Changing the path can easily be done in the event that is created for the document when you open it from the form, have a play with it and you should find it easy enough. If you have more questions please feel free to ask whatever. Mark was right; Office is incredibly easily and powerfully automated and can be customised to do many things for the user. If you are really serious about Office Automation look into VSTO. It gives a fantastic, totally managed code, interface for Office development. Regards, Toby Toby Russell

              K 1 Reply Last reply
              0
              • T tker

                Hi kcynic, The code I gave you will work straight out to open word from a windows form. Changing the path can easily be done in the event that is created for the document when you open it from the form, have a play with it and you should find it easy enough. If you have more questions please feel free to ask whatever. Mark was right; Office is incredibly easily and powerfully automated and can be customised to do many things for the user. If you are really serious about Office Automation look into VSTO. It gives a fantastic, totally managed code, interface for Office development. Regards, Toby Toby Russell

                K Offline
                K Offline
                kcynic
                wrote on last edited by
                #7

                All the words are good guide for me.But there also some problems puzzled me. For example,I offen find some compling error like some .dll files are used somewhere so VS can not copy the created file to the local device. Why?

                T 1 Reply Last reply
                0
                • K kcynic

                  All the words are good guide for me.But there also some problems puzzled me. For example,I offen find some compling error like some .dll files are used somewhere so VS can not copy the created file to the local device. Why?

                  T Offline
                  T Offline
                  tker
                  wrote on last edited by
                  #8

                  There are a myriad of reasons why you might get some messages. My advice is to make sure you don't have any instances of your code running anywhere as that will cause errors such as the one you mentioned and to google the errors as they arise. Toby

                  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