Help about Office word In C#
-
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.
-
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.
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[^]; -
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.
-
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[^]; -
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[^]
-
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
-
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
-
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?