none
How can I apply one of the property "Saved" in Word VSTO ? RRS feed

  • Question

  • Hi, there..

    I made a button on a document using Word VSTO AddIn Ribbon button.

    When I click the button, it makes another kind of button on a current document.

    Fine !

    Now, I do not need to save this document,

    and I do not want any kind of confirmation alert window while closing it.

    So, I added a simple line of code at the end of the codes.

    Unfortunately, It doe not work at all.

    What do I have to do ?

    Regards

    (I added two screen shots. One is the codes and the other one is alert window that I do not want to see.)


    Sunday, March 15, 2015 2:22 AM

Answers

  • Hi Using,

    >> Now, I do not need to save this document, and I do not want any kind of confirmation alert window while closing it.

    I have made a simple demo to reproduce your issue. If I add contents to the word instead of adding a button, the code would work correctly. I assume that the "Globals.ThisAddIn.Application.ActiveDocument.Saved" is applied to the contents, but it is not applied to the host controls.

    For a workaround, you could add the close event in the BeforeClose event. In the close event, you could close the document without saving the changes. Some key code like below:

    1.add the BeforeClose event

                Microsoft.Office.Tools.Word.Document vstoDoCuMeNT =
                Globals.Factory.GetVstoObject(nativeDoCuMeNT); 
                vstoDoCuMeNT.BeforeClose += ThisDocument_BeforeClose; 

    2. define the BeforeClose event

            void ThisDocument_BeforeClose(object sender, System.ComponentModel.CancelEventArgs e)
            {
                Globals.ThisAddIn.Application.ActiveDocument.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
                System.Windows.Forms.MessageBox.Show("The document is closing.");
            }

    But in this way, after you add the button, if you add some contents to the word, all of the changes would not be saved.

    Sorry for the inconvenience and hope it will help.​

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, March 17, 2015 1:31 AM

