For years I’ve been typing <code> tags and pasting code between them. But no more! I wrote a small utility that puts the code tags around my code and pops into the clipboard. Think of the seconds that I’ll save.
There are three situations that I wanted to cover with this code; no selection, multiple procedure selection, and intra-procedure selection. If there’s no selection, I want the whole procedure that contains the cursor. If the selection spans more than one procedure, I want the entirety of all the procedures that are touched by the selection. If the selection is within one procedure, I want what’s selected.
This code uses the Microsoft Visual Basic Extensibility library.
To get this | Select this |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
Sub CreateCodeTags() Dim cp As CodePane, cm As CodeModule Dim lStartLine As Long, lEndLine As Long Dim lStartCol As Long, lEndCol As Long Dim sStartProc As String, sEndProc As String Dim lStartType As Long, lEndType As Long Dim sOutput As String Dim doClip As DataObject Set cp = Application.VBE.ActiveCodePane Set cm = cp.CodeModule cp.GetSelection lStartLine, lStartCol, lEndLine, lEndCol sStartProc = cm.ProcOfLine(lStartLine, lStartType) sEndProc = cm.ProcOfLine(lEndLine, lEndType) 'Single cursor = get whole procedure If lStartLine = lEndLine And lStartCol = lEndCol Then sOutput = cm.Lines(cm.ProcStartLine(sStartProc, lStartType) + 1, cm.ProcCountLines(sStartProc, lStartType) - 1) 'Spans more than one procedure = get all procedures in selection ElseIf sStartProc <> sEndProc Then lStartLine = cm.ProcStartLine(sStartProc, lStartType) + 1 lEndLine = cm.ProcStartLine(sEndProc, lEndType) + cm.ProcCountLines(sEndProc, lEndType) sOutput = cm.Lines(lStartLine, lEndLine - lStartLine) 'Same line = get selected text ElseIf lStartLine = lEndLine Then sOutput = Mid$(cm.Lines(lStartLine, 1), lStartCol, lEndCol - lStartCol) 'Multiple lines = get selected text Else sOutput = Mid$(cm.Lines(lStartLine, 1), lStartCol, Len(cm.Lines(lStartLine, 1))) If lEndLine - lStartLine > 1 Then sOutput = sOutput & vbNewLine & cm.Lines(lStartLine + 1, (lEndLine) - (lStartLine + 1)) End If sOutput = sOutput & vbNewLine & Left$(cm.Lines(lEndLine, 1), lEndCol - 1) End If If Right$(sOutput, Len(vbNewLine)) = vbNewLine Then sOutput = Left$(sOutput, Len(sOutput) - Len(vbNewLine)) End If sOutput = "< code lang=""vb"">" & sOutput & "< /code>" Set doClip = New DataObject doClip.SetText sOutput doClip.PutInClipboard End Sub |
In the last line that begins with sOutput =
I had to add some extraneous spaces to be able to post code that contains code tags, but they’re not really there in the code.
Other than that I’m merely doing string manipulation with my starting and ending lines and columns.
I think I need to add the inline = "true"
argument when I’m on a single line. I think I’ll use and see how often I’m adding it
Posting code? Use <pre> tags for VBA and <code> tags for inline.