Below are more handy code snippets.
These are answers to various questions asked on VBForums.
A lot of the code fragments are very basic and some people may
even wonder why someone would post this stuff.
Well, people keep asking the questions, so, here are the answers!

In the very near future, all other scattered code fragments will be
consolidated into collection-threads like this.
Each thread will contain a table of contents like the one below,
and there will also be a single master table of contents for all
code fragment threads.


Contents, in order ;
00) Create a folder is it does not already exist
01) Return the largest number in an array of numbers
02) Sum up an array of numbers
03) Return the decimal version of a hex value
04) Highlight, and make bold if needs be, text between brackets a certain colour
05) Display the co-ordinates of the mouse pointer every 50ms
06) The occurances of a string in an array of strings
07) The occurances of a string in another string
08) Reverse the order of an array
09) Alternate implementation of reverseOrder()
10) Round up any given number
11) Code to take a screengrab and BitBlt into a PictureBox
12) An implementation of Split(), with ability to use multiple delimeters
13) Code to make a form stay on top
14) Code to open a webpage from inside your app



VB Code:
  1. '' Create a folder if it does not already exist
  2. ''
  3. Private Sub createFolder(ByVal strFolderPath As String)
  4.     If Len(Dir(strFolderPath, vbDirectory)) = 0 Then
  5.         MkDir strFolderPath
  6.     End If
  7. End Sub
  8.  
  9.  
  10.  
  11. '' Return the largest number in an array of numbers
  12. ''
  13. Private Function findLargestInArray(ByRef arrName() As Long) As Long
  14.     findLargestInArray = arrName(0): Dim i As Long
  15.     For i = 0 To UBound(arrName)
  16.         If arrName(i) > findLargestInArray Then findLargestInArray = arrName(i)
  17.     Next
  18. End Function
  19.  
  20.  
  21.  
  22. '' Sum up an array of numbers
  23. ''
  24. Private Function sumArray(ByRef arrName() As Long) As Long
  25.     Dim i As Long
  26.     For i = 0 To UBound(arrName)
  27.         sumArray = sumArray + arrName(i)
  28.     Next
  29. End Function
  30.  
  31.  
  32.  
  33. '' Return the decimal version of a hex value
  34. ''
  35. Private Function hexToDecimal(ByVal hexVal As Variant) As Long
  36.     hexToDecimal = CLng("&H" & hexVal)
  37. End Function
  38.  
  39.  
  40.  
  41. '' Highlight, and make bold if needs be, text between brackets a certain colour
  42. ''
  43. Private Sub doHighlight(colour As Long, bold As Boolean, rtb As RichTextBox)
  44.     Dim nStart As Long, nNext As Long: nStart = 1
  45.     Do
  46.         nStart = InStr(nStart, rtb.Text, "(")
  47.         If Not nStart = 0 Then
  48.             nNext = InStr(nStart, rtb.Text, ")")
  49.             If Not nNext = 0 Then
  50.                 With rtb
  51.                     .SelStart = nStart
  52.                     .SelLength = nNext - nStart - 1
  53.                     .SelBold = bold
  54.                     .SelColor = colour
  55.                 End With
  56.             End If
  57.         End If
  58.         If nStart = 0 Then Exit Do
  59.         nStart = nStart + 1
  60.     Loop
  61.     rtb.SelStart = 1
  62. End Sub
  63.  
  64.  
  65.  
  66. '' Display the co-ordinates of the mouse pointer every 50ms
  67. ''
  68. Private lastTick As Long
  69. Private mouseCords As POINTAPI
  70. Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
  71. Private Declare Function GetTickCount Lib "kernel32" () As Long
  72.  
  73. Private Type POINTAPI
  74.     x As Long
  75.     y As Long
  76. End Type
  77.  
  78. Private Sub Form_Load()
  79.     Show
  80.     Do
  81.         DoEvents
  82.         If GetTickCount - lastTick > 50 Then
  83.             lastTick = GetTickCount
  84.             GetCursorPos mouseCords
  85.             With mouseCords
  86.                 Debug.Print .x & "," & .y
  87.             End With
  88.         End If
  89.     Loop
  90. End Sub
  91.  
  92.  
  93.  
  94. '' The occurances of a string in an array of strings
  95. ''
  96. Private Function occurancesOf(ByVal strString As String, ByRef strArray() As String) As Long
  97.     Dim i As Long
  98.     For i = 0 To UBound(strArray)
  99.         occurancesOf = occurancesOf + Abs(LCase(strArray(i)) = LCase(strString))
  100.     Next
  101. End Function
  102.  
  103.  
  104.  
  105. '' The occurances of a string in another string
  106. ''
  107. Private Function occurancesOfString(ByVal strSought As String, ByVal strSearchIn As String) As Long
  108.     occurancesOfString = (Len(strSearchIn) - Len(Replace(strSearchIn, strSought, ""))) / Len(strSought)
  109. End Function
  110.  
  111.  
  112.  
  113. '' Reverse the order of an array
  114. ''
  115. Private Function reverseOrder(ByRef arrName() As String) As String()
  116.     Dim i As Long, tempArr() As String: ReDim tempArr(UBound(arrName))
  117.     For i = 0 To UBound(arrName)
  118.         tempArr(i) = arrName(UBound(arrName) - i)
  119.     Next
  120.     reverseOrder = tempArr
  121. End Function
  122.  
  123.  
  124.  
  125. '' Alternate implementation of reverseOrder()
  126. '' note: Previous reverseOrder() function is about twice as fast
  127. ''
  128. Private Function reverseOrder2(ByRef arrName() As String) As String()
  129.     reverseOrder2 = Split(StrReverse(Join(arrName, "-:!:-")), "-:!:-")
  130. End Function
  131.  
  132.  
  133.  
  134. '' Round up any given number
  135. ''
  136. Private Function doRound(ByVal num As Double) As Single
  137.     doRound = Round(num + 0.5)
  138. End Function
  139.  
  140.  
  141.  
  142. '' Code to take a screengrab and BitBlt into a PictureBox
  143. ''
  144. Private Declare Function GetDesktopWindow Lib "user32" () As Long
  145. Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
  146. Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
  147. Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
  148.  
  149. Private Sub screenDesktop(ByVal destPBox As PictureBox)
  150.     Dim hWindow As Long, hDContext As Long
  151.     destPBox.AutoRedraw = True
  152.     hWindow = GetDesktopWindow()
  153.     hDContext = GetDC(hWindow)
  154.     BitBlt destPBox.hdc, 0, 0, Screen.Width \ Screen.TwipsPerPixelX, Screen.Height \ Screen.TwipsPerPixelY, hDContext, 0, 0, vbSrcCopy
  155.     destPBox.Refresh: destPBox.Picture = destPBox.Image
  156.     ReleaseDC hWindow, hDContext
  157. End Sub
  158.  
  159.  
  160.  
  161. '' An implementation of Split(), with ability to use multiple delimeters
  162. ''
  163. Private Function Split2(ByVal strString As String, strDelimeters() As String) As String()
  164.     Dim retVal() As String
  165.     If (Not strString = "") And (Not UBound(strDelimeters) = 0) Then
  166.         Dim canContinue As Boolean, i As Long
  167.         For i = 0 To UBound(strDelimeters)
  168.             canContinue = canContinue Or (InStr(strString, strDelimeters(i)) <> 0)
  169.         Next
  170.         If canContinue Then
  171.             ReDim retVal(0)
  172.             Dim j As Long, x As String, y As String
  173.             x = strString
  174.             y = Join(strDelimeters, "")
  175.             i = 1
  176.             While (Not x = "")
  177.                 If InStr(y, Mid(x, i, 1)) <> 0 Then
  178.                     retVal(UBound(retVal)) = Left(x, i - 1)
  179.                     ReDim Preserve retVal(UBound(retVal) + 1)
  180.                     x = Mid(x, i + 1)
  181.                     i = 1
  182.                 End If
  183.                 i = i + 1
  184.             Wend
  185.         End If
  186.     End If
  187.     Split2 = retVal
  188. End Function
  189.  
  190.  
  191.  
  192. '' Code to make a form stay on top
  193. ''
  194. Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
  195. Private Const HWND_TOPMOST = -1
  196. Private Const HWND_NOTOPMOST = -2
  197. Private Const SWP_NOMOVE = 2
  198. Private Const SWP_NOSIZE = 1
  199. Private Const SWP_WNDFLAGS = SWP_NOMOVE Or SWP_NOSIZE
  200.  
  201. Private Sub setTopmost(frm As Form, bTopmost As Boolean)
  202.     SetWindowPos frm.hWnd, IIf(bTopMost, HWND_TOPMOST, HWND_NOTOPMOST), 0, 0, 0, 0, SWP_WNDFLAGS
  203. End Sub
  204.  
  205.  
  206.  
  207. ''  Code to open a webpage from inside your app
  208. ''
  209. Private Declare Function GetDesktopWindow Lib "user32" () As Long
  210. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
  211. Public Sub StartDoc(ByVal DocName As String)
  212.     ShellExecute GetDesktopWindow(), "Open", DocName, "", "C:\", 1
  213. End Sub