Archive for the 'VBA' Category

27
Mar
09

[VBA] Q: How to get pixel colour

The question asked “On the image if i click anywhere, im supposed to tell the user what colour the pixel they clicked on was”

The solution:

Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (ByVal lpPoint As POINT) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long

Private Type POINT
    x As Long
    y As Long
End Type

Private Sub Image1_Click()
    Dim pLocation As POINT
    Dim lColour, lDC As Long
    lDC = GetWindowDC(0)
    Call GetCursorPos(pLocation)
    lColour = GetPixel(lDC, pLocation.x, pLocation.y)
    Me.BackColor = lColour
End Sub

Advertisements
27
Mar
09

[VBA-Word] Syntax Highlighting Generator

Right, so I got fed up of attempting to format code to put it onto wordpress, so, I’ve updated all my posts using a Word-VBA based highlighter.
I’ve not used VBA for anything much before, so the code is reasonably slow (few seconds) but It generates easy to paste code, with span tags for the highlighted areas.

Sub TextFormat()
    Dim strFont As String
    Dim strColour As String
    Dim intStart As Integer
    Dim i As Integer
    Dim intFontSize As String
    Dim html As String

    intStart = 0
    strFont = ThisDocument.words.First.Font.Name
    strColour = ThisDocument.words.First.Font.Color
    i = 0
    intFontSize = "11px"
    html = "<div style='border: #660000 5px solid;' > <pre style='background-color:#ffffff;padding:3px;line-height:15px;' > "

    Selection.Find.ClearFormatting()
    Selection.Find.Replacement.ClearFormatting()
    With Selection.Find
        .Text = "<"
        .Replacement.Text = "<"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute(Replace:=wdReplaceAll)
    With Selection.Find
        .Text = " > "
        .Replacement.Text = " > "
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute(Replace:=wdReplaceAll)

    For Each wItem In ThisDocument.words
        If Not wItem.Font.Color = strColour Or Not wItem.Font.Name = strFont Then
            Selection.Start = intStart
            Selection.End = i
            Selection.Select()

            html = html & "<span style='color: " + getColour(strColour) + " !important; font-family: " + strFont + " !important;font-size:" + intFontSize + " !important;' > " & Selection.Text & "</span > "

            strFont = wItem.Font.Name
            strColour = wItem.Font.Color

            intStart = i
        End If
        i = i + Len(wItem)
    Next

    Selection.Start = intStart
    Selection.End = i
    Selection.Select()

    html = html & "<span style='color: " + getColour(strColour) + " !important; font-family: " + strFont + " !important;font-size:" + intFontSize + " !important;' > " & Selection.Text & "</span > " + vbNewLine + "</pre > </div > "

    Selection.Text = html

    Selection.Copy()
    ThisDocument.Undo()

    MsgBox("Done")
End Sub

Function getColour(ByVal col As String) As String
    If col = -16777216 Or col = wdColorBlack Or col = 9999999 Then
        getColour = "#000000"
    ElseIf col = 16711680 Or col = wdColorBlue Then
        getColour = "#1014e7"
    ElseIf col = 1381795 Or col = wdColorBrown Then
        getColour = "#850404"
    ElseIf col = 32768 Or col = wdColorBrown Then
        getColour = "#067c0e"
    End If
End Function

(Speed-wise, this took around 5 seconds to generate)
You can add your own colours to the getColour function. This is used when parsing the text in the document. This program does NOT actually create the highlighting, but checks fonts and colours, and then creats the html code from the document.

The CSS is all easy to change, the div/pre is the border design and you can change the spans as they are added to the html.