While no one was harmed in the building of the Indian consulate, four attackers were killed in gunfight with Afghan and Indian security personnel, said local media reports.
Indian government vehemently condemned the attack as a terrorist attack and an Afghan diplomat in Delhi said it would strengthen Kabul's determination to keep the friendship with India on the right course.
Indian and Afghan officials said Afghan President Hamid Karzai spoke to Modi and assured him that Kabul "will do all to protect Indian missions there".
Indian External affairs ministry said Modi thanked Karzai for the "efforts of Afghan forces in thwarting (the) attack on India's consulate in Herat".
"President Karzai and I talked about Consulate attack in Herat. He assured me he will do everything to protect India's Missions in Afghanistan," Modi tweeted later, while praising the consulate staff in Herat and thanked the security forces for thwarting the terror attack.
Modi will be sworn in as prime minister on Monday, May 26. Karzai is among the SAARC leaders expected to attend the event.
Modi has also invited Pakistani Prime Minister Nawaz Sharif to attend the ceremony. Pakistan is yet to reply whether Sharif will come to the Indian capital or not.
Indian embassy and consulates have come under attack in Afghanistan for a couple of times in the past.
On February 26, 2010, a combined suicide bombing and shooting attack was launched by militants on a building inhabited by Indian doctors in Kabul. At least 18 people were killed and 36 more were injured in the attack.