none
DocumentBeforePrint - VSTO Addin, Cancel does not cancel the print RRS feed

  • Question

  • I am writing a Word addin in VS 2010 for Word 2010.

    The DocumentBeforePrint event fires, and the value of 'Cancel' changes to true, but the document still prints.

    I saw that there was a bug with this in VS 2003, but it says that it was fixed in Office 2003.

    Can anyone else get this to work?

    Many thanks, Kel

    Monday, April 11, 2011 11:16 AM

Answers

  • Hi Tom,

    Thanks for looking at this.  I am writing this in VB.Net.  A colleague wrote the code for me in C#, and it worked as yours does.

    I've discovered that the events aren't being picked up properly when you use 'Handles Application.DocumentBeforePrint'.

    I have now changed all the application events to be hooked up using 'AddHandler' in ThisAddinStartup in VB and it works.

    An odd one, but it seems to be okay at the moment.

    Kelly

    • Marked as answer by Kel55 Wednesday, April 13, 2011 8:51 AM
    Wednesday, April 13, 2011 8:49 AM

All replies

  • Am I asking the wrong question?  Can anyone reproduce this error?

    Thanks, Kel

    Tuesday, April 12, 2011 10:13 AM
  • I have checked, and this works fine through VBA
    Tuesday, April 12, 2011 10:37 AM
  • Hi Kel55,

     

    Thanks for posting in MSDN Forum.

     

    I try to reproduce your issue, but I can’t meet the situation which you met. This is the snippet I used to reproduce your issue. Please find out the different between us.

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml.Linq;
    using Word = Microsoft.Office.Interop.Word;
    using Office = Microsoft.Office.Core;
    using Microsoft.Office.Tools.Word;
    
    namespace DocumentBeforePrintEventCancel
    {
      public partial class ThisAddIn
      {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
          Application.DocumentBeforePrint += new Word.ApplicationEvents4_DocumentBeforePrintEventHandler(Application_DocumentBeforePrint);
        }
    
        void Application_DocumentBeforePrint(Word.Document Doc, ref bool Cancel)
        {
          if (MessageBox.Show("Do you want to print?", "DocumentBeforPrint", MessageBoxButtons.YesNo) == DialogResult.No)
          {
            Cancel = true;
          }
        }
    
        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }
    
        #region VSTO generated code
    
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
          this.Startup += new System.EventHandler(ThisAddIn_Startup);
          this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
      }
    }
    

     

    I hope it can help you,

     

    Have a good day,

     

    Tom


    Tom Xu [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, April 13, 2011 3:09 AM
    Moderator
  • Hi Tom,

    Thanks for looking at this.  I am writing this in VB.Net.  A colleague wrote the code for me in C#, and it worked as yours does.

    I've discovered that the events aren't being picked up properly when you use 'Handles Application.DocumentBeforePrint'.

    I have now changed all the application events to be hooked up using 'AddHandler' in ThisAddinStartup in VB and it works.

    An odd one, but it seems to be okay at the moment.

    Kelly

    • Marked as answer by Kel55 Wednesday, April 13, 2011 8:51 AM
    Wednesday, April 13, 2011 8:49 AM