C# VSTO Excel确定注解选择

iyfamqjs  于 2022-11-18  发布在  C#
关注(0)|答案(2)|浏览(199)

我已经创建了一个VSTO excel插件,它应该在每次更改注解时将注解文本存储在数据库中。
(我给你的是VBA中的代码示例,因为通常这是我首先测试初始想法的地方)
选择注解时TypeName(Selection)返回“TextBox”

因此我最初认为可以将其放入Workbook_SheetSelectionChange中:

  1. Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
  2. If TypeName(Target) = "TextBox" Then
  3. 'let the user enter or change the comment
  4. 'once the user has done that and changes the selection
  5. 'trigger the save option to DB
  6. End If
  7. End Sub

问题是选取注解并不会触发Workbook_SheetSelectionChange。如有任何关于如何行程此问题的建议,我们将不胜感激。

odopli94

odopli941#

在VBA中,我会在离开储存格之前和之后检查储存格注解内容,如下所示:

  1. Option Explicit
  2. Dim lastAddress As String
  3. Dim cmtAddress As String
  4. Dim cmtText As String
  5. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  6. With Target
  7. If Not .Comment Is Nothing Then
  8. cmtAddress = .Address
  9. cmtText = .Comment.Text
  10. Else
  11. If cmtAddress <> vbNullString Then
  12. If Range(cmtAddress).Comment.Text <> cmtText Then
  13. MsgBox "comment changed in cell '" & cmtAddress & "'"
  14. End If
  15. cmtAddress = vbNullString
  16. Else
  17. If Not Range(lastAddress).Comment Is Nothing Then
  18. MsgBox "comment added in cell '" & lastAddress & "'"
  19. End If
  20. End If
  21. End If
  22. lastAddress = .Address
  23. End With
  24. End Sub
展开查看全部
mwg9r5ms

mwg9r5ms2#

请尝试下一个改编的代码事件,涵盖更多的情况。但**所有的功劳都要归功于@user3598756,他有一个绝妙的想法。从一开始我甚至不明白你的问题...

  1. Option Explicit
  2. Dim lastAddress As String
  3. Dim cmtAddress As String
  4. Dim cmtText As String
  5. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  6. With Target
  7. If Not .Comment Is Nothing Then
  8. If cmtAddress <> vbNullString Then
  9. If Not Range(cmtAddress).Comment Is Nothing Then
  10. If Range(cmtAddress).Comment.Text <> cmtText Then
  11. MsgBox "Comment changed in cell '" & cmtAddress & "', " & vbCrLf & _
  12. "(""" & Range(cmtAddress).Comment.Text & """ instead of """ & cmtText & """)."
  13. End If
  14. End If
  15. End If
  16. If Not lastAddress = "" Then
  17. If Not Range(lastAddress).Comment Is Nothing Then
  18. MsgBox "Comment added in cell '" & lastAddress & "'" & vbCrLf & _
  19. "(""" & Range(lastAddress).Comment.Text & """)."
  20. End If
  21. End If
  22. cmtAddress = .address
  23. cmtText = .Comment.Text
  24. Else
  25. If cmtAddress <> vbNullString Then
  26. If Not Range(cmtAddress).Comment Is Nothing Then
  27. If Range(cmtAddress).Comment.Text <> cmtText Then
  28. MsgBox "Comment changed in cell '" & cmtAddress & "', " & vbCrLf & _
  29. "(""" & Range(cmtAddress).Comment.Text & """ instead of """ & cmtText & """)."
  30. End If
  31. End If
  32. cmtAddress = vbNullString
  33. Else
  34. If Not lastAddress = "" Then
  35. If Not Range(lastAddress).Comment Is Nothing Then
  36. MsgBox "Comment added in cell '" & lastAddress & "'" & vbCrLf & _
  37. "(""" & Range(lastAddress).Comment.Text & """)."
  38. End If
  39. End If
  40. End If
  41. End If
  42. lastAddress = .address
  43. End With
  44. End Sub

这个想法是,该事件也应该捕捉下一个选择的情况下有这样一个'文本框',太。和一些东西,以防止空地址时,进程开始...

展开查看全部

相关问题