VBA转VB.Net VSTO学习记录-1

2022-10-31 15:38:40 浏览数 (1)

yhd-VBA转VB.Net VSTO学习记录-1

想搞个插件

代码语言:javascript复制
'显示当前文本
    Private Sub Button1_Click(sender As Object, e As RibbonControlEventArgs) Handles Button1.Click
        MsgBox(xlapp.ActiveCell.Value)
    End Sub
    '打开文件
    Private Sub Button2_Click(sender As Object, e As RibbonControlEventArgs) Handles Button2.Click

        Dim yhdfile As String '= "C:UsersAdminDesktoptest拆分源test.xlsx"

        With OpenFileDialog1
            .Filter = "Excel文件|*.xls*"
            .Multiselect = False
            If .ShowDialog() = DialogResult.OK Then

                If System.IO.File.Exists(.FileName) Then
                    MsgBox(.FileName   vbCrLf   "文件存在", Title:="哆哆提示")
                    yhdfile = .FileName
                    xlapp.ActiveCell.Value = yhdfile
                End If
            End If
        End With
        yhdfile = xlapp.ActiveCell.Value
        'xlapp.Workbooks.Open(yhdfile)

    End Sub
    '新建工作表
    Private Sub Button3_Click(sender As Object, e As RibbonControlEventArgs) Handles Button3.Click
        For Each s As Excel.Worksheet In xlapp.Worksheets
            If s.Name = "dd1" Or s.Name = "dd2" Or s.Name = "dd3" Then
                MsgBox(s.Name   "存在")
                s.Delete()
            End If
        Next
        '三种方法创建工作表
        Globals.ThisAddIn.Application.Worksheets.Add.name = "dd1"

        xlapp.Worksheets.Add.name = "dd2"
        With xlapp.Worksheets("dd2")
            .cells(1, 1) = "哆哆新建工作表dd2"
        End With

        Dim yhdsht As Excel.Worksheet
        yhdsht = xlapp.Worksheets.Add()
        yhdsht.Name = "dd3"
    End Sub
    '复制工作表
    Private Sub Button4_Click(sender As Object, e As RibbonControlEventArgs) Handles Button4.Click
        Dim Acsht As Excel.Worksheet = xlapp.ActiveSheet
        Dim Tosht As Excel.Worksheet
        Acsht.Copy(After:=xlapp.Worksheets(xlapp.Worksheets.Count))
        Dim shtname As String = "dd"   xlapp.Worksheets.Count.ToString()
        xlapp.Worksheets(xlapp.Worksheets.Count).Name = shtname
        Tosht = xlapp.Worksheets(xlapp.Worksheets.Count)
        MsgBox("工作表名:"   Tosht.Name)
        Tosht.Name = "修改dd"   xlapp.Worksheets.Count.ToString()
    End Sub

0 人点赞