星期日, 十一月 27, 2011

Posting Code to this Blog

From : Daily Does of Excel

Posting Code to this Blog:

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 thisSelect this










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


<span class="vb">sOutput =</span>

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


<span class="text">inline = "true"</span>

argument when I’m on a single line. I think I’ll use and see how often I’m adding it.

没有评论: