ArticleDirectory
- Section 1
- Section 4
- Section 5
Prelude
T just graduated from college this year. It is very difficult to find a job when I graduate because I did not study hard at school. Fortunately, at ordinary times, the C language class helped girls write their homework and accumulated some experience, so they finally found a writeProgram. On the first day of work, he receives a task, adds a command bar to a plug-in of Word 2003, and adds a command bar button to it. The unfortunate little T, the miserable life starts from now on.
Section 1
Every newbie should thank Google. Without him, I really don't know how to deal with it. Small T is no exception. After looking for it, you don't have to make much effort. The basic small T can already create a command bar.CodeAlthough it is Ctrl + C, CTRL + V, it can be used.
Commandbar = Application. commandbars. Add ( " Testcommandbar " , Msobarposition. msobartop, False , True );
Commandbar. Visible= True;
After the command bar is completed, will the victory be far away? We have carefully started word several times. Make sure it is there each time, and there is only one. Mr. T was proud of himself and paid tribute to M $. It's a great company. The designed software is so scalable and easy to learn.
Continue to put the dog. In a few cases, the command bar button is also done.
Commandbarcontrol = Commandbar. Controls. Add (msocontroltype. msocontrolbutton, type. Missing, True );
Commandbarbutton = (Commandbarbutton) commandbarcontrol;
Commandbarbutton. Caption = " Testcommandbar " ;
Commandbarbutton. Style = Msobuttonstyle. msobuttoncaption;
Commandbarbutton. Visible = True ;
Well, what is type. Missing? Ah, I knew it would be easier to write with VBA. The next step is to respond to the click event.
Commandbarbutton. Click + = Delegate {MessageBox. Show ( " Hello " );};
My Kao! It was a success. Yes, you can. T walked to QA and installed the program for her. Happy to have lunch, ready to celebrate this rare opening.
Section 2
Return for lunch. QA reported a bug. This may be the first bug that Mr. T has handled since his career. Congratulations! Your happy life has officially begun. The bug is quite strange, that is, you don't stop. By a certain number of times, MessageBox will no longer come out. Mr. t thought, I'm Kao. What's the problem? Okay. Let me try it. Click here. Click twice. I will try again .... Hello? Play with me? No problem. So small t marks the bug report on Jira as can not reproduce. I am not happy with it, what is the bird QA.
It wasn't long before QA came to tell him that it was a re-occurrence. T looked at the reopen bug report and thought it was not a taste. Okay, I'll try more. Click n. Yeah? Really, what's going on? Put it on your dog. I searched for a long time and did not find anything useful. After spending nearly one afternoon between the network and the debugger, I finally found the cause of the problem that a miserable person mentioned in a blog. Correct writing is much more difficult:
Private _ Commandbarbuttonevents_clickeventhandler handler;
Private Commandbarbutton;
Private Void Initializecommandbar (Application)
{
Commandbar = Application. commandbars. Add ( " Testcommandbar " , Msobarposition. msobartop, False , True );
Commandbar. Visible = True ;
Commandbarcontrol = Commandbar. Controls. Add (msocontroltype. msocontrolbutton, type. Missing, True );
Commandbarbutton = (Commandbarbutton) commandbarcontrol;
Commandbarbutton. Caption = " Testcommandbar " ;
Commandbarbutton. Style = Msobuttonstyle. msobuttoncaption;
Commandbarbutton. Visible = True ;
Handler = New _ Commandbarbuttonevents_clickeventhandler (commandbarbutton_click );
Commandbarbutton. Click + = Handler;
}
Private VoidCommandbarbutton_click (commandbarbutton Ctrl,Ref BoolCanceldefault)
{
MessageBox. Show ("Hello");
GC. Collect ();
}
Well, even if the GC is forced, there will be no problem. After writing the code, t began to complain. It is not unreasonable for so many people to scold M $...
Section 3
The next morning. QA reported a bug again. Create a new document. In the newly created document, the button cannot be used properly. How is the first response of Xiao t possible? The code is the same. I didn't determine which document is the current one? Are they two command bars? But there is a problem with iron. The first suspect of small T is that the two Word documents are two word processes. What about the legendary concurrency problem? Suspicion was quickly denied. In most cases, word only has one process (hey, in most cases ...). What is the problem? Ah, the good mood in the morning ruined it. Put the dog on. Now we have no clue. How do you describe this phenomenon with keywords? It cannot be found. Wait until you get off work. After an unintentional attempt, the solution to the problem was discovered, that is, "one line of code ":
Commandbarbutton. Tag = " Testcommandbar " ;
My Kao is so simple ...... But the document is not written (msdn SINGS: don't tell you, don't tell you, Don't Tell You ~)
Section 4
It's not over yet. One day, BA tells Xiao t that the demand has changed. The customer said that this button could be added to the command bar I usually use. So many command bar I don't like. Okay, add it to the standard command bar. Just change a line of code. Hey, you know what it is called well-design:
Commandbar=Application. commandbars ["Standard"];
Well, it's good. Working properly. Why? Wait for me to try again. Shit! How come there are two test command bar buttons? I have another one, Kao... Speechless. Why is the previous Code okay? Let's judge before adding the button. If the previous button exists, it will not be added.
Foreach (Commandbarcontrol CTRL In Commandbar. Controls)
{
If (CTRL. Tag. Equals ( " Testcommandbar " ))
{
Return ;
}
}
T feels uncomfortable. I didn't mean temporary when I added the button:
Commandbarcontrol = Commandbar. Controls. Add (msocontroltype. msocontrolbutton, type. Missing, True );
Who made you believe in msdn? Really Naive silly child. It turns out that temporary's behavior is extremely strange.
Section 5
Good days seem to come. The plug-in has always been good on the customer's machine. Version 2.0 is about to be released. This problem occurs. QA found that after V1.0 is uninstalled, the button is still there. Well, I know this problem, too. That's the temporary problem. It seems that it cannot be added twice, but you have to consider how to delete it. It is better to delete the program every time it exits, saving you a long night's dream.
Applicationevents4_event events = Application;
Events. Quit + = Delegate {
Commandbar = Application. commandbars [ " Standard " ];
Commandbar. Reset ();
};
But... No effect. Sometimes, there is no explanation. It seems that only one uninstaller can be written for uninstallation.
Public Class Program
{
Public Static Void Main ( String [] ARGs)
{
Application = New Application ();
Application. Visible = True ;
Application. commandbars [ " Standard " ]. Reset ();
}
}
T couldn't help asking why the same code has different effects in different places?
VOICEOVER: Well, this is a good question ~~ What's amazing about official office plug-in development.
At this time, our small T is no longer a cainiao. But do you think you already know everything? Hey hey...
Section 6
Customers who are difficult to serve complained again. Why cannot this button be customized. Oh, our great word 2003 has powerful user-defined functions. The button written by T must be placed on the standard command bar and dragged to another place. A new button will be created next time. The UD staff of the company saw en, which is very poor in availability. No, you have to change it for me. Okay, T, I thought, it's really hard. The improved code is no longer mandatory in the standard command bar.
Commandbarcontrol foundcontrol = Application. commandbars. findcontrol (type. Missing, type. Missing, " Testcommandbar " , Type. Missing );
If (Foundcontrol ! = Null )
{
Return ;
}
Hey, the cycle is saved.
Section 7
T has done a good job in the previous project. As a result, the company thinks that little T is doing this well, so the second project is also a plug-in for Word 2003. Mr. T summarized his previous experience and began to pay attention to the issue of uninstallation. After countless endless nights, Mr. T finally found a more reliable way to use Word APIs. That is, the word API is not used. The change to the command bar of Word 2003 is actually stored on the template and document. So little T has added a lot of code into one sentence:
Object Install = True ;
Application. addins. Add ( @" C: \ my_template.dot " , Ref install );
The function parameter of this type of ref object is actually... (Speechless, [ref object] is even better ...). Then, you can use findcontrol to listen to events. Qi Huo, Xiao t finally felt that he had reached the door.
Section 8
The requirements for a new project include separating the button States in different document windows. Just as if you press B, the text currently entered is in bold. This makes small t hard. However, the undead little T will survive. Actually, there are few lines of code. The key is that you don't know. Word application has a hidden attribute (not actually hidden, but not mentioned in the document. The official saying is it is for internal use ...) Customizationcontext. Therefore:
Private Void Handlenewdocument (document)
{
Application. customizationcontext = Document;
Initializecommandbar (application );
}
T is really proud of it this time. I can do it all. It's just a genius. PM, who has many years of development experience, came over and clicked on the word shortcut. In an instant, three word windows are started. Half a minute later, I found two windows with command bar and one with no. The error log indicates that the word API throws a comexception... Small T, suddenly felt helpless, word error, what can I do? Remove the customizationcontext line. Everything is normal...
Conclusion
The king of the command bar is to use. Dot to add customizationcontext. But to realize this, it is impossible to write down two or three projects. The prototype of small T is me. I am writing two Outlook 2007 plug-ins, one Outlook 2003 plug-in, one Excel plug-in, N PowerPoint plug-ins, and one word plug-in, then I realized this when I wrote a word plug-in... Maybe I am too stupid.
the above is only a small part of the story. There are still many stories about command bar, such as how to cancel the built-in command bar button action, how to use onaction, and the relationship between command bar and accessibility API, and the famous outlook + word + command bar issues. If my physical strength is limited, I will not continue to talk about it. Behind every story is the sadness, tears, and youth of countless people. M $ is still easy to learn, difficult to learn, and unreasonable. The key is that you can only get a bunch of "Knowledge" instead of real skills ". These "Knowledge" is only useful for office development. Without office development, there is no value at all. I would like to advise you to leave your company early if you cannot change your project. Don't waste your time on the stupid office platform. If you think of it as a platform.