Tag Archives: Classic ASP

Classic ASP to Excel

Have you ever wondered how to go from a Classic ASP page to Excel? I have done this a lot usually someone in the office will ask, “Can I have the total profit of the month in a spreadsheet?” now you can handle this one of two ways go and copy all the data from the database one by one or you can extract the data and spit it out using ASP to Excel.

<%
‘The line below lets the browser know this is an Excel File
Response.ContentType = “application/vnd.ms-excel”
%>
<table>
<thead>
<tr>
<th style=”background-color:#CCCCCC;color:#FFFFFF;”>First Name</font></th>
<th style=”background-color:#CCCCCC;color:#FFFFFF;”>Last Name</font></th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
<tr>
<td>Steve</td>
<td>Smith</td>
</tr>
<tr>
<td>Frank</td>
<td>McCoy</td>
</tr>
</tbody>
</table>

Basically take the code above and paste it into your editor and save it as an ASP file run it and see the results.

Email Via Classic ASP

I know it has been a while since I sat down and wrote a decent post so tonight I figured let me discuss sending emails Classic ASP style. View the code below which will be explained in further detail:

<%
Dim strMailTo
Dim strMailSubject
Dim strMailBody
Dim objCDOMail ‘The CDO object

strMailSubject = “Test Via ASP”

strMailTo = Request.Form(”sendemail”)

strMailBody = “Hello World,”
strMailBody = strMailBody & “This is my first email.”
strMailBody = strMailBody & “Sent via ASP!!!”

Set objCDOMail = Server.CreateObject(”CDO.Message”)

objCDOMail.From = “mail@yourdomain.com”
objCDOMail.To = strMailTo
‘objCDOMail.Cc = “user@domain.com”
‘objCDOMail.Bcc = “user@domain.com”
objCDOMail.Subject = strMailSubject
objCDOMail.TextBody = strMailBody
objCDOMail.Send()
Set objCDOMail = Nothing
%>

Alright so the code above may looked complicated but it really isn’t. Basically the main part of this is the objCDOMail this is telling the server that you want to send a message via email. The variables dimed are just place holders for the objCDOMail parameters for example: strMailSubject is used to hold a string message for the subject line, objCDOMail.Subject.

First Letter Upper Case in Classic ASP

Today I want to illustrate a quick and easy way to change all first letters of words in a string to upper case using classic ASP.

<%
Function ChagetoUpperCase(X)
Dim lcstring
Dim oldcase
Dim NewArray
Dim newString
Dim IntegerI
Dim I

lcstring = CStr(LCase(X))
oldcase = ” ”
NewArray = Split(lcstring,” “)
For IntegerI = LBound(NewArray) To UBound(NewArray)
For I = 1 To Len(NewArray(IntegerI))

If Len(NewArray(IntegerI)) = 1 Then

newString = newString & UCase(NewArray(IntegerI)) & ” ”

ElseIf I=1 Then

newString = newString & UCase(Mid(NewArray(IntegerI), I, 1))

ElseIf I = Len(NewArray(IntegerI)) Then

newString = newString & Mid(NewArray(IntegerI), I, 1) & ” ”

Else

newString = newString & Mid(NewArray(IntegerI), I, 1)

End If

Next
Next
ChagetoUpperCase = Trim(newString)
End Function
%>

Then just call the function like this:

<%
Dim MyNewText
MyNewText = “hello everyone”

Dim NewOutPut
NewOutPut = ChagetoUpperCase(MyNewText)

Response.Write NewOutPut
%>

It should output this Hello Everyone. And that’s all there is to it.

Image Size from JavaScript to Classic ASP

I have spent the past day or so looking, thinking, and writing the perfect code that will return the dimensions of an image using JavaScript and place them in a cookie so that I may use it on an ASP page. Well after some research and thinking I came up with the following code.

<script type=”text/javascript”>
//Reference http://www.sovalid.com/blog
//Get and Assign Image
function GetImageSize(){
var img = new Image();
img.src = “http://www.google.com/intl/en_ALL/images/logo.gif”;
//Get Width and Height
wth = img.width;
hgt = img.height;

//Create the Two Cookies
document.cookie = “ImageWidth” + “=” +escape(wth)
document.cookie = “ImageHeight” + “=” +escape(hgt)

//Alert Width and Height as Test
wh=wth+” “+hgt;
alert(wh)
}

//Call Function
GetImageSize();
</script>

Reference my last post to see how to call the JavaScript cookie in Classic ASP. You can make the code as complex as you want this is just the basics, you can for example get the image from an ASP function instead of hard coding the image. You could also get the image from the page it self using document get element by id. Happy Coding.

Pass JavaScript Cookie to Classic ASP

Keeping up with yesterdays post I figured I would illustrate how to create a JavaScript cookie and pass it to Classic ASP.

This part was taken from http://www.w3schools.com for those of you that need a course on JavaScript Cookies:

<script type=”text/javascript”>
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + “=”);
if (c_start!=-1)
{
c_start=c_start + c_name.length+1 ;
c_end=document.cookie.indexOf(”;”,c_start);
if (c_end==-1) c_end=document.cookie.length
return unescape(document.cookie.substring(c_start,c_end));
}
}
return “”
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ “=” +escape(value)+((expiredays==null) ? “” : “; expires=”+exdate.toGMTString());
}

function checkCookie()
{
username=getCookie(’username’);
if (username!=null && username!=”")
{
alert(’Welcome again ‘+username+’!');
}
else
{
username=prompt(’Please enter your name:’,”");
if (username!=null && username!=”")
{
setCookie(’username’,username,365);
}
}
}
</script>

The real important part to take away from this is:

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ “=” +escape(value)+((expiredays==null) ? “” : “; expires=”+exdate.toGMTString());
}

Now we can edit this to what we need like so:

var exdate=new Date();
var CookieValue = ‘Hello World’

exdate.setDate(exdate.getDate()+1);
document.cookie=”CookieName”+ “=” +escape(CookieValue)+((’==null) ? “” : “; expires=”+exdate.toGMTString());

To call this Cookie all you need to do is:

<%
Response.Write Request.Cookies(”CookieName”)
%>

That’s all there is to it.