HOW TO: ?????? ??????? ?? Excel ???????? Visual Basic .NET

?????? ????????? ?????? ?????????
???? ???????: 302814 - ??? ???????? ???? ????? ????? ??? ???????.
????? ???? | ?? ????

?? ??? ??????

??????

???? ??? ??????? ???? ????? ????? ?????? Excel ??????? ?? ???? ??????? ???? ?? ??????? ???????? Visual Basic .NET.

???? ???? ??? ?????? ?????

????? ????? ????? ????? ?? Visual Basic .NET ?????? ??????? ????? ? ???????? ??? ????? ?????? ????? ????? ???????:
  • ????? ?????? ?????? ????? ????? ??????? ?????? ???????? ?????? ?????? ?????? ???????? WithEvents. ??? ????? ?????? ???????? ?????? ???????? WithEvents Visual Basic .NET ???????? ???? ??????? ??? ?????? ?? ??? ???????. ?????? ??? ???? ???? ?????? ????? ????? ??? ????? ???????? ??? ?????? ????? Visual Studio .NET ???? ????? ???? ?? ????? ??? ????????? ????????. - ?? -

  • ?????? ?????? ???????? AddHandler Visual Basic .NET ???? ????? ???? ??????? ???????. AddHandler ? RemoveHandler ???? ??????? ??? ????? ?????? ?????? ????? ??? ???? ???? ????????.

????? ???? ??????? Visual Basic .NET

