bulk emails with excel - excel

Trying to create a bulk email workbook out of Excel using VBA code which includes embedded images. I'm unable to apply a "For i" to the code and can't figure out how to email from an entire list with a ListObject table. For the script below, the Sheet referenced is "Message Generator." I'm trying to send an individual email to everyone in the list until the value in the row in Column B = 0. However, the Integer I set for the loop seems to return the value 0, as though there are no values in the rows and columns at all.
Anyone know how I can send create a workbook to send bulk emails? See below for the script. Thank you!
Dim MainWB As Workbook
Dim olApp As Outlook.Application
Dim olemail As Outlook.MailItem
Dim fso As Scripting.FileSystemObject
Dim ts As Scripting.TextStream
Dim SigPath As String, SigText As String
SigPath = Environ("AppData") & "\Microsoft\Signatures\New.htm"
Set fso = New Scripting.FileSystemObject
Set ts = fso.OpenTextFile(SigPath)
SigText = ts.ReadAll
ts.Close
Set fso = Nothing
Set MainWB = ActiveWorkbook
Dim Subject As String
Dim Body As String
Dim i As Integer
Dim l As Integer
l = NumberOfNonBlankRowsInColumn(2) - 2 'subtract 2 header rows
Set olApp = New Outlook.Application
For i = 0 To l
Set olemail = olApp.CreateItem(olMailItem)
Subject = MainWB.Sheets("Message Generator").Range("B3").Offset(i, 0).Value
Body = MainWB.Sheets("Message Generator").Range("AB3").Offset(i, 0).Value
With olemail
.BodyFormat = olFormatHTML
.To = "UTOAI#outlook.com"
.Subject = Subject
.Body = Body
.Attachments.Add "C:\Users\Jacka\Documents\Test\logo.jpg"
.HTMLBody = "<img src='cid:logo.jpg'" & "width='309.5' height='39.5'><br>" & _vbanewline & .HTMLBody & SigText
.Display
End With
Set olemail = Nothing
Next i
Set olApp = Nothing
End Sub
Function NumberOfNonBlankRowsInColumn(souceCol As Integer) As Integer
Dim NumberOfRowsInColumn As Integer, j As Integer
Dim CurrentRowValue As String
NumberOfRowsInColumn = Cells(Rows.Count, sourceCol).End(xlUp).row
For j = 1 To NumberOfRowsInColumn
CurrentRowValue = Cells(j, sourceCol).Value
If IsEmpty(CurrentRowValue) Or CurrentRowValue = "" Then
Exit For
End If
Next j
NumberOfNonBlankRowsInColumn = (j - 1)
End Function

Try not to make this mistake. The reason it worked sometimes and not others is because I had a value in the row above my column which later deleted. Therefore, I set NonBlankRowsInColumn to 2 (where the values begin) and done. See the edited function below.
Function NumberOfNonBlankRowsInColumn(souceCol As Integer) As Integer
Dim NumberOfRowsInColumn As Integer, j As Integer
Dim CurrentRowValue As String
NumberOfRowsInColumn = Cells(Rows.Count, sourceCol).End(xlUp).row
For j = 2 To NumberOfRowsInColumn
CurrentRowValue = Cells(j, sourceCol).Value
If IsEmpty(CurrentRowValue) Or CurrentRowValue = "" Then
Exit For
End If
Next j
NumberOfNonBlankRowsInColumn = (j - 1)
End Function

Related

How to export e-mail body in two different cells?

