Thursday, 22 August 2013

Send Mail from MS Outlook

Function SendMailFromOutlook(sMailto,sMailCC,sSubject,SBody,sAttachment,MyName)
 
 
        set oMailobj=CreateObject("Outlook.Application")
        
        set oSendmail=oMailobj.CreateItem(0)
        oSendmail.To=sMailto
        oSendmail.Subject=sSubject
        oSendmail.CC=sMailCC
        oSendmail.HTMLBody = sBody & "<br><br><b> -Regards</b><br>" & MyName
         If (sAttachment <> "") Then
                 oSendmail.Attachments.Add(sAttachment)
              End If
 
        oSendmail.Send
        set oSendmail=Nothing
        set oMailobj=Nothing
End Function

Example: Call the above Function with the values
(1)
sMailto="rohan@gmail.com;sameep@yahoo.com"
sMailCC="richa@gmai.com;"
sSubject="Party at home"
sBody=" be there at 10"
sAttachment="c:\partyimage.jpeg"
MyName="Mitesh"

Call SendMailFromOutlook(sMailto,sMailCC,sSubject,SBody,sAttachment,MyName)

(2) Or you can call this function by using inline values

Call SendMailFromOutlook("mitesh@gmai.com","","mysubject","how are you","","Rohan")



OUTPUT
=======
 Mail will be sent from Outlook in your machine

Tuesday, 28 May 2013

EXCEL - Get Values from a Cell by matching values from some other cell in same row


I found that there is a Vlookup function in Excel which returns the value from a column by comparing values from some other column. Followinf function also works like that. User have to enter following values
 
myfile = Excel File path

SheetName = Excel Sheet Name

Columnametocheck = Name of the source column

valtoCheck = Value to match

ColumnNameforReturnValues =   Will return the value from this coulmn


Ex.
getCellValues (myfile,mySheetname, "myColumn1", "B","myColumn3")
will search "B" in mycolumn1  and return the correspoding value from myColumn3 that is "2"

myColumn1
myColumn2
myColumn3
A
X
1
B
Y
2
C
Z
3



Function getCellValues (Excelfile,Sheetname, Columnametocheck, valtocheck, ColumnNameforReturnValues )
    returnvalue =""

   '--connecting Excel---
    set bdayExcel  =CreateObject("Excel.Application")
    bdayExcel.Visible = true
    bdayExcel.Workbooks.Open(Excelfile)

    '---connect with sheet --
    Set xlSheet =bdayExcel.Sheets(Sheetname)

   '-- get SourceColumn Number--
    colnumber = xlSheet.Range("A1:z7").find(Columnametocheck).column

   ' ---- Get Return Coulumn Number---
    returncolnumber = xlSheet.Range("A1:z7").find(ColumnNameforReturnValues).column


'--LOGIC---
    UsedRows= xlSheet.UsedRange.rows.count

    for i = 1 to usedRows
        val=xlSheet.Cells(i,colnumber).value
     
        if val = valtocheck then

            if len(returnvalue) = 0 then
                returnvalue = xlSheet.Cells(i,returncolnumber).value
             Else
                returnvalue =  returnvalue & "," & xlSheet.Cells(i,returncolnumber).value
            End If
        End IF
    Next


'---return found value---
getRowNumbers = returnvalue

End Function

Monday, 20 May 2013

Print some charaters from a string

'Set printlength for total number of character to be displayed
printLength = 4

'Source String 
myString ="12345678ABCDEFGHIJ"

'Logic
stringLength = len(myString)

for i =0 to stringLength / printLength
     myAns = myAns & vbcrlf & mid(myString  ,i*printLength +1,printLength)
next
msgbox myAns


OUTPUT -  message box with these output
=======
1234 
5678
ABCD
EFGH
IJ