none
How to close and exit current Microsoft word from Winform? RRS feed

  • Question

  • Currently I have created a few Winform to work with Microsoft word (2007). One of the form is used to attached document to the word.

    After I attached the document I want to close the current Winword but not exit yet. I am having problem closing the current Winword from the form. If I create a new instance of word it always end up launch a new Winword. I have tried to get the current running word process and kill the process but it is not ideal. 

    Please help!!!


    ============== Praneth Meas
    Tuesday, March 8, 2011 6:26 PM

Answers

  • Hello Praneth,

    By doing a sample on my side, it works fine without showing a prompt on my side. The version of Word on my side is Microsoft Office Word 2007 (12.0.6545.5000) SP2 MSO (12.0.6545.5004). Please check it and see if there is any difference between us. The code I used is as below,

              try
                {
                    Word.Application wordApp = Marshal.GetActiveObject("Word.Application") as Word.Application;
                    if (wordApp != null)
                    {
                        object save=Word.WdSaveOptions.wdSaveChanges;
                        object missing=Type.Missing;
                        wordApp.ActiveDocument.Close(ref save, ref missing, ref missing);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }

    Although it is C# code, the syntax is the same as yours.

    The Document.Close method will close the specified document. And the Documents.Close method will close all documents which is represented by Documents collection in statement. For these two methods, please see these two MSDN pages:

    Document.Close Method: http://msdn.microsoft.com/en-us/library/bb214403(office.12).aspx.
    Documents.Close Method: http://msdn.microsoft.com/en-us/library/bb214417(office.12).aspx.


    Bessie Zhao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Bessie Zhao Wednesday, March 16, 2011 9:59 AM
    Thursday, March 10, 2011 9:53 AM

All replies

  • Hello Praneth,

    Thanks for posting. Are you looking for Document.Close method to close the specified document? How you attch the document to the Word?

    From what you side, I think you are using Word automation in this Windows form application. So we also need to release com objects by using ReleaseComObject method or GC.Collect. For more information about it, please see this thread: http://social.msdn.microsoft.com/forums/en-US/vsto/thread/a12add6b-99ea-4677-8245-cd667101683e.

    If this post does not help you, just feel free to follow up. Also would you mind simplifying a project/steps and sharing it with us? So I could use it on my side to see this scenario for further research. Have a nice day.


    Bessie Zhao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, March 9, 2011 5:26 AM
  • Hi Bessie,

     

    Thanks for response. Actually I am able to Close the document through get instance of current running Word and call ActiveDocument.Close() method. However, it is always prompting for save change the document.

    How do we force to close the document without prompting? I am not sure what to pass in ActiveDocument.Close(......) method.


    ============== Praneth Meas
    Wednesday, March 9, 2011 6:07 AM
  • Hello Praneth,

    Please use wdSaveChanges as the first parameter of ActiveDocument.Close() method. This value says that it saves pending changes automatically without prompting the user.

    For more information, please see these two MSDN pages:

    Document.Close Method: http://msdn.microsoft.com/en-us/library/bb214403(office.12).aspx?wa=wsignin1.0.

    WdSaveOptions Enumeration: http://msdn.microsoft.com/en-us/library/bb238160(office.12).aspx.

    If you have any concern on this post, just feel free to follow up. Have a nice day.


    Bessie Zhao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, March 9, 2011 7:35 AM
  • I have tried it but still it prompt for me to save changes. Not sure I miss anything below is my CloseWord function:

     

    Private Sub CloseWord()

            Try        

     

                Dim oWord As Word.Application = System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application")

                       If Not oWord Is Nothing Then           

                    oWord.ActiveDocument.Close(SaveChanges:=Word.WdSaveOptions.wdSaveChanges)                  

                        End If       

            Catch ex As Exception

                Throw ex

            End Try

        End Sub

     

    By the way what the different between these two functions:  oWord.ActiveDocument.Close and oWord.Documents.Close? I try both and they act same to me.


    ============== Praneth Meas
    Wednesday, March 9, 2011 7:09 PM
  • Hello Praneth,

    By doing a sample on my side, it works fine without showing a prompt on my side. The version of Word on my side is Microsoft Office Word 2007 (12.0.6545.5000) SP2 MSO (12.0.6545.5004). Please check it and see if there is any difference between us. The code I used is as below,

              try
                {
                    Word.Application wordApp = Marshal.GetActiveObject("Word.Application") as Word.Application;
                    if (wordApp != null)
                    {
                        object save=Word.WdSaveOptions.wdSaveChanges;
                        object missing=Type.Missing;
                        wordApp.ActiveDocument.Close(ref save, ref missing, ref missing);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }

    Although it is C# code, the syntax is the same as yours.

    The Document.Close method will close the specified document. And the Documents.Close method will close all documents which is represented by Documents collection in statement. For these two methods, please see these two MSDN pages:

    Document.Close Method: http://msdn.microsoft.com/en-us/library/bb214403(office.12).aspx.
    Documents.Close Method: http://msdn.microsoft.com/en-us/library/bb214417(office.12).aspx.


    Bessie Zhao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Bessie Zhao Wednesday, March 16, 2011 9:59 AM
    Thursday, March 10, 2011 9:53 AM