C# VSTO Excel确定注解选择

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

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

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

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

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

odopli94

odopli941#

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

Option Explicit

Dim lastAddress As String
Dim cmtAddress As String
Dim cmtText As String

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    With Target
        If Not .Comment Is Nothing Then
            cmtAddress = .Address
            cmtText = .Comment.Text
        Else
            If cmtAddress <> vbNullString Then
                If Range(cmtAddress).Comment.Text <> cmtText Then
                    MsgBox "comment changed in cell '" & cmtAddress & "'"
                End If
                cmtAddress = vbNullString
            
            Else
                If Not Range(lastAddress).Comment Is Nothing Then
                    MsgBox "comment added in cell '" & lastAddress & "'"
                End If
            End If
        End If
        
        lastAddress = .Address
    End With
End Sub
mwg9r5ms

mwg9r5ms2#

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

Option Explicit

Dim lastAddress As String
Dim cmtAddress As String
Dim cmtText As String

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    With Target
        If Not .Comment Is Nothing Then
                If cmtAddress <> vbNullString Then
                    If Not Range(cmtAddress).Comment Is Nothing Then
                        If Range(cmtAddress).Comment.Text <> cmtText Then
                            MsgBox "Comment changed in cell '" & cmtAddress & "', " & vbCrLf & _
                            "(""" & Range(cmtAddress).Comment.Text & """ instead of """ & cmtText & """)."
                        End If
                    End If
                End If
                If Not lastAddress = "" Then
                        If Not Range(lastAddress).Comment Is Nothing Then
                            MsgBox "Comment added in cell '" & lastAddress & "'" & vbCrLf & _
                            "(""" & Range(lastAddress).Comment.Text & """)."
                        End If
                End If
                cmtAddress = .address
                cmtText = .Comment.Text
        Else
                If cmtAddress <> vbNullString Then
                    If Not Range(cmtAddress).Comment Is Nothing Then
                        If Range(cmtAddress).Comment.Text <> cmtText Then
                            MsgBox "Comment changed in cell '" & cmtAddress & "', " & vbCrLf & _
                            "(""" & Range(cmtAddress).Comment.Text & """ instead of """ & cmtText & """)."
                        End If
                    End If
                    cmtAddress = vbNullString
                Else
                   If Not lastAddress = "" Then
                        If Not Range(lastAddress).Comment Is Nothing Then
                            MsgBox "Comment added in cell '" & lastAddress & "'" & vbCrLf & _
                            "(""" & Range(lastAddress).Comment.Text & """)."
                        End If
                    End If
                End If
        End If
        
        lastAddress = .address
    End With
End Sub

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

相关问题