Monday, September 13, 2021

Creating and sending a mail using default folder using outlook in python

 import win32com.client as client
outlook=client.Dispatch("Outlook.Application")
#message=outlook.Createitem(0)
namespace=outlook.GetNameSpace('MAPI')
inbox=namespace.GetDefaultFolder(6)
message=inbox.items.add
message.To="allsourav@gmail.com;allsourav2@gmail.com"
message.CC="souravandamiya@gmail.com"
message.BCC="allsourav2@gmail.com"
message.Subject="Happy Birthday"
message.Body="Wish you a happy birthday ,learning outlook using python again"
message.Save()
message.Display()
message.Send()

Creating a mail and send it using outlook in python

install pywin32

 

pip install pywin32

 

 

import win32com.client as client
outlook=client.Dispatch("Outlook.Application")
message=outlook.Createitem(0)
#namespace=outlook.GetNameSpace('MAPI')
#inbox=namespace.GetDefaultFolder(6)
message.To="allsourav@gmail.com;allsourav2@gmail.com"
message.CC="souravandamiya@gmail.com"
message.BCC="allsourav2@gmail.com"
message.Subject="Happy Birthday"
message.Body="Wish you a happy birthday ,learning outlook using python"
#message.Save()
message.Display()
#message.Send()


Sunday, February 14, 2021

Get prices of the products from amazon and flipkart automatically using VBA

 Option Explicit

Sub Search()


Dim sh As Worksheet
Set sh = ActiveSheet

sh.Range("A1").Value = 1

Call Fetch_from_Amazon
    Application.Wait Now + TimeValue("0:00:10")
Call Fetch_from_Flipkart

MsgBox "Done"

End Sub

Sub Fetch_from_Amazon()
 
Dim sh As Worksheet
Set sh = ActiveSheet


Dim i As Integer

Dim IE As InternetExplorer
Dim html_doc As HTMLDocument

Set IE = New InternetExplorer

IE.Visible = True
IE.navigate "WWW.Amazon.in"
Application.Wait (Now + TimeValue("0:00:10"))

'Do Until IE.readyState = READYSTATE_COMPLETE
'    DoEvents
'Loop

''Do Until IE.readyState = 4
''        DoEvents
''    Loop
''
''    Application.Wait (Now + TimeValue("0:00:10"))
''
''    Do Until IE.readyState = 4
''        DoEvents
''    Loop
'
''Set the max time to load
'
'Dim maxLoadingTime As Single, myTimer As Single
'maxLoadingTime = 10     '< -- # of seconds to allow page to load -- <
'myTimer = Timer
'
'Do
'    DoEvents
'    If Timer >= maxLoadingTime + myTimer Then
'        Debug.Print Time & " Notice: Connection Error. Refreshing webpage"
'        IE.stop
'        IE.Refresh
'    End If
'Loop Until IE.readyState = 4


Set html_doc = IE.document

For i = 4 To sh.Range("A" & Application.Rows.Count).End(xlUp).Row
    DoEvents
    
    If sh.Range("A1").Value = 0 Then Exit Sub
    
    On Error Resume Next
    
    html_doc.getElementById("twotabsearchtextbox").Value = sh.Range("A" & i).Value
    html_doc.getElementsByClassName("nav-input")(1).Click
    
    Do Until IE.readyState = READYSTATE_COMPLETE
        DoEvents
    Loop
    
    Application.Wait Now + TimeValue("0:00:02")
    
    sh.Range("B" & i).Value = html_doc.getElementsByClassName("a-size-medium a-color-base a-text-normal")(0).innerText
    sh.Range("C" & i).Value = html_doc.getElementsByClassName("a-price-whole")(0).innerText
     
Next i

IE.Quit


End Sub

Sub Fetch_from_Flipkart()

Dim sh As Worksheet
Set sh = ActiveSheet

Dim IE As InternetExplorer
Dim html_doc As HTMLDocument
Dim i As Integer

Set IE = New InternetExplorer

IE.Visible = True
IE.navigate "WWW.Flipkart.com"


Do Until IE.readyState = READYSTATE_COMPLETE
    DoEvents
Loop

'SendKeys "{ESC}"
'DoEvents

Set html_doc = IE.document

