PDA

View Full Version : outlook vba



mubaraklax
03-05-2012, 05:46 AM
Hi,
I tried to run the following code in outlook 2003. But i am getting error "Run time rror 424; object required" at line item.Body.Select. Could some one please help me what is the issue?

Sub savedailyownership()
Dim ns As NameSpace
Dim Inbox As MAPIFolder
Dim myitem As Outlook.MailItem
Dim FileName As String
Dim i As Integer
Dim objSearchFolder As Outlook.MAPIFolder
Dim item As Object
Dim mai As MailItem

Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
Set objSearchFolder = Inbox
i = 0
If Inbox.Items.Count = 0 Then
MsgBox "Inbox is Empty", vbInformation, "Nothing Found"
End If
For Each item In Inbox.Items
If item.Subject Like "Test outlook-Excel" Then
'item.Display
item.Body.Select
Selection.Copy
Dim xlApp As Object ' Excel.Application
Dim xlWkb As Object ' Excel.Workbook
Set xlApp = CreateObject("Excel.Application") ' New Excel.Application
Set xlWkb = xlApp.Workbooks.Add
xlApp.Visible = True
xlApp.Workbooks.Add
xlApp.Selection.Paste False, False, False
End If
Next
End Sub

JP2112
03-14-2012, 06:40 AM
Welcome to the board. Please format your VBA code using code tags like this:


' your code goes here


There is no Body.Select method. I think you are trying to copy and paste the body of an email into an Excel worksheet. If so, what you want to do is assign the Body property to a String variable, then set an Excel worksheet range to that variable. Ex:


' declare variable to hold email body
Dim emailbody As String

' ... skip ahead ...

If item.Subject Like "Test outlook-Excel" Then
'item.Display
emailbody = item.Body

' .... skip ahead ...

xlApp.Worksheets(1).Range("a1").value = emailbody

mubaraklax
03-14-2012, 12:35 PM
Hi JP,

thank you very much for your reply.