In this series of articles, we will use some small examples to gradually learn VSTO for excel, a technology that has been around for a long time, I rarely find more comprehensive or specific articles, except MSDN.
I started this series on my blog. I wanted to consolidate my technical knowledge, and I also wanted to share my technical experiences with you, hope to gain more valuable experience!
The first part of this series has the following articles:
- Getting Start (Add-In level)
- Simple uidesign (Robbin)
- Batch operation (with an example of qq contact export, Add-In level) J
- Rich User-Defined UI interaction (Add-In level)
- Deploy the Add-In component to the client
In the second phase, we will make flexible modifications and adjustments based on the situation in the first phase. You are welcome to make a decision!
Yes. What is VSTO? In fact, many of my friends, including MSDN, have provided a very good definition. Here I will not go to it. Please refer to the following URL for reference:
Reference: http://blog.csdn.net/v_jzho/archive/2007/09/29/1806904.aspx
Reference (Official comparison): http://msdn.microsoft.com/zh-cn/library/zcfbd2sk (v = VS.80). aspx
Before learning about a development object, it is very important to understand the object model. So again, as shown in the following example, we have introduced the object model from MSDN, all of which are in Chinese, I believe everyone can understand it. Please study with patience. After all, this is very important in the future!
Reference: http://msdn.microsoft.com/zh-cn/library/wss56bz7 (VS.80). aspx
Now that we are working on development, we need to build the development environment. The following is an introduction from MSDN. Here is a brief introduction, because when VS08 is installed and Office2007 is installed, this development environment is basically complete.
Install design tools on Development computers
- Install the Office, including the main interoperability assembly. For more information, see How to: Install the main Office interoperability assembly.
- Install Office 2003 SP1 or later. For more information, see Office 2003 Service Pack 1 website (http://office.microsoft.com/zh-cn/officeupdate/default.aspx ).
- Install VSTO 2005 or Visual Studio Team System, including the programming language Visual Basic or Visual C #.
By default, VSTO 2005 is installed with Visual Studio Team System When Visual Basic or Visual C # is installed.
- If Visual Studio is an English version but uses non-English Windows Settings, you can install the Visual Studio Tools for Office Language Pack to view the message of the Visual Studio Tools for Office Runtime Library in a Windows language. Visual Studio Tools for Office language packs can be downloaded from Microsoft (http://www.microsoft.com/downloads/search.aspx? Displaylang = zh-cn. This language pack is automatically installed in non-English versions of Visual Studio Tools for Office.
If you install Visual Studio Team System before installing Office, you must repair the Visual Studio Tools for Office Runtime Library by running the runtime Installation File (VSTOR.exe. For more information, see how to install the Visual Studio Tools for Office Runtime Library.
- The first Add-In project:
Well, after a lot of preparations have been completed, I think everyone would like to reveal their skills. It is not very difficult to create an Office project. For example, create an Office Excel Add-In (external program) Project Based on c # language.
The following machine does not have the 03 office, but you can also use the 03 template for development. For convenience, I use the 07 template (though required. net 3.5 framework ......) Then, we can see that the project structure is as follows:
What is ThisAddIn. CS? Let me hear about it.
This ThisAddIn file is similar to an external program (AddIn) entry. All important operations, including object acquisition, must be performed here.
It is clear in MSDN:
When you create an Excel add-in project, Visual Studio generates a class, called ThisAddIn, which is the foundation of your solution. this class provides a starting point for writing your code, and it also exposes the object model of Excel to your add-in.
Haha, it's no longer cool. Continue with our code. In the simplest way, we can see that ThisAddIn has a StartUp method. In this method, we mainly write some processing functions when the external program is loaded, for example, registering an event or initializing an object is as follows:
1 // register an event
2 this. Application. WorkbookBeforeSave + = new Microsoft. Office. Interop. Excel. AppEvents_WorkbookBeforeSaveEventHandler (Application_WorkbookBeforeSave );
We have registered a Custom Event, which causes the plug-in to respond to us before saving the event. Then, in the ThisAddIn class, add the implementation function code of the event just registered as follows:
Code
/// <Summary>
/// Save the previous function code
/// </Summary>
/// <Param name = "Wb"> Workbook parameter </param>
/// <Param name = "SaveAsUI"> Save options </param>
/// <Param name = "Cancel"> Cancel? </Param>
Void Application_WorkbookBeforeSave (Microsoft. Office. Interop. Excel. Workbook Wb, bool SaveAsUI, ref bool Cancel)
{
Excel. Worksheet activeWorksheet = (Excel. Worksheet) Application. ActiveSheet );
Excel. Range firstRow = activeWorksheet. get_Range ("A1", missing );
FirstRow. EntireRow. Insert (Excel. XlInsertShiftDirection. xlShiftDown, System. Type. Missing );
Excel. Range newFirstRow = activeWorksheet. get_Range ("A1", missing );
NewFirstRow. Value2 = "This text was added by using code ";
}
So far, a simple Add-In program has been completed. Let's start F5 together!
When debugging the program, open a blank working thin, and click Save. Note that the A1 value is filled. Indicates that the plug-in is running normally. This is the end of the first article. I hope you will have some gains. (Every article should be as short and competent as possible)
So far, a simple Add-In program has been completed. Let's start F5 together!
When debugging the program, open a blank working thin, and click Save. Note that the A1 value is filled. Indicates that the plug-in is running normally. This is the end of the first article. I hope you will have some gains. (Every article should be as short and competent as possible) -- Don't go away, there will be a small Tips later ~~~
Small Tips:
After each debugging, we 'd better develop a good habit of recycling! Otherwise, these plug-ins may be inexplicably faulty. I cannot tell you the specific reason, so let's talk about recycling habits first.
How to recycle it? Simple: "Clear Solution" and "Clear Project". In this case, OK!