I want to export e-mail data from a specific folder by a range of dates.
The macro exports the received date and the body of the email.
The objective is to search for certain data that comes from the extracted body and show them in other rows.
Due to the 32767 character limit that Excel has in a cell, the bodies of some emails are not being fully exported.
Is there a way to export the body in two rows instead of one to avoid the Excel limitation?
Other suggestions to accomplish this process are appreciated.
Sub ImportEmails()
Dim OutlookApp As Outlook.Application
Dim OutlookNamespace As Outlook.Namespace
Dim IFolder As Outlook.MAPIFolder
Dim OutlookMail As Variant
Dim i As Integer
Dim wb As Workbook
Dim ws As Worksheet
Dim rng As Range
Set OutlookApp = New Outlook.Application
'Outlook connection
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
Set wb = ThisWorkbook
'Select the folder to export emails, depending on the userĀ“s folder name you must change it
Set IFolder = OutlookNamespace.GetDefaultFolder(olFolderInbox).Folders("Test")
Set ws = wb.Sheets("Imported")
i = 0
Application.ScreenUpdating = False
ws.Cells.Clear
'Create headers
ws.Range("A1").Value = "Date Time"
ws.Range("B1").Value = "Body"
'Condition to select the today date in case of blank and export the emails
If IsEmpty(Range("end_date").Value) = True Then
Range("end_date").Value = "=today()"
End If
'Exporting proccedure
For Each OutlookMail In IFolder.Items
'Date validation
If DateValue(OutlookMail.ReceivedTime) >= DateValue(Range("start_date")) And DateValue(OutlookMail.ReceivedTime) <= DateValue(Range("end_date")) Then
'Fill the worksheet cells with the emails
ws.Range("A2").Offset(i, 0).Value = OutlookMail.ReceivedTime
ws.Range("B2").Offset(i, 0).Value = OutlookMail.Body
i = i + 1
End If
Next OutlookMail
Application.ScreenUpdating = True
Set IFolder = Nothing
Set OutlookNamespace = Nothing
Set OutlookApp = Nothing
LRimpr = LastRow(ws)
Set rng = ws.Range("A2:B" & LRimpr)
'Sort the columns by newest to oldest using the worksheet last row
With rng
.Sort Key1:=.Cells(1), Order1:=xlDescending, _
Orientation:=xlTopToBottom, Header:=xlNo
End With
MsgBox "Emails are successfully imported", vbInformation, "Import complete"
ws.Activate
End Sub
If you would be happy exporting the email body in multiple cells in a single row then replace your line
ws.Range("B2").Offset(i, 0).Value = OutlookMail.Body
with
Const CHUNK_SIZE As Long = 32000
Dim segment As Long
segment = 0
Do While True
ws.Range("B2").Offset(i, segment).Value = Mid$(OutlookMail.Body, segment * CHUNK_SIZE + 1, CHUNK_SIZE)
segment = segment + 1
If segment * CHUNK_SIZE > Len(OutlookMail.Body) Then Exit Do
Loop
Adjust the value for CHUNK_SIZE to your requirements ... it controls the number of characters that will be put into each cell, with the last cell having the 'remaining' characters (or all the characters if the body has less characters than CHUNK_SIZE)
To split the body into cells in a column.
Option Explicit
Sub ImportEmails_SplitBody_MultipleRows()
' Reference Microsoft Outlook nn.n Object Library
Dim OutlookApp As Outlook.Application
Dim iFolder As Outlook.Folder
Dim iFolderItems As Outlook.Items
Dim j As Long
Dim OutlookItem As Object
Dim lenBody As Long
Dim maxLen As Long
Dim i As Long
Dim wb As Workbook
Dim ws As Worksheet
Dim rng As Range
Set OutlookApp = New Outlook.Application
' Select folder
Set iFolder = Session.GetDefaultFolder(olFolderInbox).Folders("Test")
' Sort items
Set iFolderItems = iFolder.Items
iFolderItems.Sort "[ReceivedTime]", True
Set wb = ThisWorkbook
Set ws = wb.Sheets("Imported")
i = 0
' Application is Excel. No impact on Outlook
'Application.ScreenUpdating = False
ws.Cells.Clear
'Create headers
ws.Range("A1").Value = "Date Time"
ws.Range("B1").Value = "Body"
'Condition to select today's date in case of blank
If IsEmpty(Range("end_date").Value) = True Then
Range("end_date").Value = "=today()"
End If
'Debug.Print Range("start_date")
'Debug.Print Range("end_date")
'Exporting procedure
maxLen = 32767
'Debug.Print " maxLen: " & maxLen
For j = 1 To iFolderItems.Count
'Date validation
If iFolderItems(j).Class = olMail Then
Set OutlookItem = iFolderItems(j)
'Debug.Print OutlookItem.Subject
If DateValue(OutlookItem.ReceivedTime) >= DateValue(Range("start_date")) And _
DateValue(OutlookItem.ReceivedTime) <= DateValue(Range("end_date")) Then
lenBody = Len(OutlookItem.Body)
Dim txt As String
txt = OutlookItem.Body
Dim lenTxt As Long
lenTxt = Len(txt)
Do Until lenTxt = 0
'Fill the worksheet cells with the emails
'Debug.Print " Len(txt): " & Len(txt)
If lenTxt > maxLen Then
ws.Range("A2").Offset(i, 0).Value = OutlookItem.ReceivedTime
ws.Range("B2").Offset(i, 0).Value = Left(txt, maxLen)
txt = Right(txt, Len(txt) - maxLen)
Else
ws.Range("A2").Offset(i, 0).Value = OutlookItem.ReceivedTime
ws.Range("B2").Offset(i, 0).Value = txt
txt = ""
End If
i = i + 1
lenTxt = Len(txt)
Loop
Set OutlookItem = Nothing
End If
End If
Next
Application.ScreenUpdating = True
Set iFolder = Nothing
Set iFolderItems = Nothing
Set OutlookApp = Nothing
MsgBox "Emails are successfully imported", vbInformation, "Import complete"
ws.Activate
End Sub

