none
How to cancel close event of word template 2007? RRS feed

  • Question

  • I am opening word 2007 template from my c# application.
    In the word template there are some content controls.
    My requirement is that when user close the template,
    all the content controls will be validated for mandatory, correct
    input etc. If validation fails template will not close.
    For this I have captured the close event of the template in my code
    but not able to cancel the close event if validate fails. Close event
    always fires and template closes down.
     I need to stop the template from closing when validation fails.

    Note: I am Using Microsoft.Office.Interop.Word  12.0

    • Moved by Bob Shen Wednesday, April 25, 2012 6:03 AM (From:Visual C# General)
    Tuesday, April 24, 2012 9:19 AM

Answers

  • Hi Devender,

    Document_Close event of VBA doesnot have a facility to prevent document from closing.

    However, u can achieve the same via using Application.DoumentBeforeClose event.

    Please see the below links explaining the same.

    http://msdn.microsoft.com/en-us/library/aa211766(v=office.11).aspx

    http://msdn.microsoft.com/en-us/library/ff834271.aspx

    I won't be able to provide u the working code example cause I am not so well verged with VB. Rather I would make use of VSTO.

    However, above mentioned links would give u a better idea.

    Hope it helps!


    Regards, Parth Shah

    Wednesday, April 25, 2012 10:13 AM
  • Can we do the same close event of a document in VBA?

    Open the document in question in Word. Press Alt+F11 to switch to the VBA Editor. Look for the document in the Project window and expand "Microsoft Word Objects"; select the item "ThisDocument".

    At the top of the VBA code windows there are two drop-down lists. From the one on the left, select "Document". Then from the one on the right, select "Close". The VBA Editor will write the skeleton code for the Close event for the document and you can write your code there. This event will be specific to that document.

    In addition, the Word Application has a DocumentBeforeClose event which could be monitored by your C# application. The application-level event has a Cancel parameter.


    Cindy Meister, VSTO/Word MVP

    Wednesday, April 25, 2012 5:06 PM
    Moderator

All replies

  • Try handling BeforeClose event. Set Cancel property of argument to true if document should not be closed.

    Tuesday, April 24, 2012 9:44 AM
  • There is no BeforeClose event I found. Below is the list of available events in

    Microsoft.Office.Interop.Word.DocumentClass:

            public virtual event DocumentEvents2_BuildingBlockInsertEventHandler BuildingBlockInsert;
            public virtual event DocumentEvents2_ContentControlAfterAddEventHandler ContentControlAfterAdd;
            public virtual event DocumentEvents2_ContentControlBeforeContentUpdateEventHandler ContentControlBeforeContentUpdate;
            public virtual event DocumentEvents2_ContentControlBeforeDeleteEventHandler ContentControlBeforeDelete;
            public virtual event DocumentEvents2_ContentControlBeforeStoreUpdateEventHandler ContentControlBeforeStoreUpdate;
            public virtual event DocumentEvents2_ContentControlOnEnterEventHandler ContentControlOnEnter;
            public virtual event DocumentEvents2_ContentControlOnExitEventHandler ContentControlOnExit;
            public virtual event DocumentEvents_CloseEventHandler DocumentEvents_Event_Close;
            public virtual event DocumentEvents_NewEventHandler DocumentEvents_Event_New;
            public virtual event DocumentEvents_OpenEventHandler DocumentEvents_Event_Open;
            public virtual event DocumentEvents2_CloseEventHandler DocumentEvents2_Event_Close;
            public virtual event DocumentEvents2_SyncEventHandler DocumentEvents2_Event_Sync;
            public virtual event DocumentEvents2_NewEventHandler New;
            public virtual event DocumentEvents2_OpenEventHandler Open;
            public virtual event DocumentEvents2_XMLAfterInsertEventHandler XMLAfterInsert;
            public virtual event DocumentEvents2_XMLBeforeDeleteEventHandler XMLBeforeDelete;

    Tuesday, April 24, 2012 10:04 AM
  • In my Visual Studio 2010, when I double click on ThisDocument.cs in Solution Explorer, the events appear in Events tab of Properties panel.

    Tuesday, April 24, 2012 10:41 AM
  •  ThisDocument.cs is not available in my solution. Please help me how I can use this file in my solution.

    If possible, pls. provide some code snippet.

    In my solution I am just using Microsoft.Office.Interop.Word.dll

    Wednesday, April 25, 2012 4:52 AM
  • I am opening word 2007 template from my c# application.
    In the word template there are some content controls.
    My requirement is that when user close the template,
    all the content controls will be validated for mandatory, correct
    input etc. If validation fails template will not close.
    For this I have captured the close event of the template in my code
    but not able to cancel the close event if validate fails. Close event
    always fires and template closes down.
     I need to stop the template from closing when validation fails.

    Which will be the right choice- VSTO or VBA?

    Please help.

    Wednesday, April 25, 2012 6:38 AM
  • I am opening word 2007 template from my c# application.
    In the word template there are some content controls.
    My requirement is that when user close the template,
    all the content controls will be validated for mandatory, correct
    input etc. If validation fails template will not close.
    For this I have captured the close event of the template in my code
    but not able to cancel the close event if validate fails. Close event
    always fires and template closes down.
     I need to stop the template from closing when validation fails.

    Which will be the right choice- VSTO or VBA?

    Wednesday, April 25, 2012 7:14 AM
  • Hi Devender,

    Please see the below mentioned link for ur dilemma between VSTO and VBA.

    http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/2dffdbef-10b2-4916-976c-f4221035466a

    I guess in the closing event of ur template u can write e.Handled = true; where e is EventArgs.

    Hope it helps!

    Please mark it as an answer if it resolves ur issue


    Regards, Parth Shah

    Wednesday, April 25, 2012 8:10 AM
  • Hi Parth shah,

    Thanks for reply.

    I am new to VSTO and VBA.

    You suggest :

    " in the closing event of ur template u can write e.Handled = true; where e is EventArgs."

    I think you are suggesting here for VSTO solution.

    Can we do same in VBA?

    As far I know there is no EventArgs in closing event of Template in VBA.

    Can we handle/cancel close event in VBA?

    Wednesday, April 25, 2012 8:41 AM
  • Hi Devender,

    In the close event in VBA u have got an argument "Cancel" which will be false when close event is fired.

    Setting the Cancel argument to "True" will prohibit the close event to complete and will leave the form open.

    Please see the below links for more clarification.

    http://msdn.microsoft.com/en-us/library/aa171213(v=office.11).aspx

    http://www.excelbanter.com/showthread.php?t=429942

    Please mark it as an answer if it resolves ur issue.


    Regards, Parth Shah

    • Proposed as answer by parth.shah Wednesday, April 25, 2012 8:51 AM
    Wednesday, April 25, 2012 8:51 AM
  • Hi Parth,

    When I use "Cancel As Boolean"

    Example: Private Sub Document_Close(Cancel As Boolean)

    in Close event in VBA, I am getting compile error.

    It is not working for me.
    Please provide some working code snippet to cancel close event of a template.

    Thanks.

    Wednesday, April 25, 2012 9:16 AM
  • Hi Devender,

    Please refer to the following link:

    Document.BeforeClose Event
    http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.document.beforeclose(v=vs.100).aspx

    private void DocumentBeforeClose()
    {
        Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
        vstoDoc.BeforeClose += new System.ComponentModel.CancelEventHandler(ThisDocument_BeforeClose);
    }
    
    void ThisDocument_BeforeClose(object sender, System.ComponentModel.CancelEventArgs e)
    {
        System.Windows.Forms.MessageBox.Show("The document is closing.");
    }

    Thanks.


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, April 25, 2012 9:32 AM
    Moderator
  • Can we cancel the close event of a template in VBA?

    If yes, please provide some code snippet.

    I am not able to do so as there is no EventArgs in close event of a document in VBA.

    Please help.

    Wednesday, April 25, 2012 9:45 AM
  • Thanks Yoyo Jiang for reply.

    I think it will work in VSTO and could be a solution but I am not using VSTO.

    Can we do the same close event of a document in VBA?

    Wednesday, April 25, 2012 9:49 AM
  • Hi Devender,

    Document_Close event of VBA doesnot have a facility to prevent document from closing.

    However, u can achieve the same via using Application.DoumentBeforeClose event.

    Please see the below links explaining the same.

    http://msdn.microsoft.com/en-us/library/aa211766(v=office.11).aspx

    http://msdn.microsoft.com/en-us/library/ff834271.aspx

    I won't be able to provide u the working code example cause I am not so well verged with VB. Rather I would make use of VSTO.

    However, above mentioned links would give u a better idea.

    Hope it helps!


    Regards, Parth Shah

    Wednesday, April 25, 2012 10:13 AM
  • as a matter of curiousity, what program are you working with (word, excel, powerpoint, visio, etc.)?

    al


    If this answer solves your problem, please check Mark as Answered. If this answer helps, please click the Vote as Helpful button. Al Edlund Visio MVP

    Wednesday, April 25, 2012 11:34 AM
  • Can we do the same close event of a document in VBA?

    Open the document in question in Word. Press Alt+F11 to switch to the VBA Editor. Look for the document in the Project window and expand "Microsoft Word Objects"; select the item "ThisDocument".

    At the top of the VBA code windows there are two drop-down lists. From the one on the left, select "Document". Then from the one on the right, select "Close". The VBA Editor will write the skeleton code for the Close event for the document and you can write your code there. This event will be specific to that document.

    In addition, the Word Application has a DocumentBeforeClose event which could be monitored by your C# application. The application-level event has a Cancel parameter.


    Cindy Meister, VSTO/Word MVP

    Wednesday, April 25, 2012 5:06 PM
    Moderator
  • Hi Devender

    Has my answer to your other thread helped at all?


    Cindy Meister, VSTO/Word MVP

    Wednesday, April 25, 2012 5:21 PM
    Moderator
  • I am using word template.
    Thursday, April 26, 2012 7:02 AM
  • Hi Cindy Meister,

    Thanks for giving steps for adding "Close" event in a document in VBA.. :)

    I get following sekelton code for close event:

    Private Sub Document_New()

    End Sub

    My question is:  can we cancel the close event in VBA?

    Thursday, April 26, 2012 9:58 AM
  • HI Devender

    <<I get following sekelton code for close event:

    Private Sub Document_New()

    End Sub>>

    No, that's the default that the VBA editor will add when you choose "Document" from the list on the left. You MUST choose Close from the list on the right. This is the Close event that can be connected to the document. But as far as I know, it cannot be used to prevent the document from closing.

    In order do that you must use the second event I mentioned, that's at the application level. The question is, what you want to use for that. If you want this to be entirely in the document, then you must add a class to your VBA project to provide the event handler for DocumentBeforeClose. See the information here:

    http://office.microsoft.com/client/helppreview.aspx?AssetId=HV806000649990&lcid=1033&NS=WINWORD


    Cindy Meister, VSTO/Word MVP

    Friday, April 27, 2012 8:45 AM
    Moderator
  • Hi Devender,

    I find another thread you asked in Office forum, and also I understand your issue is not about WPF, so I'm merging your thread.

    Thank you for your understanding.

    Have a nice day.


    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us

    Friday, April 27, 2012 9:05 AM
  • Finally i decided to go with VSTO for my purpose.
    Thanks to all for your suggestions.
    Wednesday, May 2, 2012 9:42 AM
  • Using VSTO I am able to cancel the close event by writing following code in ThisDocument_BeforeClose event handler:

    e.Cancel = true;

    when I click on close button of the template it prompt me to save the document but I don't want this prompt message.
    For this I did following code in ThisDocument_BeforeClose event handler but it is not working for me:

    Word.Template template = (Word.Template)this.Application.ActiveDocument.get_AttachedTemplate();
    template.Saved = true;

    Wednesday, May 2, 2012 9:44 AM