All replies


  •             Microsoft.Office.Interop.Word.Document       nativeDoCuMeNT =
                Globals.ThisAddIn.Application.ActiveDocument;

                Microsoft.Office.Tools.Word.Document   vstoDoCuMeNT =
                Globals.Factory.GetVstoObject( nativeDoCuMeNT );

                Microsoft.Office.Tools.Word.Controls.Button       VuTToN =
                vstoDoCuMeNT.Controls.AddButton( Globals.ThisAddIn.Application.Selection.Range, 100, 20, "Button" );

                VuTToN.Text = "Hello Button" ;

                Globals.ThisAddIn.Application.ActiveWindow.ActivePane.View.Zoom.Percentage = 100;

                Globals.ThisAddIn.Application.ActiveDocument.Saved = true;

    Sunday, March 15, 2015 2:22 AM
  • I changed, checked and confirmed the boolean value of the each one. It works fine.

                vstoDoCuMeNT.Saved = true ;
                nativeDoCuMeNT.Saved = true ;

    But, the alert pop up never give up.

    What do I have to do ?

    Regards

    Sunday, March 15, 2015 11:24 PM
  • Hi Using,

    >> Now, I do not need to save this document, and I do not want any kind of confirmation alert window while closing it.

    I have made a simple demo to reproduce your issue. If I add contents to the word instead of adding a button, the code would work correctly. I assume that the "Globals.ThisAddIn.Application.ActiveDocument.Saved" is applied to the contents, but it is not applied to the host controls.

    For a workaround, you could add the close event in the BeforeClose event. In the close event, you could close the document without saving the changes. Some key code like below:

    1.add the BeforeClose event

                Microsoft.Office.Tools.Word.Document vstoDoCuMeNT =
                Globals.Factory.GetVstoObject(nativeDoCuMeNT); 
                vstoDoCuMeNT.BeforeClose += ThisDocument_BeforeClose; 

    2. define the BeforeClose event

            void ThisDocument_BeforeClose(object sender, System.ComponentModel.CancelEventArgs e)
            {
                Globals.ThisAddIn.Application.ActiveDocument.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
                System.Windows.Forms.MessageBox.Show("The document is closing.");
            }

    But in this way, after you add the button, if you add some contents to the word, all of the changes would not be saved.

    Sorry for the inconvenience and hope it will help.​

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, March 17, 2015 1:31 AM
  • Dear, Edward !!

    I really appreciated with your kind/exact info. and briiiiliiant workaround.

    It works like a charm !!!

    Regards

    Everydai

    Tuesday, March 17, 2015 8:52 AM
  • I am not sure about that You are coming back or not.

    Any ways.

    I have one more (actually, the same one) question.

    OK

    It works fine with me.

    But.

    If I run the same (exactly same) process again, It does not work at all.

    I checke it out.

    The process/flow does not goes into the event(BeforeClose event).

    It is just working as if the event is not exist at all.

    So, from the second time, I can see the old good alert window again.

    Can you show me some nice workaround for this kind of situation ?

    I hope I made good explanation.

    Regards

    .

    (I guess, it will be better to explain some more specifically.

    There is a document "FiRST".

    I click Ribbon AddIn button.

    It makes a button on another(newly added) document "SeCoND".

    Now, I close the "SeCoND".

    The event you teached me works beautifully.

    Now, I am with the document "FiRST" again.

    I click the Ribbon AddIn button again.

    It makes a button on another(newly added) document "THiRD" (actually it has another some_counted_automatic_name).

    I am colsing the "THiRD" document.

    HERE, I can see the "ALeRT WiNDoW".

    I still do not want to see "ALeRT WiNDoW".

    Please help.

    Thanks for reading long and tedius story of mine.

    )





    Tuesday, March 17, 2015 1:56 PM
  • Hi Using,

    >>I click the Ribbon AddIn button again. It makes a button on another(newly added) document "THiRD" (actually it has another some_counted_automatic_name).

    Could you share us the Ribbon AddIn button is the same button which is button13? I am not sure how the button achieve that make a button on another document “THIRD”. Based on your coded, I did not see the function. If you add a new button, I think that you need to get the document “THIRD”, and add the BeforeClose event, just as the code below:

    Microsoft.Office.Tools.Word.Document vstoDoCuMeNT =
                Globals.Factory.GetVstoObject(nativeDoCuMeNT); 
                vstoDoCuMeNT.BeforeClose += ThisDocument_BeforeClose

    In the code above, you need to set the “THIRD” document as “nativeDoCuMeNT”.

    In addition, as the reply above, this workaround may introduce some new issues which we cannot expect. I am not sure why you do not want to save the button. But I recommend you save the changes or remove the button with code instead of using the BeforeClose event.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, March 19, 2015 7:48 AM
  • Hi, You are back, Welcome.

    If I made any kind of confusion. sorry, that's because I am very beginner.

    Here.

    I am attaching Code/Ribbon screencapture and code itself.

    Since your some days ago answer I made so many changes, I lost all the code and so I worte some new ones. And (Un)fortunately, it shows the same situation.

    void V_DoCu_BeforeClose( object sender, System.ComponentModel.CancelEventArgs e )
    	{
    		Globals.ThisAddIn.Application.ActiveDocument.Close( Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges );	
    	}
    private void AsK_Click( object sender, RibbonControlEventArgs e )
    	{
    		Microsoft.Office.Interop.Word.Document	 N_DoCu = Globals.ThisAddIn.Application.Documents.Add();			
    		Microsoft.Office.Tools.Word.Document V_DoCu = Globals.Factory.GetVstoObject( N_DoCu );										
    		Button VuTToN = V_DoCu.Controls.AddButton( Globals.ThisAddIn.Application.Selection.Range, 100, 20, "something" );	
    		VuTToN.Text		= "Hello button" ;
    		Globals.ThisAddIn.Application.ActiveWindow.ActivePane.View.Zoom.Percentage = 100; 
    		V_DoCu.BeforeClose	+= new System.ComponentModel.CancelEventHandler ( V_DoCu_BeforeClose );
    	}
    

    Thursday, March 19, 2015 2:43 PM
  • You can see red angry bird up there.

    That is my button on Ribbon.

    If I click it.

    It makes a new document and a button on it.

    The event you teach works fine.

    So, I can finish the new document without alert window.

    Now

    Let's make click "again".

    This time the event you teach me, does not work at all.

    Ther reason why I do not want to save the document with document_level_button is

    if I save it. The button looks so stupid. It is there, but it never works at all. It looks like a ghost. That is all. If the button works fine. I am fine with it too.

    Regards

    Thursday, March 19, 2015 2:46 PM
  • ​Hi Using,

    Sorry for the delay.

    This workaround may introduce some new issues. Since the original issue is about “one of the property "Saved" in Word VSTO”, and the new issue about “BeforeClose event do not work”, I will recommend you starting a new thread to discuss it speratately.

    The reason why I suggested is:

    #1 There would be more community members to discuss the question.
    #2 For people who have the similar question, it would be easier for them to find the answer from a specific thread.

    Thanks for your understanding.

    Best Regards,

    Edward

     

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, March 26, 2015 10:10 AM