How to get the latest emails and append to the existing file instead of looping through all items using VBA?

I have code that loops through all Outlook emails under a subfolder and extracts the body of the email based on the subject. Code takes a lot of time to loop through all emails as there are thousands of them.
How do I modify the code to append data, extracted from the latest emails, to the existing file instead of looping through all the emails and overwriting again & again?
Let's say I want to run the code every day to get the prior day's email data.
Option Explicit
Sub FinalMacro()
Application.DisplayAlerts = False
Dim iCounter As Integer
'iCounter = 1
Dim wkb As Workbook
Set wkb = ThisWorkbook
Sheets("Sheet1").Cells.Clear
' point to the desired email
Const strMail As String = "emailaddress#outlook.com"
Dim oApp As Outlook.Application
Dim oMapi As Outlook.MAPIFolder
'Dim oMail As Outlook.MailItem
Dim x As Long, y As Long
Dim destCell As Range
Dim i As Long
Dim oItem As Object
With ActiveSheet
Set destCell = .Cells(Rows.Count, "A").End(xlUp)
End With
On Error Resume Next
Set oApp = GetObject(, "OUTLOOK.APPLICATION")
If (oApp Is Nothing) Then Set oApp = CreateObject("OUTLOOK.APPLICATION")
On Error GoTo 0
Set oMapi = oApp.GetNamespace("MAPI").Folders(strMail).Folders("inbox").Folders("Other mails")
For Each oItem In oMapi.Items
If oItem.Subject = "Volume data" Then
' get html table from email object
Dim HTMLdoc As MSHTML.HTMLDocument
Dim tables As MSHTML.IHTMLElementCollection
Dim table As MSHTML.HTMLTable
Set HTMLdoc = New MSHTML.HTMLDocument
With HTMLdoc
.Body.innerHTML = oItem.HTMLBody
Set tables = .getElementsByTagName("table")
End With
Dim t As Long, r As Long, c As Long
Dim eRow As Long
For t = 0 To tables.Length - 1
eRow = Sheets(1).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
For r = 0 To (tables(t).Rows.Length - 1)
For c = 0 To (tables(t).Rows(r).Cells.Length - 1)
Range("A" & eRow).Offset(r, c).Value = tables(t).Rows(r).Cells(c).innerText
Next c
Next r
eRow = Sheets(1).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Next t
Cells(eRow, 1) = "Date & Time of Receipt:" & " " & oItem.ReceivedTime
Cells(eRow, 1).Interior.Color = vbRed
Cells(eRow, 1).Font.Color = vbWhite
Cells(eRow, 1).Columns.AutoFit
Set oApp = Nothing
Set oMapi = Nothing
Set HTMLdoc = Nothing
Set tables = Nothing
wkb.Save '"C:\Users\Desktop\Trial_1.xlsm"
End If
Next oItem
Application.DisplayAlerts = True
End Sub
To quickly select (filter) latest emails, you can use Items.Restrict.
To use your workbook for the accumulative storage of information, you just need not to erase the sheet, but to find the last filled line and add the content from the letters after it.
Smth like (not tested):
Set oMapi = oApp.GetNamespace("MAPI").Folders(strMail).Folders("inbox").Folders("Other mails")
Set wkb = ThisWorkbook
Set append_ws = wkb.Sheets("Sheet1") ' this worksheet is for appending
'Sheets("Sheet1").Cells.Clear ' - remove this statement
' set filter to: non-flagged mailitems received < 1 day ago
flt = "[FlagStatus] <> 1 And [MessageClass]='IPM.Note' And [ReceivedTime]>='" & _
Format(Now - 1, "ddddd 0:00") & "'"
Set Restricted = oMapi.Items.Restrict(flt)
For I = Restricted.Count To 1 Step -1
Set oItem = Restricted(I)
If oItem.Subject = "Volume data" Then
content_from_email = "smth from letter" ' get the content from the letter
lastrow = append_ws.Cells(append_ws.Rows.Count, 1).End(xlUp).row + 1
append_ws.Cells(lastrow, 1).Value = content_from_email
oItem.MarkAsTask olMarkComplete ' set flag to the processed items
oItem.Save
End If
Next I

