Search code examples
c#ms-wordvstoword-addinsword-interop

MS Word AddIn Catch Document Zoom change


I am developing word VSTO addin and iam trying to detect an event when user change the zoom level either by changing slider at bottom right corner of word document or by using keyboard and mouse. but i didn't get success.

Is there any way to fire or detect an event when user change zoom level(zoom out/in) in word active document.

If any one have idea or any alternative to this then please suggest.

Thanks.


Solution

  • There is no built-in event in the Word Object model for zoom change. You can use the following approach to set a timer and look for changes in the Zoom property:

        // Add these 2 class members.
        System.Timers.Timer _zoomTimer = new System.Timers.Timer();
        public double _lastZoomValue = 100; // default zoom
    
    
        // in the Startup function of the addin, set the timer.
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            _zoomTimer.Elapsed += OnZoomChanged;
            _zoomTimer.Interval = 1000;
            _zoomTimer.Start();
        }
    
        // dispose the timer
        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            _zoomTimer.Dispose();
        }
    
        // check if there is active window.
        private void OnZoomChanged(object source, ElapsedEventArgs e)
        {
            _zoomTimer.Stop();
    
            var app = this.Application;
            if (app!=null && app.ActiveWindow != null && app.ActiveWindow.Zoom != _lastZoomValue)
            {
                _lastZoomValue = app.Application.ActiveWindow.Zoom;
                // DO SOMETHING
            }
    
            _zoomTimer.Start();
        }