前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >VB.net-VSTO-13文本插入、提取、删除

VB.net-VSTO-13文本插入、提取、删除

作者头像
哆哆Excel
发布2024-02-26 09:49:15
1400
发布2024-02-26 09:49:15
举报
文章被收录于专栏:哆哆Excel

仿**格子,我自己也做一个

代码如下

代码语言:javascript
复制
 '====文本插入=========
    Public Sub MyInsert(control As Office.IRibbonControl)

        Select Case control.Id
            Case "Insert_btn1" '"批量插入到开头"
                On Error Resume Next
                Dim SelectRngs As Excel.Range = xlapp.Selection
                If IsNothing(SelectRngs) Then
                    MsgBox(Prompt:="你没有选择,我退了哦", Title:="哆哆提示")
                    Return
                End If
                Dim s As String = xlapp.InputBox(Prompt:="输入要插入的文本", Title:="请输入", Default:="文本", Type:=2)
                If s = "False" Then Return
                Dim useRng As Excel.Range = xlapp.ActiveSheet.UsedRange
                Dim Rngs As Excel.Range = xlapp.Intersect(useRng, SelectRngs)
                For Each rng1 As Excel.Range In Rngs
                    If Not String.IsNullOrEmpty(rng1.Text) Then
                        Dim tmpS As String = rng1.Text
                        rng1.Value = s + tmpS
                    End If
                Next

                On Error GoTo 0
            Case "Insert_btn2" '"批量插入到中间"
                On Error Resume Next
                Dim SelectRngs As Excel.Range = xlapp.Selection
                If IsNothing(SelectRngs) Then
                    MsgBox(Prompt:="你没有选择,我退了哦", Title:="哆哆提示")
                    Return
                End If
                Dim s As String = xlapp.InputBox(Prompt:="输入‘位置、文本’;注意用顿号‘、’隔开" + vbCrLf + "例如:3、文本", Title:="请输入", Default:="3、文本", Type:=2)
                If s = "False" Then Return

                Dim pattern As String = "[\p{P}\p{S}]+" ' 匹配所有标点和符号
                Dim replacement As String = "、" ' 替换为全角顿号
                Dim tempArr() As String = Split(Regex.Replace(s, pattern, replacement), "、")
                Dim outInt As Integer = 1
                If tempArr.Length < 2 OrElse Integer.TryParse(tempArr(0), outInt) = False Then
                    MsgBox(Prompt:="输入有误,我退了哦", Title:="哆哆提示")
                    Return
                End If

                Dim s1 As Integer = Integer.Parse(tempArr(0))
                Dim s2 As String = tempArr(1)
                Dim useRng As Excel.Range = xlapp.ActiveSheet.UsedRange
                Dim Rngs As Excel.Range = xlapp.Intersect(useRng, SelectRngs)
                For Each rng1 As Excel.Range In Rngs
                    If Not String.IsNullOrEmpty(rng1.Text) Then
                        Dim tmpS As String = rng1.Text
                        rng1.Value = tmpS.Insert(s1, s2)
                    End If
                Next

                On Error GoTo 0
            Case "Insert_btn3" '"批量插入到末尾"
                On Error Resume Next
                Dim SelectRngs As Excel.Range = xlapp.Selection
                If IsNothing(SelectRngs) Then
                    MsgBox(Prompt:="你没有选择,我退了哦", Title:="哆哆提示")
                    Return
                End If
                Dim s As String = xlapp.InputBox(Prompt:="输入要插入的文本", Title:="请输入", Default:="文本", Type:=2)
                If s = "False" Then Return
                Dim useRng As Excel.Range = xlapp.ActiveSheet.UsedRange
                Dim Rngs As Excel.Range = xlapp.Intersect(useRng, SelectRngs)
                For Each rng1 As Excel.Range In Rngs
                    If Not String.IsNullOrEmpty(rng1.Text) Then
                        Dim tmpS As String = rng1.Value.ToString()
                        rng1.Value = tmpS + s
                    End If
                Next

                On Error GoTo 0
            Case Else

        End Select
    End Sub
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-02-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 哆哆Excel 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档