Email Body Loop Values

I am attempting to loop through a column (n=96) in my worksheet, when it comes across a value <10 I would like the macro to open outlook and email offset values (four columns across) from the values it found.
I've generated a working example though it seems to be limited to only one example I've tested. I think I am approaching it from the wrong angle.
Sub SendReminderMail()
Dim p As Long
Dim OutLookApp As Object
Dim OutLookMailItem As Object
Dim iCounter As Integer
Dim MailDest As String
'If MsgBox("Are you sure?", vbYesNo) = vbNo Then Exit Sub
Set OutLookApp = CreateObject("Outlook.application")
Set OutLookMailItem = OutLookApp.CreateItem(0)
p = 2
Do Until Trim$(Cells(p, 1).Value) = ""
If Cells(p, 1).Value <= 10 Then
Set OutLookMailItem = OutLookApp.CreateItem(0)
With OutLookMailItem
.To = "Emailaddress etc"
.Subject = "Reminder: " & Cells(1, 7).Value
.Body = Cells(p, 1).Offset(0, 4).Value
.Display
End With
End If
p = p + 1
Loop
End Sub
How do I set it up to loop through all the <10 values and tell it to paste the offset values into the body of the email?
I think that you need to split this into two blocks of code.
First block would iterate through rows, check criteria and, if needed, call the second one, so the mail sending Sub, passing by necessary parameters.
Someting similar to the below code:
Sub SendReminderMail(ByVal MailSubject As String, mailBody As String)
Dim p As Long
Dim OutLookApp As Object
Dim OutLookMailItem As Object
Dim iCounter As Integer
Dim MailDest As String
'If MsgBox("Are you sure?", vbYesNo) = vbNo Then Exit Sub
Set OutLookApp = CreateObject("Outlook.application")
Set OutLookMailItem = OutLookApp.CreateItem(0)
Set OutLookMailItem = OutLookApp.CreateItem(0)
With OutLookMailItem
.To = "Emailaddress etc"
.Subject = MailSubject
.Body = mailBody
.Display
End With
End Sub
Sub IterateThroughRows()
Dim p As Integer
Dim Sht As Worksheet
Dim MailSubject As String
Dim mailBody As String
Set Sht = ThisWorkbook.Sheets("SheetName")
p = 2
Do Until Sht.Cells(p, 1).Value = ""
If Cells(p, 1).Value <= 10 Then
mailBody = mailBody + " | " + Sht.Cells(p, 1).Offset(0, 4).Value
End If
p = p + 1
Loop
Call SendReminderMail(MailSubject, mailBody)
MailSubject = "Reminder: " & Sht.Cells(1, 7).Value
End Sub