For i = 4 To sh.Range("A" & Application.Rows.Count).End(xlUp).Row

    If sh.Range("A1").Value = 0 Then Exit Sub
    
    DoEvents
    On Error Resume Next
    Dim searchtext As String
    
    html_doc.getElementsByClassName("_3704LK")(0).Value = sh.Range("A" & i).Value
    searchtext = sh.Range("A" & i).Value
    'html_doc.getElementsByClassName("L0Z3Pu")(0).Click
    'ie.navigate "http://www.bestbuy.com/site/searchpage.jsp?st=" & searchtext & "&_dyncharset=UTF-8&id=pcat17071&type=page&sc=Global&cp=1&nrp=&sp=&qp=&list=n&iht=y&usc=All+Categories&ks=960&keys=keys"
    IE.navigate "https://www.flipkart.com/search?q=" & searchtext & "&otracker=search&otracker1=search&marketplace=FLIPKART&as-show=on&as=off&as-pos=1&as-type=HISTORY"
    Do Until IE.readyState = READYSTATE_COMPLETE
        DoEvents
    Loop
    
    Application.Wait Now + TimeValue("0:00:03")
    
    sh.Range("D" & i).Value = html_doc.getElementsByClassName("_4rR01T")(0).innerHTML
    sh.Range("E" & i).Value = html_doc.getElementsByClassName("_30jeq3 _1_WHN1")(0).innerHTML

Next i

IE.Quit

End Sub



Sub Clear_Sheet()

Dim sh As Worksheet
Set sh = ActiveSheet

sh.Range("B4:E" & Application.Rows.Count).ClearContents

End Sub


Sub Stop_Macro()

Dim sh As Worksheet
Set sh = ActiveSheet

sh.Range("A1").Value = 0

End Sub


I changed some code to make it work,however originally it was in this video


https://youtu.be/0JHbb5-elMU

Tuesday, January 12, 2021

Condition based mass mailing using outlook and vba

 Option Explicit

Sub Send_Email_with_Signature()

Dim Outlook_App As Object
Dim msg As Object
Dim sign As String
Dim i As Integer
Set Outlook_App = CreateObject("Outlook.Application")
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Data")
 
For i = 3 To sh.Range("B" & Application.Rows.Count).End(xlUp).Row

If sh.Range("A" & i).Value = "" Then  ''Check Skip
    
    Set msg = Outlook_App.CreateItem(0)
    
    With msg
       ' .display
    End With
    
    sign = msg.htmlbody
    
    With msg
        .To = sh.Range("C" & i).Value
        .Subject = "Payment Reminder"
        .htmlbody = "Dear <b>" & sh.Range("B" & i).Value & "</b>,<br><br><p>Please pay your bill for below given service(s)- </p>" & _
        "<ul>" & _
        IIf(sh.Range("D" & i).Value <> "", "<li><b style='color:DodgerBlue'><u>" & sh.Range("D2").Value & ":</u></b>  " & Format(sh.Range("D" & i).Value, "0.0") & " is pending.</li>", "") & _
        IIf(sh.Range("E" & i).Value <> "", "<li><b style='color:Tomato;'><u>" & sh.Range("E2").Value & ":</u></b>  " & Format(sh.Range("E" & i).Value, "0.0") & " is pending.</li>", "") & _
        IIf(sh.Range("F" & i).Value <> "", "<li><b style='color:green;'><u>" & sh.Range("F2").Value & ":</u></b>  " & Format(sh.Range("F" & i).Value, "0.0") & " is pending.</li>", "") & _
        IIf(sh.Range("G" & i).Value <> "", "<li><b style='color:Orange;'><u>" & sh.Range("G2").Value & ":</u></b>  " & Format(sh.Range("G" & i).Value, "0.0") & " is pending.</li>", "") & _
        IIf(sh.Range("H" & i).Value <> "", "<li><b style='color:Blue;'><u>" & sh.Range("H2").Value & ":</u></b>  " & Format(sh.Range("H" & i).Value, "0.0") & " is pending.</li>", "") & _
        "</ul>" & _
        sign
    
        If sh.Range("H1").Value = 1 Then  ''' check option button value
            .send
        Else
            .display
        End If
    End With
    
    Set msg = Nothing

End If

Next i


Set Outlook_App = Nothing

If sh.Range("H1").Value = 1 Then MsgBox "Done"

End Sub



Source:https://www.youtube.com/watch?v=A6tggcOV6ts

Send email in outlook with excel sheet snapshot using VBA

 Option Explicit

Sub Send_Email_With_snapshot()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Sheet1")

Dim lr As Integer
lr = sh.Range("A" & Application.Rows.Count).End(xlUp).Row

sh.Range("A1:H" & lr).Select

With Selection.Parent.MailEnvelope.Item
    .to = sh.Range("L6").Value
    .cc = sh.Range("L7").Value
    .Subject = sh.Range("L8").Value
    .attachments.Add "C:\Users\allso\Desktop\new vba projects\arnab first class.xlsx"
    .send
End With

MsgBox "Done"


End Sub

 


 

 

Source:https://www.pk-anexcelexpert.com/send-email-with-snapshot/

 Source:https://www.youtube.com/watch?v=aD-lo81I5C4