Excel vba code to loop through gmail emails in gmail inbox - excel

Does anyone have excel vba code to copy and paste to loop through gmail emails in a gmail inbox?
While there is a ton out there about:
1) sending gmail with excel vba;
2) looping through emails with excel vba in outlook; and
3) looping through gmail emails in a gmail inbox using other programming languages;
I couldn't find anything to loop through gmail emails in a gmail inbox.
I know I'm actually asking for quite the finished product. It's not that I'm hoping that anyone would be so kind as to write the code for me, but I am hoping that someone might have it already.
After all my attempts to tweak the code I was able to find relative to 1), 2) and 3) above, it became clear to me that I just need to go ahead and post this question. (Who knows, it may help a ton of other folks too.)

You just need to use the names of the folder, check the following
Sub SetFlagIcon()
Dim mpfInbox As Outlook.Folder
Dim obj As Outlook.MailItem
Dim i As Integer
Set mpfInbox = Application.GetNamespace("MAPI").Folders("john.smith#gmail.com").Folders("[Gmail]").Folders("Sent Mail")
' Loop all items in the Inbox\Test Folder
For i = 1 To mpfInbox.Items.Count
If mpfInbox.Items(i).Class = olMail Then
Set obj = mpfInbox.Items.Item(i)
For Each Recipient In obj.Recipients
If Recipient.Address = "myfriend#hotmail.com" Then
'Set the yellow flag icon
obj.FlagIcon = olYellowFlagIcon
obj.Save
End If
Next Recipient
End If
Next
End Sub

Related

What do Set olMail = Nothing Set olApp = Nothing do in a VBA code to send emails via outlook from excel?

I am trying to edit an existing macro that sends out each row of a spreadsheet as an email. I want the macro to send out these emails in batches instead of all at once.
I am using .DeferredDeliveryTime to do this.
The macro that I have has this at the end
Set olMail = Nothing
Set olApp = Nothing
My question is if I remove this part will my deferred emails still be sent. I am afraid these lines will close outlook and the emails won't be sent.
Setting to Nothing isn't necessary(in modern Excel versions 2010+). but there were issues with older versions of Excel (for which the workaround was to explicitly set).
It's just release an object and clear memory in VBA. For more information, you can refer the following link:
When should an Excel VBA variable be killed or set to Nothing?

Working with Outlook 365 on an windows enviroment

I know it's a general question.
I am trying to create an application/extension that reads the contents of an email before it is sent out. So basically if the email contains a 8 character numerical for example, it will prompt an alert box whether the employee is sure before it is sent out.
I have worked with some Python outlook modules for myself. But my task would for it to be able to run on most employee desktops which wouldn't have Python installed.
I am looking at tools such as Visual Studio but am unsure where I should start.
You can develop a VBA macro or Outlook COM add-in (for example, VSTO based one) for that. See Walkthrough: Creating Your First VSTO Add-In for Outlook to get started quickly.
Basically you need to handle the ItemSend event of the Application class which is fired whenever an Microsoft Outlook item is sent, either by the user through an Inspector (before the inspector is closed, but after the user clicks the Send button) or when the Send method for an Outlook item, such as MailItem, is used in a program.
For example, here is a VBA sample code:
Public WithEvents myOlApp As Outlook.Application
Public Sub Initialize_handler()
Set myOlApp = Outlook.Application
End Sub
Private Sub myOlApp_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim prompt As String
prompt = "Are you sure you want to send " & Item.Subject & "?"
If MsgBox(prompt, vbYesNo + vbQuestion, "Sample") = vbNo Then
Cancel = True
End If
End Sub
Finally, you may find the Getting Started with VBA in Outlook 2010 article helpful.

Email notification on submission (response) to Excel Survey

How can I receive an email notification every time a submission is made to an online Excel Survey?
You may subscribe as an alert to your email with the following steps:
Click on your document.
Choose File > Alert Me > Set alert on the document.
Choose your setting accordingly and DONE :)
However do not that, not only new subscription will trigger email, any changes will trigger email as well, sometime is quite annoying.
Try this. Forgive me as I don't have excel installed at home so I can't test to make sure it's right. This probably requires Outlook.
Sub CreateMail()
Dim myItem As Object
Set myItem = Application.CreateItem(olMailItem)
myItem.Subject = "Mail to myself"
myItem.To = "myself#myself.com"
myItem.body ="stuff"
myItem.send
End Sub

Extract data out from currently viewed Email via Excel VBA