Unable to Send E-mails, run-time error 1004

While running this code i get run-time error 1004, "Application-defined object defined error". This error is showing up on the line starting with "NumRows = Worksheets("Data")" in the first function. Can someone just check on this code and let me know what's wrong here, i am new to VBA macros with limited knowledge.
Public Sub loopCheck()
Dim NumRows As Integer
Dim eID As String
Dim eName As String
Dim eEmail As String
Dim supportGroup As String
Dim managerEmail As String
Dim acName As String
Dim x As Integer
Application.ScreenUpdating = False
NumRows = Worksheets("Data").Range("A5", Range("A5").End(xlDown)).Rows.Count ' Set numrows = number of rows of data.
Worksheets("Data").Range("A5").Select ' Select first record.
For x = 1 To NumRows ' Establish "For" loop to loop "numrows" number of times.
eID = Worksheets("Data").Range("A" & x + 4).Value
eName = Worksheets("Data").Range("B" & x + 4).Value
eEmail = Worksheets("Data").Range("C" & x + 4).Value
supportGroup = Worksheets("Data").Range("F" & x + 4).Value
managerEmail = Worksheets("Data").Range("G" & x + 4).Value
acName = Worksheets("Data").Range("I" & x + 4).Value
'Prepare table to be sent locally.
Worksheets("Data").Range("AA5").Value = eID
Worksheets("Data").Range("AB5").Value = eName
Worksheets("Data").Range("AC5").Value = eEmail
Worksheets("Data").Range("AF5").Value = supportGroup
managerEmail = managerEmail + ";" + Worksheets("Data").Range("AA1").Value
'Call Emails function.
Call Emails(acName, eEmail, managerEmail)
ActiveCell.Offset(1, 0).Select
Next
Application.ScreenUpdating = True
End Sub
Public Sub Emails(x As String, y As String, z As String)
Dim outlook As Object
Dim newEmail As Object
Dim xInspect As Object
Dim pageEditor As Object
Dim a As String
Dim b As String
Dim c As String
a = y
b = z
c = x
Set outlook = CreateObject("Outlook.Application")
Set newEmail = outlook.CreateItem(0)
With newEmail
.To = a
.CC = b
.BCC = ""
.Subject = Worksheets("MF").Range("A1") & c
.Body = ""
.display
Set xInspect = newEmail.getInspector
Set pageEditor = xInspect.WordEditor
Worksheets("MF").Range("A9").Copy
pageEditor.Application.Selection.PasteAndFormat (wdFormatPlainText)
Worksheets("MF").Range("A3").Copy
pageEditor.Application.Selection.PasteAndFormat (wdFormatPlainText)
Worksheets("Data").Range("AA4:AF5").Copy
pageEditor.Application.Selection.PasteAndFormat (wdFormatPlainText)
Worksheets("MF").Range("A5").Copy
pageEditor.Application.Selection.PasteAndFormat (wdFormatPlainText)
Worksheets("MF").Range("A7").Copy
pageEditor.Application.Selection.PasteAndFormat (wdFormatPlainText)
.send
Set pageEditor = Nothing
Set xInspect = Nothing
End With
Set newEmail = Nothing
Set outlook = Nothing
End Sub
I have made some corrections in your code and it works at my end . Please try this. Mainly it relates to setting workbook and worksheets references properly otherwise your code seems to be okay:
Public Sub loopCheck()
Dim NumRows As Integer
Dim eID As String
Dim eName As String
Dim eEmail As String
Dim supportGroup As String
Dim managerEmail As String
Dim acName As String
Dim wb1 As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim x As Integer
Set ws1 = ThisWorkbook.Worksheets("Data") ' Set workbook & worksheet reference
Set ws2 = ThisWorkbook.Worksheets("MF") '' Set workbook & worksheet reference
NumRows = ws1.Range("A5", Range("A5").End(xlDown)).Rows.Count ' Set numrows = number of rows of data.
ws1.Range("A5").Select ' Select first record.
For x = 1 To NumRows ' Establish "For" loop to loop "numrows" number of times.
eID = ws1.Range("A" & x + 4).Value
eName = ws1.Range("B" & x + 4).Value
eEmail = ws1.Range("C" & x + 4).Value
supportGroup = ws1.Range("F" & x + 4).Value
managerEmail = ws1.Range("G" & x + 4).Value
acName = ws1.Range("I" & x + 4).Value
'Prepare table to be sent locally.
With ws1
.Range("AA5").Value = eID
.Range("AB5").Value = eName
.Range("AC5").Value = eEmail
.Range("AF5").Value = supportGroup
managerEmail = managerEmail + ";" + ws1.Range("AA1").Value
'Call Emails function.
Call Emails(acName, eEmail, managerEmail)
ActiveCell.Offset(1, 0).Select
End With
Next
Application.ScreenUpdating = True
End Sub
Public Sub Emails(x As String, y As String, z As String)
Dim outlook As Object
Dim newEmail As Object
Dim xInspect As Object
Dim pageEditor As Object
Dim a As String
Dim b As String
Dim c As String
Dim str As String
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
a = y
b = z
c = x
Set outlook = CreateObject("Outlook.Application")
Set newEmail = outlook.CreateItem(0)
Set ws2 = ThisWorkbook.Worksheets("MF")
str = ws2.Range("A1").Value & c
With newEmail
.To = a
.CC = b
.BCC = ""
.Subject = str
.Body = ""
.Display
Set xInspect = newEmail.GetInspector
Set pageEditor = xInspect.WordEditor
Set ws1 = ThisWorkbook.Worksheets("Data")
ws2.Range("A9").Copy
pageEditor.Application.Selection.PasteAndFormat (wdFormatPlainText)
ws2.Range("A3").Copy
pageEditor.Application.Selection.Paste xlValuesAndFormat (wdFormatPlainText)
ws1.Range("AA4:AF5").Copy
pageEditor.Application.Selection.Paste xlValuesAndFormat (wdFormatPlainText)
ws2.Range("A5").Copy
pageEditor.Application.Selection.Paste xlValuesAndFormat (wdFormatPlainText)
ws2.Range("A7").Copy
pageEditor.Application.Selection.PasteAndFormat (wdFormatPlainText)
.Send
Set pageEditor = Nothing
Set xInspect = Nothing
End With
Set newEmail = Nothing
Set outlook = Nothing
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub
Either your worksheet must be active or you have to address your range like this:
NumRows = Worksheets("Data").Range("A5", Worksheets("Data").Range("A5").End(xlDown)).Rows.Count

