Event programming in office automation development

Source: Internet
Author: User

This problem was raised by a csdn user. I personally think it is necessary to summarize it. The problem is probably like this: when using VB or VB.net for office automation development, you sometimes have to know what operations the user has done, such as switching open files and disabling ExcelProgram, Changed the content of the cell, and so on. In fact, there are a large number of events in the office object model, which can be precisely controlled to cell changes, Sheet switching, file opening and closing, and macro loading. By using these events, we can see whether the opened office program is closed.

The following is an Excel example (taking VB.net as an example): Create a project, add a reference to excel, and declare the following variables in form:

Dim withevents objexcel as Excel. Application
Dim objworkbook as Excel. Workbook

Open or activate Excel:
Objexcel = new excel. Application
Objworkbook = objexcel. workbooks. Add
Objexcel. Visible = true

Then you can respond to the Excel Object event, for example:

Private   Sub objexcel_workbookbeforeclose () Sub Objexcel_workbookbeforeclose ( Byval WB As Excel. Workbook, Byref Cancel As   Boolean ) Handles Objexcel. workbookbeforeclose
Me . Lstevent. Items. Add ( " To close: "   & WB. Name)
End sub

Private   Sub objexcel_workbookbeforesave () Sub Objexcel_workbookbeforesave ( Byval WB As Excel. Workbook, Byval Saveasui As   Boolean , Byref Cancel As   Boolean ) Handles Objexcel. workbookbeforesave
Me . Lstevent. Items. Add ( " Save: "   & WB. Name)
End sub

Private   Sub objexcel_workbookopen () SubObjexcel_workbookopen (ByvalWBAsExcel. workbook)HandlesObjexcel. workbookopen
Me. Lstevent. Items. Add ("Open:" &WB. Name)
End sub

Private   Sub objexcel_sheetchange () Sub Objexcel_sheetchange ( Byval Sh As   Object , Byval Target As Excel. Range) Handles Objexcel. sheetchange
Me . Lstevent. Items. Add ( " Changed: "   &   Ctype (Sh, Excel. worksheet). Name &   " Of "   & Target. Address)
End sub

The running interface is as follows:

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.