I am new in Excel VBA and still trying hard to understand Excel VBA.
Am trying to make life easier for my team members.
Appreciate if someone can point me a direction on how to retrieve out text from the currently viewed email and paste the information on a Excel speadsheet.
Email will look something like this.
Name: Tan AK
Contact number: 65-12223456
=====================================
Would like to extract out the details
E.g
Cell A1 should display the first sentence
Cell A2 should display the second sentence.
Appreciate if someone can help me out.
General questions like yours are not popular here. Below I give information that should get you started. If you run into difficulties try a question like this:
I want to achieve A but the half-a-dozen statements below do B. I do not see what I am doing wrong.
Questions which focus on a single issue and demonstrate what you have tried are usually answered very quickly.
The Outlook security system makes it much more difficult for an Excel macro to read from Outlook than for an Outlook macro to write to Excel.
On the other hand, it is much easier to distribute an Excel macro because you can email the workbook to each of your friends and they can immediately use it. With an Outlook macro, you will have to export the module and each friend would then have to import it.
I suggest you start with an Outlook macro. When you have learnt enough to get that macro working to your satisfaction, you may know enough to start exploring the issues of reading from Outlook .
This little Outlook macro show how to access the selected emails:
Option Explicit
Public Sub DemoExplorer()
Dim Exp As Outlook.Explorer
Dim ItemCrnt As MailItem
Dim NumSelected As Long
Set Exp = Outlook.Application.ActiveExplorer
NumSelected = Exp.Selection.Count
If NumSelected = 0 Then
Debug.Print "No emails selected"
Else
For Each ItemCrnt In Exp.Selection
With ItemCrnt
Debug.Print "From " & .SenderName & " Subject " & .Subject
End With
Next
End If
End Sub
The Outlook macro in the answer below creates an Excel workbook and exports details of every email in the Inbox to it. This demonstrates many of the techniques you will need. Perhaps more importantly, it shows what the text and html bodies of an email look like to a VBA macro. This will help you split a body into sentences.
How to copy Outlook mail message into excel using VBA or Macros

How do I track who uses my Excel spreadsheet?

I created an Excel spreadsheet that my boss wants to put on the company's internal website. The spreadsheet contains some seldom-used, esoteric, but handy functions that only certain employees within the company will find really useful.
The problem is that I don't know who the future users are, and my boss wants me to identify who uses my spreadsheet.
He asked that I password-protect the Excel spreadsheet in such a way that one password does NOT unlock all of the copies that people can download from the site. For example, I can't just make the password "stackoverflow" because once a user legitimately gets the password from me, and is shared with other people, it can be used by anyone within the company to unlock all subsequently downloaded spreadsheets. I will never be able to ascertain who is using the spreadsheet. Also, I cannot modify the website, so I hope to achieve this tracking of users through Excel and email.
Is there a way to have Excel randomly generate a string, which the user emails me, and then I respond with the appropriate password that will unlock the file (based off the generated string)? This requires the user to check in with me before using the spreadsheet (the ideal situation).
Is such an arrangement possible in Excel 2010 Professional Plus?
I think password protection in the method you describe is unnecessarily cumbersome if it is even doable at all.
He asked that I password-protect the Excel spreadsheet in such a way that one password does NOT unlock all of the copies that people can download from the site.
I can't imagine how this might be possible using only Excel. Maybe an Add-in could do this, but at the file level, I don't think it could be done, at least not easily.
I will never be able to ascertain who is using the spreadsheet.
It sounds like this is the really important bit. You are not using the password as a security measure, only as a gatekeeping method to determine who is using the file. This can be automated in other ways, easiest of which would be to use certain Environment variables, e.g.:
MsgBox Environ("username") will display a message box with the current user's name.
You can assign Environ("username") to a string variable, and then you could for example automate Outlook to send you an email that "John Doe has opened the file", or something to that effect. If you want to avoid getting an email every time, you could do some tweaking with a Named Range variable in the Excel file, so that the macro will only send the email once, etc.
Alternatively, you may be able to write a log/txt file to a shared network location (of course, assuming the user is connected to the network) instead of sending emails.
Update
Here is some example code that I've taken from places around the web, it will send an email from the user. You will have to modify the sendTo lines to use your email address as recipient, etc.
Put this in the Workbook's code module, it should email you any time they open this file:
Option Explicit
Private Sub Workbook_Open()
' This example uses late-binding instead of requiring an add'l reference to the
' MS Outlook 14.0 Object Library.
Dim oApp As Object 'Outlook.Application 'Object
Dim ns As Object 'Namespace
Dim fldr As Object 'MAPIFolder
Dim mItem As Object 'Outlook.MailItem
Dim sendTo As Object 'Outlook.Recipient
Dim bOutlookFound As Boolean
On Error Resume Next
Set oApp = GetObject(, "Outlook.Application")
bOutlookFound = Err.Number = 0
On Error GoTo 0
If Not bOutlookFound Then Set oApp = CreateObject("Outlook.Application") 'New Outlook.Application
'# Set the namespace and folder so you can add recipients
Set ns = oApp.GetNamespace("MAPI")
Set fldr = ns.GetDefaultFolder(6) 'olFolderInbox
'# create an outlook MailItem:
Set mItem = oApp.CreateItem(0) 'olMailItem
'# assign a recipient
Set sendTo = mItem.Recipients.Add("YourName#Company.Com")
sendTo.Type = 1 'To olTo
'# assign another recipient
Set sendTo = mItem.Recipients.Add("YourManager#Company.Com")
sendTo.Type = 1
'# Validate the recipients (not necessary if you qualify valid email addresses:
For Each sendTo In mItem.Recipients
sendTo.Resolve
Next
mItem.Subject = "A user has opened the Excel file"
mItem.Body = "This is an automated message to inform you that " & _
Environ("username") & " has downloaded and is using the file."
mItem.Save
mItem.Send
'If outlook was not already open, then quit
If Not bOutlookFound Then oApp.Quit
Set oApp = Nothing
End Sub
Expanding on David's answer you could also use a macro that auto-runs when the sheet is opened and it could write Environ("username") to the next available row in a hidden worksheet. I've used Environ("username") before and it is quite useful, quick, and easy.
This sub on any worksheet will automatically run (IIRC):
Private Sub Auto_Open()
End Sub
You could also put a time stamp in the next column to show when the spreadsheet was used...

Resources