Excel VBA loop through all hyperlinks in outlook html and copy to excel

Hi I have written some vba code to loop through all emails in a folder , but I am struggling to find a way to look for a hyperlink. copy the hyperlink to the next empty row in column A. copy the text beneath the hyperlink to Column B. Then look for next hyperlink and repeat process. At present my code copies everything from the email and the hyperlinks are showing actual link not the visible wording.
Code
Option Explicit
Sub Get_Google_Alerts_From_Emails()
Sheet1.Select
ActiveSheet.Cells.NumberFormat = "#"
Application.DisplayAlerts = False
Dim oHTML As MSHTML.HTMLDocument: Set oHTML = New MSHTML.HTMLDocument
Dim ObjOutlook As Object
Dim MyNamespace As Object
Dim i As Integer
Dim j As Long
Dim abody() As String
Dim strSubject As String
Dim k
Dim x
Dim google_text As String
Dim strPattern As String
Dim strReplace As String
Dim strInput As String
Dim objRegex As Object
Set objRegex = CreateObject("vbscript.regexp")
Dim regEx As New RegExp
strPattern = "\s+"
strReplace = " "
x = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
Set ObjOutlook = GetObject(, "Outlook.Application")
Set MyNamespace = ObjOutlook.GetNamespace("MAPI")
k = MyNamespace.GetDefaultFolder(6).Folders("_Google_Alerts").Items.Count
For i = k To 1 Step -1
On Error GoTo vend
strSubject = MyNamespace.GetDefaultFolder(6).Folders("_Google_Alerts").Items(i).Subject
If strSubject Like "*Google*" Then GoTo google:
GoTo notfound
google:
abody = Split(MyNamespace.GetDefaultFolder(6).Folders("_Google_Alerts").Items(i).Body, vbCrLf)
For j = 0 To UBound(abody)
On Error GoTo error_google
If Len(abody(j)) > 1 Then
With regEx
.Pattern = "(https?[:]//([0-9a-z=\?:/\.&-^!#$;_])*)"
.Global = False
.IgnoreCase = True
End With
If regEx.Test(abody(j)) Then
google_text = regEx.Replace(abody(j), strReplace)
End If
With objRegex
.Pattern = "[A-Z]+"
.Global = True
.IgnoreCase = False
If .Test(abody(j)) Then
x = x + 1
Sheet1.Range("A" & x) = google_text
Sheet1.Range("C" & x) = strSubject
Else
End If
End With
End If
error_google:
Next j
MyNamespace.GetDefaultFolder(6).Folders("_Google_Alerts").Items(i).Move MyNamespace.GetDefaultFolder(6).Folders("_Google_Alerts_Complete")
GoTo comp
notfound:
comp:
Next i
vend:
Set ObjOutlook = Nothing
Set MyNamespace = Nothing
Application.DisplayAlerts = True
End Sub
At present my code copies everything from the email and the hyperlinks are showing actual link not the visible wording.
Here is a very basic example to achieve what you want. I am using Debug.Print to show the data. Feel free to amend it to move it to Excel. I am running this code from Excel.
Option Explicit
Const olMail As Integer = 43
Sub Sample()
Dim OutApp As Object
Dim MyNamespace As Object
Dim objFolder As Object
Dim olkMsg As Object
Dim objWordDocument As Object
Dim objWordApp As Object
Dim objHyperlinks As Object
Dim objHyperlink As Object
Set OutApp = CreateObject("Outlook.Application")
Set MyNamespace = OutApp.GetNamespace("MAPI")
'~~> Let the user select the folder
Set objFolder = MyNamespace.PickFolder
'~~> Loop through the emails in that folder
For Each olkMsg In objFolder.Items
'~~> Check if it is an email
If olkMsg.Class = olMail Then
'~~> Get the word inspector
Set objWordDocument = olkMsg.GetInspector.WordEditor
Set objWordApp = objWordDocument.Application
Set objHyperlinks = objWordDocument.Hyperlinks
If objHyperlinks.Count > 0 Then
For Each objHyperlink In objHyperlinks
Debug.Print objHyperlink.Address '<~~ Address
Debug.Print objHyperlink.TextToDisplay '<~~ Display text
Next
End If
End If
Next
End Sub

Resources