???? ??????? ??????? ????? ??????? ??? ??????? ??????? ??????? Excel ?? ???? ??????? ???? ?? ??????? ???????? Visual Basic .NET.
  1. ??? ????? Microsoft Visual Studio .NET. ?? ??????? ???? ???? ??? ???? ?? ???? ??? ???????. ??? ???????? Visual Basic ? ??? ????? Windows. ??? ????? Form1 ???? ???????.
  2. ????? ???? ??? ????? ?????? Microsoft Excel. ?????? ????? ???? ??????? ???????:
    1. ?? ??????? ??????? ???? ??? ????? ????.
    2. ?? ??????? COM ? ??? ???? ????? ?????? Microsoft Excel 10.0 ?? ???? ??? ?????. ??????: ??? ?? ??? ?? ??? ?????? ?????? ??? ? ???? Microsoft ????? ?? Microsoft Office XP ???????? Interop ????????? (PIAs) ????????. ?????? ??? ??????? ?????? ??? Office XP PIAs ???? ??? ??? ??????? ????? ?????? ?? "????? ??????? ?? Microsoft:
      328912INFO: Microsoft Office XP PIAs ???????? ???????
    3. ???? ??? ????? ?? ???? ?????? ????? ????? ????? ?????????. ??? ????? ???? ?????? wrappers ???????? ???? ?????? ???? ??? "???".
  3. ?? ??????? ??????? ??? ????? ?????? ???????. ??? ?????? ??????? ?? ????? ????? ????? ??? ???. ?? ???? ???????? ???????? ??????? ?? ?????? ??????? ????:
       '==================================================================
       'Demonstrates Using a Delegate for Event Handling
       '==================================================================
    
       Private xlApp As Excel.Application
       Private xlBook As Excel.Workbook
       Private xlSheet1 As Excel.Worksheet
       Private xlSheet2 As Excel.Worksheet
       Private xlSheet3 As Excel.Worksheet
       Private EventDel_BeforeBookClose As Excel.AppEvents_WorkbookBeforeCloseEventHandler
       Private EventDel_CellsChange As Excel.DocEvents_ChangeEventHandler
    
       Public Sub UseDelegate()
          'Start Excel and create a new workbook.
          xlApp = CreateObject("Excel.Application")
          xlBook = xlApp.Workbooks.Add()
          xlBook.Windows(1).Caption = "Uses WithEvents"
    
          'Get references to the three worksheets.
          xlSheet1 = xlBook.Worksheets.Item(1)
          xlSheet2 = xlBook.Worksheets.Item(2)
          xlSheet3 = xlBook.Worksheets.Item(3)
          CType(xlSheet1, Excel._Worksheet).Activate()
    
          'Add an event handler for the WorkbookBeforeClose Event of the
          'Application object.
          EventDel_BeforeBookClose = New Excel.AppEvents_WorkbookBeforeCloseEventHandler( _
                AddressOf BeforeBookClose)
          AddHandler xlApp.WorkbookBeforeClose, EventDel_BeforeBookClose
    
          'Add an event handler for the Change event of both Worksheet 
          'objects.
          EventDel_CellsChange = New Excel.DocEvents_ChangeEventHandler( _
                AddressOf CellsChange)
          AddHandler xlSheet1.Change, EventDel_CellsChange
          AddHandler xlSheet2.Change, EventDel_CellsChange
          AddHandler xlSheet3.Change, EventDel_CellsChange
    
          'Make Excel visible and give the user control.
          xlApp.Visible = True
          xlApp.UserControl = True
       End Sub
    
       Private Sub CellsChange(ByVal Target As Excel.Range)
          'This is called when a cell or cells on a worksheet are changed.
          Debug.WriteLine("Delegate: You Changed Cells " + Target.Address + " on " + _
                            Target.Worksheet.Name())
       End Sub
    
       Private Sub BeforeBookClose(ByVal Wb As Excel.Workbook, ByRef Cancel As Boolean)
          'This is called when you choose to close the workbook in Excel.
          'The event handlers are removed, and then the workbook is closed 
          'without saving changes.
          Debug.WriteLine("Delegate: Closing the workbook and removing event handlers.")
          RemoveHandler xlSheet1.Change, EventDel_CellsChange
          RemoveHandler xlSheet2.Change, EventDel_CellsChange
          RemoveHandler xlSheet3.Change, EventDel_CellsChange
          RemoveHandler xlApp.WorkbookBeforeClose, EventDel_BeforeBookClose
          Wb.Saved = True 'Set the dirty flag to true so there is no prompt to save.
       End Sub
    					
  4. ????? ???? ????? ???? ??? ??????? ?? ?? ???? ???????? ???????? ??????? ?? ?????? ???????:
       '==================================================================
       'Demonstrates Using WithEvents for Event Handling
       '==================================================================
    
       Private WithEvents xlApp As Excel.Application
       Private xlBook As Excel.Workbook
       Private WithEvents xlSheet1 As Excel.Worksheet
       Private WithEvents xlSheet2 As Excel.Worksheet
       Private WithEvents xlSheet3 As Excel.Worksheet
    
       Public Sub UseWithEvents()
          'Start Excel and create a new workbook.
          xlApp = CreateObject("Excel.Application")
          xlBook = xlApp.Workbooks.Add()
          xlBook.Windows(1).Caption = "Uses WithEvents"
    
          'Get references to the three worksheets.
          xlSheet1 = xlBook.Worksheets.Item(1)
          xlSheet2 = xlBook.Worksheets.Item(2)
          xlSheet3 = xlBook.Worksheets.Item(3)
          CType(xlSheet1, Excel._Worksheet).Activate()
    
          'Make Excel visible and give the user control.
          xlApp.Visible = True
          xlApp.UserControl = True
       End Sub
    
       Private Sub xlApp_WorkbookBeforeClose(ByVal Wb As Excel.Workbook, _
         ByRef Cancel As Boolean) Handles xlApp.WorkbookBeforeClose
          Debug.WriteLine("WithEvents: Closing the workbook.")
          Wb.Saved = True 'Set the dirty flag to true so there is no prompt to save
       End Sub
    
       Private Sub xlSheet1_Change(ByVal Target As Excel.Range) Handles xlSheet1.Change
          Debug.WriteLine("WithEvents: You Changed Cells " + Target.Address + " on Sheet1")
       End Sub
    
       Private Sub xlSheet2_Change(ByVal Target As Excel.Range) Handles xlSheet2.Change
          Debug.WriteLine("WithEvents: You Changed Cells " + Target.Address + " on Sheet2")
       End Sub
    					
  5. ??? ?????? ??? ???? Module1.vb:
    Imports Microsoft.Office.Interop
    					
  6. ??? ?????? ??? ???? Module2.vb:
    Imports Microsoft.Office.Interop
    					
  7. ?? "?????? ??????" ???? ????? ??????? ??? Form1.vb ???? ??????? ?? ????? ????? "?????".
  8. ?? ??????? ???? ??? Toolbox ???? ???? ??????? ?? ?? ?????? ?????? ??? Form1. ????? ??????? TextButton1 ??? ??????? WithEvents ?? ?? ?????? ??????? TextButton2 ??? ??????? Delegates.
  9. ?? ??????? ??? ? ??? ????? ???? ???? ????????? ???????? ???????. ?? ?????? ????????? ???????? ??????? ??? ??????? ??????? ???? ??????:
    
        Private Sub Button1_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button1.Click
            UseWithEvents()
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button2.Click
            UseDelegate()
        End Sub
    					

