Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 8th, 2011, 12:42 PM
Registered User
 
Join Date: Sep 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Highlight a row based on cell content

Hi,

I am writing a code that will copy outlook emails to an excel spreadsheet. I have figured out how to get the emails into excel but I need help to highlight a row based on what is in the column 'Subject'. Specifically, if the subject of an email is 'Re: *', I need the entire row to be highlighted (color doesn't matter). Here is what I have so far:

Sub ExportToExcel()

On Error GoTo ErrHandler

Dim appExcel As Excel.Application

Dim wkb As Excel.Workbook

Dim wks As Excel.Worksheet

Dim rng As Excel.Range

Dim strSheet As String

Dim strPath As String

Dim intRowCounter As Integer

Dim intColumnCounter As Integer

Dim msg As Outlook.MailItem

Dim nms As Outlook.NameSpace

Dim fld As Outlook.MAPIFolder

Dim itm As Object

strSheet = "OutlookItems" & Format(Date, "dd-mm-yyyy") & ".xls"

strPath = "C:\Documents and Settings\atret2\Outlook to Excel\"

strSheet = strPath & strSheet

Debug.Print strSheet
'Select export folder
Set nms = Application.GetNamespace("MAPI")

Set fld = nms.PickFolder
'Handle potential errors with Select Folder dialog box.
If fld Is Nothing Then
MsgBox "There are no msgs"
End If
'Open and activate Excel workbook.
Set appExcel = CreateObject("Excel.Application")

appExcel.Workbooks.Open (strSheet)

Set wkb = appExcel.ActiveWorkbook

Set wks = wkb.Sheets(1)

wks.Activate

appExcel.Application.Visible = True
'Copy field items in mail folder.

For Each itm In fld.Items

intColumnCounter = 1

Set msg = itm

intRowCounter = intRowCounter + 1

Set rng = wks.Cells(intRowCounter, intColumnCounter)

rng.Value = msg.To

intColumnCounter = intColumnCounter + 1

Set rng = wks.Cells(intRowCounter, intColumnCounter)

rng.Value = msg.SenderEmailAddress

intColumnCounter = intColumnCounter + 1

Set rng = wks.Cells(intRowCounter, intColumnCounter)

rng.Value = msg.Subject

intColumnCounter = intColumnCounter + 1

Set rng = wks.Cells(intRowCounter, intColumnCounter)

rng.Value = Format(msg.SentOn, "MM-DD")

intColumnCounter = intColumnCounter + 1

Set rng = wks.Cells(intRowCounter, intColumnCounter)

rng.Value = Format(msg.ReceivedTime, "MM-DD")


Next itm

Set appExcel = Nothing

Set wkb = Nothing

Set wks = Nothing

Set rng = Nothing

Set msg = Nothing

Set nms = Nothing

Set fld = Nothing

Set itm = Nothing

Exit Sub

ErrHandler: If Err.Number = 1004 Then

MsgBox strSheet & " doesn't exist"

Else

MsgBox Err.Number & "; Description: "

End If

Set appExcel = Nothing

Set wkb = Nothing

Set wks = Nothing

Set rng = Nothing

Set msg = Nothing

Set nms = Nothing

Set fld = Nothing

Set itm = Nothing

End Sub

Please help!!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Selected Cell Highlight RollingWoodFarm Excel VBA 1 December 3rd, 2006 11:10 AM
Highlight Datagrid Cell derekl ASP.NET 1.0 and 1.1 Basics 1 July 28th, 2006 06:12 AM
if the cell content is a part of another cell cont sriramus Excel VBA 1 November 15th, 2005 10:20 AM
Row highlight with mousemove hello2lu Access VBA 0 July 14th, 2004 04:25 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.