zoukankan      html  css  js  c++  java
  • 20190801OfficeVBA Indenter

    Public Sub SmartIndenterProcedure()
        
        Dim StartLine As Long, EndLine As Long
        Dim LineIndex As Long
        Dim StartCol As Long, EndCol As Long
        Dim LineText As String
        Dim ProcName As String, KeyWord As String
        Dim IndentLevel As Integer, IsAfterUnderLine As Boolean
        Dim IndentThisLine As Boolean, BackThisLine As Boolean
        Dim IndentNextLine As Boolean, BackNextLine As Boolean
        
        
        Set ThisCodePane = ActiveWorkbook.VBProject.VBE.ActiveCodePane    '获取活动代码窗格
        ThisCodePane.GetSelection StartLine, StartCol, EndLine, EndCol    '获取光标位置或选定范围的 起止行列号
        
        ProcName = ActiveWorkbook.VBProject.VBE.SelectedVBComponent.CodeModule.ProcOfLine(StartLine, vbext_pk_Proc)
        StartLine = ActiveWorkbook.VBProject.VBE.SelectedVBComponent.CodeModule.ProcStartLine(ProcName, vbext_pk_Proc)
        EndLine = ActiveWorkbook.VBProject.VBE.SelectedVBComponent.CodeModule.ProcCountLines(ProcName, vbext_pk_Proc) + StartLine
        
        '循环每一行,删除行首缩进
        For LineIndex = StartLine To EndLine
            LineText = ThisCodePane.CodeModule.Lines(LineIndex, 1)
            LineText = RegReplace(LineText, "^s*")
            ThisCodePane.CodeModule.ReplaceLine LineIndex, LineText
        Next LineIndex
        
        
        '设置缩进级别
        IndentLevel = 0
        For LineIndex = StartLine To EndLine
            LineText = ThisCodePane.CodeModule.Lines(LineIndex, 1)
            KeyWord = Left(LineText, IIf(InStr(LineText, " ") = 0, Len(LineText), InStr(LineText, " ") - 1))
            
            Select Case KeyWord
            Case "Do", "For", "Private", "Public", "Select", "Sub", "While", "With", "Function", "Type", "Property"
                IndentNextLine = True                 'After certain keywords, indent next line
            Case "If"                      'After If, where line ends in Then, indent next line
                If Right(LineText, 4) = "Then" Then IndentNextLine = True
                ' If InStr(LineText, " Then ") > 0 Or InStr(LineText, " Then'") > 0 Then IndentNextLine = True
            Case "Loop", "Next", "End"                 'At Loop, Next, End, un-indent this line
                BackThisLine = True
            Case "Case", "Else", "ElseIf"
                BackThisLine = True  'Un-indent Case or Else
                IndentNextLine = True                           'Indent line after Case or Else
                'Case "Public", "Private"
                '    If Split(LineText, " ")(1) = "Sub" Or Split(LineText, " ")(1) = "Function" Then
                '       IndentNextLine = True
                '    End If
            End Select
            
            '判断续行问题
            If Right(LineText, 2) = " _" And IsAfterUnderLine = False Then
                IndentNextLine = True                             'Indent line after underscore
                IsAfterUnderLine = True      'Set a flag to un-indent the line after next
            ElseIf Right(LineText, 2) <> " _" And IsAfterUnderLine Then
                BackNextLine = True
                IsAfterUnderLine = False
            End If
            
            '处理本行的缩进级别
            If IndentThisLine Then
                IndentLevel = IndentLevel + 1
                IndentThisLine = False
            End If
            
            If BackThisLine Then
                IndentLevel = IndentLevel - 1
                BackThisLine = False
            End If
            
            On Error GoTo ErrHandler
            ThisCodePane.CodeModule.ReplaceLine LineIndex, Space$(IndentLevel * 4) & LineText
            On Error GoTo 0
            
            If IndentNextLine Then
                IndentLevel = IndentLevel + 1    '下一行的缩进级别
                IndentNextLine = False
            End If
            
            If BackNextLine Then
                IndentLevel = IndentLevel - 1    '下一行的缩进级别
                BackNextLine = False
            End If
            
        Next LineIndex
        
        Set ThisCodePane = Nothing
        
        Exit Sub
    ErrHandler:
        If IndentLevel < 0 Then IndentLevel = 0  'Will not happen unless extra lines selected
        Resume Next
    End Sub
    
    Private Function RegReplace(ByVal OrgText As String, ByVal Pattern As String, Optional RepStr As String = "") As String
        Dim Regex As Object
        Dim newText As String
        Set Regex = CreateObject("VBScript.RegExp")
        With Regex
            .Global = True
            .Pattern = Pattern
        End With
        newText = Regex.Replace(OrgText, RepStr)
        RegReplace = newText
        Set Regex = Nothing
    End Function
    

      

  • 相关阅读:
    vue获取当前v-for里当前点击元素
    js利用正则替换图片路径问题
    undefined null 各种值比较(面试题)
    SSE两个页面的相互通信
    微信小程序导航栏,下面内容滑动,上册导航栏跟着滑动,内容随着导航栏滑动
    微信小程序缓存滑动距离,当页面浏览到一定位置,滑动其他页面后返回该页面记录之前的滑动距离
    ajax拖拽上传文件
    Java 面向对象(四)
    关于Scanner调用 sc.nextInt() 异常try后不能二次输入导致死循环问题
    Java 面向对象(三)
  • 原文地址:https://www.cnblogs.com/nextseven/p/11285920.html
Copyright © 2011-2022 走看看