Create a commandbar in word and display the winform

Source: Internet
Author: User
Some time ago, you had to select a text in a Word document and then add a button in the right-click menu to bring up a winform, two problems encountered in the actual process are recorded. the procedure is as follows:
  1. Create a new one in vs2005Shared add-in
    Project, step by step next, to select an application host, only select Microsoft Word, and then continue to next to complete, a wordaddin project will be automatically created and installed with the project.
  2. Add code to the onconnection method in connect. CS of the project. This is the startup entry. This method is automatically called when word is opened. The following code is used:Public void onconnection (object application, extensibility. ext_connectmode connectmode, object addininst, Ref system. array custom) <br/>{< br/> applicationobject = application; <br/> addininstance = addininst; <br/> word. application APP = application as word. application; <br/> createcontextmenu (APP); <br/>}
  3. Createcontextmenu (APP) method. Note that the commandbarbutton added here must be global. Otherwise, the click event will only trigger 1 ~ 2 times, which will not be triggered later:
    Private Office. commandbarbutton showbutton; <br/> Private Static office. commandbarbutton showtermbutton; <br/> Public static void createcontextmenu (word. applicationclass APP) <br/>{< br/> Office. commandbars = (office. commandbars) app. commandbars; <br/> Object missing = system. reflection. missing. value; <br/> Office. commandbar listsbar = commandbars ["Lists"]; <br/> If (listsbar! = NULL) <br/>{< br/> showbutton = (office. commandbarbutton) listsbar. findcontrol (office. msocontroltype. msocontrolbutton, missing, constants. testforviewparaphrase, true, true); <br/> If (showbutton = NULL) <br/> showbutton = (office. commandbarbutton) listsbar. controls. add (office. msocontroltype. msocontrolbutton, missing, missing, 1, missing); <br/> showbutton. caption = constants. testforviewparaphrase; <br/> showbutton. style = Microsoft. office. core. msobuttonstyle. msobuttoncaption; <br/> showbutton. visible = true; <br/> showbutton. tag = constants. testforviewparaphrase; <br/> showbutton. click + = new Microsoft. office. core. _ commandbarbuttonevents_clickeventhandler (showbutton_click); <br/> showtermbutton = (office. commandbarbutton) listsbar. findcontrol (office. msocontroltype. msocontrolbutton, missing, constants. textforaddtermnet, true, true); <br/> If (showtermbutton = NULL) <br/> showtermbutton = (office. commandbarbutton) listsbar. controls. add (office. msocontroltype. msocontrolbutton, missing, missing, 1, missing); <br/> showtermbutton. caption = constants. textforaddtermnet; <br/> showtermbutton. style = Microsoft. office. core. msobuttonstyle. msobuttoncaption; <br/> showtermbutton. visible = true; <br/> showtermbutton. tag = constants. textforaddtermnet; <br/> showtermbutton. click + = new Microsoft. office. core. _ commandbarbuttonevents_clickeventhandler (showtermbutton_click); <br/>}< br/>}
  4. A form will be displayed in the showtermbutton_click event. Special processing is to make the pop-up form automatically placed on the open Word and find a feasible processing method on the Internet for a long time, is Reference http://social.msdn.microsoft.com/forums/en-US/vsto/thread/19539460-14ce-4ea0-9a53-50d38a4eb3b0/
    The Code is as follows:
    Void showtermbutton_click (Microsoft. office. core. commandbarbutton Ctrl, ref bool canceldefault) <br/>{< br/> try <br/>{< br/> word. range = appclass. activewindow. selection. range; <br/> If (string. isnullorempty (range. text) <br/> return; <br/> form = new form (); <br/> form. showdialog (New wordwin32window (appclass); <br/> form. dispose (); <br/>}< br/> catch (exception ex) <br/>{< br/> log. error ("show form error" + ex); <br/>}< br/>}


    The wordwin32window class code is as follows:
    /// <Br/> // word example code by H. obertanner http://www.outlooksharp.de <br/> // created 2008 with Office 2007 and vsto 3.0 <br/> // free for non ‑cial use. <br/> using system; <br/> using system. windows. forms; <br/> using system. runtime. interopservices; <br/> using system. collections. generic; <br/> using system. threading; <br/> using system. reflection; <br/> using system. diagnostics; <br/> using SY Stem. text; <br/> namespace Co. wordlib <br/> {<br/> /// <summary> <br/> // This class retrieves the iwin32window from the current active word window. <br/> // This cocould be used to set the parent for Windows Forms and messageboxes. <br/> /// </Summary> <br/> // <example> <br/> // wordwin32window parentwindow = new wordwin32window (thisaddin. wordapplication); <br/> // MessageBox. show (parentwind Ow, "This MessageBox doesn't go behind word !!! "," Attention! ", Messageboxbuttons. OK, messageboxicon. question); <br/> /// </example> <br/> public class wordwin32window: iwin32window <br/>{< br/> /// <summary> <br/> // returns a handle to the desktop-window <br/> /// </ summary> <br/> // <returns> the handle </returns> <br/> [dllimport ("USER32")] <br/> Private Static extern intptr getasktopwindow (); <br/> /// <summary> <br/> // finds a window-handle by I TS name or classname <br/> /// </Summary> <br/> /// <Param name = "hwndparent"> the parent windows-handle </param> <br/> // <Param name = "hwndchildafter"> start the search after the given windows-handle </param> <br/> // <Param name = "lpszclass "> the classname to search for </param> <br/> // <Param name =" lpszwindow "> the window caption to search for </param> <br/> /// <returns> <returns the windows-handle o R intptr. zero </returns> <br/> [dllimport ("USER32")] <br/> Private Static extern intptr find1_wex (intptr hwndparent, intptr hwndchildafter, string lpszclass, string lpszwindow ); <br/> /// <summary> <br/> // gets the title of a window. <br/> /// </Summary> <br/> /// <Param name = "hwnd"> the Windows handle. </param> <br/> // <Param name = "lpstring"> A pointer to a stringbuilder-object. </param> <br/> /// <Param name = "nmaxcount"> MAX num of chars to get. </param> <br/> [dllimport ("USER32", charset = charset. auto)] <br/> Private Static extern int getwindowtext (intptr hwnd, stringbuilder lpstring, int nmaxcount ); <br/> # region iwin32window members <br/> // <summary> <br/> // This holds the window handle for the found window. <br/> // </Summary> <br/> intptr _ prop whandle = intptr. zero; <br/> /// <Summary> <br/> // The <B> handle </B> of the word windowobject. <br/> // </Summary> <br/> Public intptr handle <br/> {<br/> get {return _ blank whandle ;} <br/>}< br/> # endregion <br/> // <summary> <br/> // The <B> wordwin32window </B> class cocould be used to get the parent iwin32window for Windows. forms and messageboxes. <br/> /// </Summary> <br/> /// <Param name = "wordapp"> the word applicationo Bject. </param> <br/> Public wordwin32window (Object wordapp) <br/> {<br/> // how to find my word Windows-handle... <br/> string oldcaption = (string) wordapp. getType (). invokemember ("caption", bindingflags. getproperty, null, wordapp, null); <br/> // Unique Identifier <br/> string newcaption = guid. newguid (). tostring (); <br/> // set a new caption to the word window <br/> wordapp. getType (). invokemembe R ("caption", bindingflags. setproperty, null, wordapp, new object [] {newcaption}); <br/> // get the desktop into whandle <br/> intptr hdesktop = getdesktopwindow (); <br/> // find all word Windows-handles <br/> // find the first instance <br/> intptr hwnd = find1_wex (hdesktop, intptr. zero, "opusapp", null); <br/> stringbuilder windowtext = new stringbuilder (255); <br/> while (hwnd! = Intptr. zero) <br/>{< br/> // get the window-caption <br/> getwindowtext (hwnd, windowtext, 255); <br/> If (windowtext. tostring (). endswith (newcaption) <br/>{< br/> // gotcha <br/> _ ?whandle = hwnd; <br/> break; <br/>}< br/> // next-starting after the last found window <br/> hwnd = find1_wex (hdesktop, hwnd, "opusapp", null ); <br/>}< br/> // reset the caption <br/> wordapp. getType (). invokemember ("caption", bindingflags. putdispproperty, null, wordapp, new object [] {oldcaption}); <br/>}< br/>}

  5. Another button event, showing bubbles:
    Void showbutton_click (Microsoft. office. core. commandbarbutton Ctrl, ref bool canceldefault) <br/>{< br/> try <br/>{< br/> Office. balloon balloon = appclass. assistant. newballoon; <br/> balloon. balloontype = Office. msoballoontype. msoballoontypenumbers; <br/> balloon. button = Office. msobuttonsettype. msobuttonsetok; <br/> balloon. heading = "view text"; <br/> balloon. TEXT = appclass. activewindow. selection. text; <br/> balloon. show (); <br/>}< br/> catch (exception ex) <br/>{< br/>}< br/>}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.