none
word viewer using c sharp RRS feed

  • Question

  • i want to disable menu bar (open,open for editing,views,windows,help) in wordviewer. when i open my file in wordviewer.this above feature disable or not workable.NOTE dont  disable others features of menubar. i have my code.YOU send me changes of code with respect to my code and my code is

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Diagnostics;
    using System.Threading;
    using System.Windows.Automation;
    using System.Windows.Threading;
    using System.Windows.Forms;

    using Condition = System.Windows.Automation.Condition;

    namespace UITest
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow
        {
            public MainWindow()
            {
                InitializeComponent();
            }      
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                Thread automateThread = new Thread(new ThreadStart(Automate));
                automateThread.Start();
            }

            private void Automate()
            {
                Process myProcess = new Process();
                myProcess.StartInfo.FileName = @"C:\Program Files\Microsoft Office\OFFICE11\WORDVIEW.EXE";
                //myProcess.StartInfo.FileName = @"notepad.exe";

                myProcess.StartInfo.Arguments = @"C:\Users\ABDANI\Downloads\cvforIT.doc";
              
                myProcess.Start();
                Thread.Sleep(2000);

                #region RootElement

                LogMessage("Getting RootElement...");
                AutomationElement rootElement = AutomationElement.RootElement;
                if (rootElement == null)
                {
                    WriteLogError();
                    return;
                }
                LogMessage("OK." + Environment.NewLine);
                #endregion

                #region WordViewerWindow
              
                LogMessage("Searching for Word Viewer Window...");
                Condition condition = new PropertyCondition(AutomationElement.ClassNameProperty, "OpusApp");
                AutomationElement appElement = rootElement.FindFirst(TreeScope.Children, condition);
                if(appElement ==null)
                {
                    WriteLogError();
                    return;
                }            
                LogMessage("OK " + Environment.NewLine);
                #endregion

                #region MenuBar

                LogMessage("Searching for Menu Bar...");
                Condition menuBarCondition = new PropertyCondition(AutomationElement.ClassNameProperty, "MsoCommandBar");
               
                // Find the menuBar, which is a child of the main window.
                AutomationElement menuBarElement = appElement.FindFirst(TreeScope.Descendants, menuBarCondition);

                if (menuBarElement == null)
                {
                    WriteLogError();
                    return;
                }           
                     
                LogMessage("OK " + Environment.NewLine);
                #endregion

                #region EditMenu
                LogMessage("Searching for Edit Menu...");
                // Create the AndCondition to find the File menu.
                AndCondition fileMenuFind = new AndCondition(
                    new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.MenuItem),
                    new PropertyCondition(AutomationElement.NameProperty,"Edit"),
                    Automation.ControlViewCondition);

                // Find the File menu.
                AutomationElement fileMenuElement =
                    menuBarElement.FindFirst(TreeScope.Children, fileMenuFind);

                if (fileMenuElement == null)
                {
                    WriteLogError();
                    return;
                }
                LogMessage("OK " + Environment.NewLine);           

                #endregion

               

                #region Expand Edit Menu
                LogMessage("Expand the Edit Menu");

                // Expand the Edit Menu.
                InvokePattern editMenuInvokePattern =
                    fileMenuElement.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
                editMenuInvokePattern.Invoke();
                LogMessage("OK " + Environment.NewLine);
                #endregion

                #region Go To...
                LogMessage("Find Go To Menu");       
                AutomationElementCollection goToMenuElement =
                    FindByMultipleConditions(fileMenuElement);

                foreach (AutomationElement i in goToMenuElement)
                {
                    if (i.Current.Name == "Go To...")
                    {
                        LogMessage(i.Current.Name.ToString() + "is Find..." + Environment.NewLine);

                        #region Expand Edit Menu
                        LogMessage("Invoke the Go To Menu");

                        // Expand the Go To Menu.
                        InvokePattern goToInvokePattern =
                            i.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
                        goToInvokePattern.Invoke();
                        LogMessage("OK " + Environment.NewLine);
                        #endregion

                        // RichEdit20W
                        #region Set Value of GoTo
                        PropertyCondition cond = new PropertyCondition(AutomationElement.ClassNameProperty, "RichEdit20W");
                        AutomationElement textProvider = rootElement.FindFirst(TreeScope.Descendants, cond);
                        TextPattern textpatternPattern = textProvider.GetCurrentPattern(TextPattern.Pattern) as TextPattern;

                        if (textpatternPattern == null)
                        {                       
                            return;
                        }
                        else
                        {
                            textpatternPattern.DocumentRange.Select();
                            SendKeys.SendWait("10");                       
                        }
                        Thread.Sleep(1000);
                        #endregion

                        // Simulate press "Enter".
                        SendKeys.SendWait("~");                   
                        // Invoke the menu to get the Open dialog.
                        Thread.Sleep(1000);
                    }
                }
                #endregion                            
            }

            private AutomationElementCollection FindByMultipleConditions(AutomationElement elementWindowElement)
            {
                Condition conditions = new PropertyCondition(AutomationElement.ControlTypeProperty,
                      ControlType.MenuItem);

                // Find all children that match the specified conditions.
                AutomationElementCollection elementCollection =
                    elementWindowElement.FindAll(TreeScope.Descendants, conditions);
                return elementCollection;
            }   

            private void DisplayLogMessage(string message)
            {
                txtLogs.Text += message;
            }

            private delegate void SetMessageCallback(string message);

            private void LogMessage(string message)
            {
                this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new SetMessageCallback(DisplayLogMessage), message);
            }

            private void WriteLogError()
            {
                LogMessage("ERROR." + Environment.NewLine);
            }      
        }
    }


    qasim
    • Edited by qasimabdani Saturday, January 21, 2012 5:47 PM
    Saturday, January 21, 2012 5:43 PM

Answers

All replies

  • The Word Viewer does not expose an API and is not part of the VSTO technology.
    Cindy Meister, VSTO/Word MVP
    Sunday, January 22, 2012 7:06 AM
    Moderator
  • Give me another solution.what i will do?i want to block or disable editing ,selecting options in wordviewer?using c# i used word automation api in my code using System.Windows.Automation;


    qasim
    Saturday, January 28, 2012 4:32 AM