|
本帖最后由 wpxxsyzx 于 2018-7-13 16:48 编辑
- <p>VS中可以快速插入常用的代码段,模仿之,在VBA中快速添加循环语句,条件语句等,代码如下;为了熟悉Advanced Installers,用其制作了安装程序,附件就是,请右击以管理员身份运行该安装程序</p><p>
- </p><p>Imports Microsoft.Office.Core
- Public Class ThisAddIn
- Public objBtns(18) As CommandBarButton
- Public objButton As CommandBarButton
- Dim strCaption() = {"If...End If", "If...Else...End If", "If...ElseIf...Else...End If", "Select Case语句", "#If...#Else...#End If",
- "For...Next", "For Each...Next", "DO While...Loop", "DO Until...Loop", "DO...Loop Until", "DO...Loop While",
- "添加引用", "声明变量", "获取字段名", "连接工作薄", "连接文本文件", "查询工作表", "查询工作表区域", "查询其他工作薄中的工作表"}
- Private Sub ThisAddIn_Startup() Handles Me.Startup
- Application.VBE.CommandBars("Code Window").Reset()
- With Application.VBE.CommandBars("Code Window").Controls.Add(MsoControlType.msoControlPopup,,, 1, True)
- .Caption = "插入代码段"
- With .Controls.add(MsoControlType.msoControlPopup,,,, True)
- .Caption = "条件语句"
- For i As Integer = 0 To 4
- objButton = .Controls.add(MsoControlType.msoControlButton,,,, True)
- objButton.Caption = strCaption(i)
- AddHandler objButton.Click, AddressOf objButtonClick
- objBtns(i) = objButton
- Next
- End With
- With .Controls.add(MsoControlType.msoControlPopup,,,, True)
- .Caption = "循环语句"
- For i As Integer = 5 To 10
- objButton = .Controls.add(MsoControlType.msoControlButton,,,, True)
- objButton.Caption = strCaption(i)
- AddHandler objButton.Click, AddressOf objButtonClick
- objBtns(i) = objButton
- Next
- End With
- With .Controls.add(MsoControlType.msoControlPopup,,,, True)
- .Caption = "ADO相关"
- For i As Integer = 11 To 18
- objButton = .Controls.add(MsoControlType.msoControlButton,,,, True)
- objButton.Caption = strCaption(i)
- AddHandler objButton.Click, AddressOf objButtonClick
- objBtns(i) = objButton
- Next
- End With
- End With
- End Sub
- Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
- End Sub
- Private Sub objButtonClick(Ctrl As CommandBarButton, ByRef CancelDefault As Boolean)
- Dim strAdd As String
- Select Case Ctrl.Caption
- Case strCaption(0)
- strAdd = "If True Then" & vbCrLf & vbCrLf & "End If"
- Case strCaption(1)
- strAdd = "If True Then" & vbCrLf & vbCrLf & "Else" & vbCrLf & vbCrLf & "End If"
- Case strCaption(2)
- strAdd = "If True Then" & vbCrLf & vbCrLf & "ElseIf False Then" & vbCrLf & vbCrLf & "Else" & vbCrLf & vbCrLf & "End If"
- Case strCaption(3)
- strAdd = "Select Case Value" & vbCrLf & "Case Value1" & vbCrLf & vbCrLf & "Case Value2" & vbCrLf & vbCrLf & "Case Else" & vbCrLf & vbCrLf & "End Select"
- Case strCaption(4)
- strAdd = "#If True Then" & vbCrLf & vbCrLf & "#Else" & vbCrLf & vbCrLf & "#End If"
- Case strCaption(5)
- strAdd = "For i = 1 To 10" & vbCrLf & vbCrLf & "Next i"
- Case strCaption(6)
- strAdd = "For Each item In collectionObject" & vbCrLf & vbCrLf & "Next item"
- Case strCaption(7)
- strAdd = "Do Until False" & vbCrLf & vbCrLf & "Loop"
- Case strCaption(8)
- strAdd = "Do While True" & vbCrLf & vbCrLf & "Loop"
- Case strCaption(9)
- strAdd = "Do" & vbCrLf & vbCrLf & "Loop Until False"
- Case strCaption(10)
- strAdd = "Do" & vbCrLf & vbCrLf & "Loop While True"
- Case strCaption(11)
- Application.VBE.ActiveVBProject.References.AddFromGuid("{B691E011-1797-432E-907A-4D8C69339129}", 6, 1)
- Case strCaption(12)
- strAdd = "Dim objConn As New ADODB.Connection" & vbCrLf & "Dim objRst As New ADODB.Recordset" & vbCrLf & "Dim strSql As String" &
- vbCrLf & vbCrLf & "objRst.Close" & vbCrLf & "objConn.Close" & vbCrLf & "Set objRst = Nothing" & vbCrLf & "Set objConn = Nothing"
- Case strCaption(13)
- strAdd = "For i = 0 To objRst.Fields.Count - 1" & vbCrLf & "工作表名.Cells(1, i + 1) = objRst.Fields(i).Name" & vbCrLf & "Next i"
- Case strCaption(14)
- strAdd = "objConn.Open " & """provider=microsoft.ace.oledb.12.0;extended properties='excel 12.0;hdr=yes;IMEX=1';data source=""" & " & " & "文件名及完整路径"
- Case strCaption(15)
- strAdd = "objConn.Open " & """provider=microsoft.ace.oledb.12.0;extended properties='text;FMT=DELIMITED';data source=""" & " & " & "文本文件路径"
- Case strCaption(16)
- strAdd = "strSql = " & """select * from [工作表名称$]"""
- Case strCaption(17)
- strAdd = "strSql = " & """select * from [工作表名称$A1:G100]"""
- Case strCaption(18)
- strAdd = "strSql = " & """select * from [Excel 8.0;Database=""" & " & 文件名及完整路径 & " & """].[Sheet1$a:b]"""
- End Select
- Application.VBE.ActiveCodePane.CodeModule.AddFromString(strAdd)
- End Sub
- End Class
- </p>
复制代码
|
|