Click to See Complete Forum and Search --> : Type mismatch


jytioh
05-27-2004, 01:37 AM
I am currently migrating my website from a windows 2000 server to a windows 2003 server. However, one of the functions was not working on the new server. This function is used to convert url to hyperlinks automatically. I got the error 'type mismatch' on the new server which i have no problem with the previous.

The function uses regular expressions which from my knowledge requires at least VB scripting version 5.0. I've checked the new server and the version is 5.6 which should not be a problem.

Below are my codes. If you have some codes which does not require regular expressions, please suggest to me.

Thank you.

===========================================================
<%
'***** BEGIN FUNCTIONS *****
' This function takes a string as input and links any http's it
' finds so that they are then clickable in a browser. If only
' looks for http:// so www.asp101.com alone wouldn't link, but
' http://www.asp101.com would.
Function LinkURLs(strInput)
Dim iCurrentLocation ' Our current position in the input string
Dim iLinkStart ' Beginning position of the current link
Dim iLinkEnd ' Ending position of the current link
Dim strLinkText ' Text we're converting to a link
Dim strOutput ' Return string with links in it

' Start at the first character in the string
iCurrentLocation = 1

' Look for http:// in the text from the current position to
' the end of the string. If we find it then we start the
' linking process otherwise we're done because there are no
' more http://'s in the string.
Do While InStr(iCurrentLocation, strInput, "http://", 1) <> 0
' Set the position of the beginning of the link
iLinkStart = InStr(iCurrentLocation, strInput, "http://", 1)

' Set the position of the end of the link. I use the
' first space as the determining factor.
iLinkEnd = InStr(iLinkStart, strInput, " ", 1)

' If we didn't find a space then we link to the
' end of the string
If iLinkEnd = 0 Then iLinkEnd = Len(strInput) + 1

' Take care of any punctuation we picked up
Select Case Mid(strInput, iLinkEnd - 1, 1)
Case ".", "!", "?"
iLinkEnd = iLinkEnd - 1
End Select
Select Case Mid(strInput, iLinkEnd - 2, 1)
Case ".."
iLinkEnd = iLinkEnd - 2
End Select

' This adds to the output string all the non linked stuff
' up to the link we're curently processing.
strOutput = strOutput & Mid(strInput, iCurrentLocation, iLinkStart - iCurrentLocation)

' Get the text we're linking and store it in a variable
strLinkText = Mid(strInput, iLinkStart, iLinkEnd - iLinkStart)

' Build our link and append it to the output string
strOutput = strOutput & "<A HREF=""" & strLinkText & """ target=""_blank"" style=""text-decoration: none; color: #000066; font-weight: bold;"">" & strLinkText & "</A>"

' Some good old debugging
'Response.Write iLinkStart & "," & iLinkEnd & "<BR>" & vbCrLf

' Reset our current location to the end of that link
iCurrentLocation = iLinkEnd
Loop

' Tack on the end of the string. I need to do this so we
' don't miss any trailing non-linked text
strOutput = strOutput & Mid(strInput, iCurrentLocation)

' Set the return value
LinkURLs = strOutput
End Function 'LinkURLs
%>

buntine
05-27-2004, 10:26 PM
Thats a pretty nifty little function.. On which line is the error produced? This may help us define the problem.

As stated by the name, the 'Type Mismatch' error is produced when pass a parameter of the wrong data type to a function or sub routine.

Regards.

NCit
06-01-2004, 11:58 AM
BTW, I don't see any Regular Expression in your function (maybe I'm blind :) ). If you want another function (which uses really Regular Expression), I'm writing it down.


<%
'I once got this function from www.4guysfromrolla.com
'Usage : InsertHyperlinks(strText)

Function InsertHyperlinks(inText)
Dim objRegExp, strBuf
Dim objMatches, objMatch
Dim Value, ReplaceValue, iStart, iEnd

strBuf = ""
iStart = 1
iEnd = 1
Set objRegExp = New RegExp

objRegExp.Pattern = "\b(www|http|\S+@)\S+\b" ' Match URLs and emails
objRegExp.IgnoreCase = True ' Set case insensitivity.
objRegExp.Global = True ' Set global applicability.
Set objMatches = objRegExp.Execute(inText)
For Each objMatch in objMatches
iEnd = objMatch.FirstIndex
strBuf = strBuf & Mid(inText, iStart, iEnd-iStart+1)
If InStr(1, objMatch.Value, "@") Then
strBuf = strBuf & GetHref(objMatch.Value, "EMAIL", "_BLANK")
Else
strBuf = strBuf & GetHref(objMatch.Value, "WEB", "_BLANK")
End If
iStart = iEnd+objMatch.Length+1
Next
strBuf = strBuf & Mid(inText, iStart)
InsertHyperlinks = strBuf
End Function


Function GetHref(url, urlType, Target)
Dim strBuf

strBuf = "<a href="""
If UCase(urlType) = "WEB" Then
If LCase(Left(url, 3)) = "www" Then
strBuf = "<a href=""http://" & url & """ Target=""" & _
Target & """>" & url & "</a>"
Else
strBuf = "<a href=""" & url & """ Target=""" & _
Target & """>" & url & "</a>"
End If
ElseIf UCase(urlType) = "EMAIL" Then
strBuf = "<a href=""mailto:" & url & """ Target=""" & _
Target & """>" & url & "</a>"
End If

GetHref = strBuf

End Function
%>