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: