Beispiel #1
0
 private void acitvateWordApp()
 {
     try
     {
         _wordApp.Visible = true;
         _wordApp.Activate();
     }
     catch (Exception ex)
     {
         MessageBox.Show("Error: do not load the document into the control until the parent window is shown! {0}", ex.ToString());
     }
 }
Beispiel #2
0
        /// <summary>
        /// Loads a document into the control
        /// </summary>
        /// <param name="t_filename">path to the file (every type word can handle)</param>
        public void LoadDocument(string t_filename)
        {
            deactivateevents = true;
            filename         = t_filename;

            if (wd == null)
            {
                wd = new ApplicationClass();
            }
            try
            {
                wd.CommandBars.AdaptiveMenus             = false;
                wd.DocumentBeforeClose                  += new ApplicationEvents4_DocumentBeforeCloseEventHandler(OnClose);
                wd.ApplicationEvents4_Event_NewDocument += new ApplicationEvents4_NewDocumentEventHandler(OnNewDoc);
                wd.DocumentOpen += new ApplicationEvents4_DocumentOpenEventHandler(OnOpenDoc);
                wd.ApplicationEvents4_Event_Quit += new ApplicationEvents4_QuitEventHandler(OnQuit);
            }
            catch { }

            if (document != null)
            {
                try
                {
                    object dummy = null;
                    wd.Documents.Close(ref dummy, ref dummy, ref dummy);
                }
                catch { }
            }

            if (wordWnd == 0)
            {
                wordWnd = FindWindow("Opusapp", null);
            }
            if (wordWnd != 0)
            {
                SetParent(wordWnd, this.Handle.ToInt32());

                object fileName    = filename;
                object newTemplate = false;
                object docType     = 0;
                object readOnly    = false;
                object isVisible   = true;
                object missing     = System.Reflection.Missing.Value;
                try
                {
                    if (wd == null)
                    {
                        throw new WordInstanceException();
                    }
                    if (wd.Documents == null)
                    {
                        throw new DocumentInstanceException();
                    }
                    if (wd != null && wd.Documents != null)
                    {
                        document = wd.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
                    }
                    //document = wd.Documents.Add(ref fileName, ref newTemplate, ref docType, ref isVisible);

                    if (document == null)
                    {
                        throw new ValidDocumentException();
                    }
                }
                catch { }
                try
                {
                    wd.ActiveWindow.DisplayRightRuler        = false;
                    wd.ActiveWindow.DisplayScreenTips        = false;
                    wd.ActiveWindow.DisplayVerticalRuler     = false;
                    wd.ActiveWindow.DisplayRightRuler        = false;
                    wd.ActiveWindow.ActivePane.DisplayRulers = false;
                    wd.ActiveWindow.ActivePane.View.Type     = WdViewType.wdPrintView;
                    //wd.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdPrintView;//wdWebView; // .wdNormalView;
                }
                catch { }

                /// Code Added
                /// Disable the specific buttons of the command bar
                /// By default, we disable/hide the menu bar
                /// The New/Open buttons of the command bar are disabled
                /// Other things can be added as required (and supported ..:) )
                /// Lots of commented code in here, if somebody needs to disable specific menu or sub-menu items.
                ///
                CommandBars bars = wd.ActiveWindow.Application.CommandBars;
                if (showToolBar)
                {
                    //object type = "Standard";
                    bars.FindControl(Microsoft.Office.Core.MsoControlType.msoControlButton, 3, missing, missing).Enabled   = false;
                    bars.FindControl(Microsoft.Office.Core.MsoControlType.msoControlButton, 23, missing, missing).Enabled  = false;
                    bars.FindControl(Microsoft.Office.Core.MsoControlType.msoControlButton, 109, missing, missing).Enabled = false;
                    bars["Standard"].Controls[1].Enabled = false;
                    //type = "Print Preview";
                    bars["Print Preview"].Controls[9].Enabled = false;
                }
                else
                {
                    int counter = bars.Count;
                    for (int i = 1; i <= counter; i++)
                    {
                        bars[i].Enabled = false;
                    }
                }
                if (!showMenuBar)
                {
                    bars["Menu Bar"].Enabled = false;
                }

                //int counter = wd.ActiveWindow.Application.CommandBars.Count;
                //for (int i = 1; i <= counter; i++)
                //{
                //    try
                //    {
                //        String nm = wd.ActiveWindow.Application.CommandBars[i].Name;
                //        if (showToolBar)
                //        {
                //            if (nm == "Standard")
                //            {
                //                //nm=i.ToString()+" "+nm;
                //                //MessageBox.Show(nm);
                //                int count_control = wd.ActiveWindow.Application.CommandBars[i].Controls.Count;
                //                for (int j = 1; j <= 3; j++)
                //                {
                //                    //MessageBox.Show(wd.ActiveWindow.Application.CommandBars[i].Controls[j].ToString());
                //                    wd.ActiveWindow.Application.CommandBars[i].Controls[j].Enabled = false;
                //                }
                //            }
                //            if (nm == "Print Preview")
                //            {
                //                int count_control = wd.ActiveWindow.Application.CommandBars[i].Controls.Count;
                //                for (int j = 1; j <= count_control; j++)
                //                {
                //                    if (wd.ActiveWindow.Application.CommandBars[i].Controls[j].TooltipText == "¹Ø±ÕÔ¤ÀÀ")
                //                        //MessageBox.Show(wd.ActiveWindow.Application.CommandBars[i].Controls[j].ToString());
                //                        wd.ActiveWindow.Application.CommandBars[i].Controls[j].Enabled = false;
                //                }
                //            }
                //        }
                //        else
                //        {
                //            wd.ActiveWindow.Application.CommandBars[i].Enabled = false;
                //        }
                //        if (nm == "Menu Bar" && !this.showMenuBar)
                //        {
                //            //To disable the menubar, use the following (1) line
                //            wd.ActiveWindow.Application.CommandBars[i].Enabled = false;

                //            /// If you want to have specific menu or sub-menu items, write the code here.
                //            /// Samples commented below
                //            //							MessageBox.Show(nm);
                //            //int count_control=wd.ActiveWindow.Application.CommandBars[i].Controls.Count;
                //            //MessageBox.Show(count_control.ToString());
                //            /*
                //            for(int j=1;j<=count_control;j++)
                //            {
                //                /// The following can be used to disable specific menuitems in the menubar
                //                /// wd.ActiveWindow.Application.CommandBars[i].Controls[j].Enabled=false;
                //                //MessageBox.Show(wd.ActiveWindow.Application.CommandBars[i].Controls[j].ToString());
                //                //MessageBox.Show(wd.ActiveWindow.Application.CommandBars[i].Controls[j].Caption);
                //                //MessageBox.Show(wd.ActiveWindow.Application.CommandBars[i].Controls[j].accChildCount.ToString());
                //                ///The following can be used to disable some or all the sub-menuitems in the menubar
                //                ////Office.CommandBarPopup c;
                //                ////c = (Office.CommandBarPopup)wd.ActiveWindow.Application.CommandBars[i].Controls[j];
                //                ////
                //                ////for(int k=1;k<=c.Controls.Count;k++)
                //                ////{
                //                ////	//MessageBox.Show(k.ToString()+" "+c.Controls[k].Caption + " -- " + c.Controls[k].DescriptionText + " -- " );
                //                ////	try
                //                ////	{
                //                ////		c.Controls[k].Enabled=false;
                //                ////		c.Controls["Close Window"].Enabled=false;
                //                ////	}
                //                ////	catch
                //                ////	{
                //                ////
                //                ////	}
                //                ////}
                //                    //wd.ActiveWindow.Application.CommandBars[i].Controls[j].Control	 Controls[0].Enabled=false;
                //                }
                //                */
                //        }

                //        nm = "";
                //    }
                //    catch (Exception ex)
                //    {
                //        MessageBox.Show(ex.ToString());
                //    }
                //}
                // Show the word-document
                try
                {
                    wd.Visible = true;
                    wd.Activate();
                    SetWindowPos(wordWnd, this.Handle.ToInt32(), 0, 0, this.Bounds.Width, this.Bounds.Height, SWP_NOZORDER | SWP_NOMOVE | SWP_DRAWFRAME | SWP_NOSIZE);
                    //Call onresize--I dont want to write the same lines twice
                    OnResize();
                }
                catch
                {
                    //MessageBox.Show("Error: do not load the document into the control until the parent window is shown!");
                }
                /// We want to remove the system menu also. The title bar is not visible, but we want to avoid accidental minimize, maximize, etc ..by disabling the system menu(Alt+Space)
                try
                {
                    int hMenu = GetSystemMenu(wordWnd, false);
                    if (hMenu > 0)
                    {
                        int menuItemCount = GetMenuItemCount(hMenu);
                        RemoveMenu(hMenu, menuItemCount - 1, MF_REMOVE | MF_BYPOSITION);
                        RemoveMenu(hMenu, menuItemCount - 2, MF_REMOVE | MF_BYPOSITION);
                        RemoveMenu(hMenu, menuItemCount - 3, MF_REMOVE | MF_BYPOSITION);
                        RemoveMenu(hMenu, menuItemCount - 4, MF_REMOVE | MF_BYPOSITION);
                        RemoveMenu(hMenu, menuItemCount - 5, MF_REMOVE | MF_BYPOSITION);
                        RemoveMenu(hMenu, menuItemCount - 6, MF_REMOVE | MF_BYPOSITION);
                        RemoveMenu(hMenu, menuItemCount - 7, MF_REMOVE | MF_BYPOSITION);
                        RemoveMenu(hMenu, menuItemCount - 8, MF_REMOVE | MF_BYPOSITION);
                        //DrawMenuBar(wordWnd);
                    }
                }
                catch { };
                this.Parent.Focus();
            }
            deactivateevents = false;
        }