?????? ????????? ????????

  1. ???? CTRL + ALT + O ???? ?????? ?????.
  2. ???? F5 ?????? ???????? ???????.
  3. ?? ???????? ???? ??? ??????? WithEvents. ???????? ???? ????? Excel ?????? ???? ?? ???? ????? ???.
  4. ????? ??? ?????? ??? ????? ?? ????? ??? ?? ????. ???? ??????? ENTER ??? ?? ?????. ??? ?????? ????? ?? Visual Studio ?????? ?? ???? ??????? ???????.
  5. ?? ?????? Excel.
  6. ?? ???????? ???? ??? ????? ???????. ??? ????? ???????? ???? ????? Excel ????? ?????? ???? ?? ????? ??? ??????.
  7. ????? ??? ?????? ??? ????? ?? ????? ??? ?? ????. ???? ??????? ENTER ??? ?? ?????. ??? ?????? ????? ?? Visual Studio ?????? ?? ???? ??????? ???????.
  8. ?? ?????? Excel ?? ????? ??????? ?????? ???? ????? ?????.

??????? ??????? ????????

??? ?????? ????????? ???????? ?? ????? ????? ????? ???????:
??? ??????? ??? ????? ?? ????? 'System.InvalidCastException' ??? ?? interop.excel.dll

??????? ??????: ?? ???? ????? ??? ?????
?? ???? ???? ??? ????? ???? ??????? ??????? ?? "????? ??????? ?? Microsoft:
316653PRB: ??? ???????? WithEvents ?? ?????? ??????? ??????? ?? Microsoft Excel ?? Studio.NET ?????

?????

????? ?? ?????????? ???? ?????? ?????? Microsoft Network (MSDN) ???? ???:
????? Microsoft Office ?? Visual Studio
http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx
?????? ??? ??????? ?????? ??? ????? Excel ?? Visual Basic .NET ???? ??? ???? ????????? ????????? ??????? ?? "????? ??????? ?? Microsoft:
301982HOWTO: ????? Microsoft Excel ?? Visual Basic .NET
302094HOWTO: ????? Excel ?? .Net Visual Basic ??????? ?? ?????? ??? ???????? ?? ???? ??????? ??????


(c) ??? ???? Microsoft ???? ???? ?????. ????????? ??? Joel Alley ? Microsoft Corporation.

???????

???? ???????: 302814 - ????? ??? ??????: 14/????? ???????/1428 - ??????: 6.2
????? ???
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Excel 2002 Standard Edition
????? ??????: 
kbmt kbautomation kbhowtomaster KB302814 KbMtar
????? ????
???: ??? ????? ??? ?????? ???????? ?????? ????? ???? ????? ?????????? ????? ?? ????????? ?????? ????. ???? ???? ?????????? ???? ?? ???????? ???????? ?????? ????????? ????? ????????? ???????? ????? ???????? ?????? ?? ?????? ??? ?? ???????? ???????? ?? ????? ??????? ?????? ??? ??????? ?????? ??. ?????? ?? ???? ??? ??????? ???????? ????? ?? ???? ????? ?????? ??? ????? ??? ????? ??????? ?? ????? ?? ?????? ??? ??? ??????? ??????? ?? ????? ????? ????? ????? ?????. ?? ????? ???? ?????????? ??????? ??? ????? ?? ??????? ?? ????? ?????? ?? ??? ????? ?? ????? ??????? ?? ???????? ?? ??? ???????. ???? ???? ?????????? ???????? ??? ????? ?????? ??????? ??????
???? ??? ????? ??????? ?????? ??????????302814

????? ???????

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com