User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Visual Basic 4 / 5 / 6 section within the Software Development category of DaniWeb, a massive community of 456,528 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,741 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums
Views: 2373 | Replies: 6 | Solved
Reply
Join Date: Oct 2007
Posts: 5
Reputation: salman.paracha is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
salman.paracha salman.paracha is offline Offline
Newbie Poster

Help Compile Error: Object Required

  #1  
Oct 8th, 2007
Please help..

I am trying to create an outlook script that will parse the subject line of emails coming from a particular source and organize them, by the ticket number present in the subject line. However, VB is sucking the life out of me on the error below

I am getting a Compile Error: Object Required on the line
Dim subjectLine as String
Set subjectLine = Item.Subject.

If I take the same MailItem (Item) object and pass the Subject property to the MsgBox()
it works...So I am beat on this one bad.

Please view the complete script below...Any and all the help will be greatly appreciated.


 Sub CustomMailMessageRule(Item As Outlook.MailItem)
    Const folderInbox = 6
    Const ticketsFldName = "tickets"
    Const nutrioFldName = "nutrio"
    
    Dim objFolder As Outlook.Folder
    
    Set objOutlook = CreateObject("Outlook.Application")
    Set objNamespace = objOutlook.GetNamespace("MAPI")
    Set objFolder = objNamespace.GetDefaultFolder(folderInbox)
    
    Dim ticketsFolder As Outlook.Folder
    Dim nutrioFolder As Outlook.Folder
        
    ticketsFolder = objFolder.Folder(ticketsFldName)
    
    If (ticketsFolder Is Nothing) Then
     ticketsFolder = objFolder.Folders.Add(ticketsFldName)
     nutrioFolder = ticketsFolder.Folders.Add(nutrioFldName)
    
    ElseIf (nutrioFolder Is Nothing) Then
     nutrioFolder = ticketsFolder.Folders.Add(nutrioFldName)
    
    End If
    
    Dim subjectLine As String
    Set subjectLine = Item.Subject
    
    Set begIndexHash = InStr(0, "#", subjectLine)
    Set endIndexColon = InStr(0, ":", subjectLine)
    
    Dim ticketNumber As String
    If (begIndexHash = 0 Or endIndexColon = 0) Then
        Return
    Else
        Set ticketNumber = Mid$(subjectLine, begIndexHash + 1, (endIndexColon - begIndexHash) - 1)
    End If
    
    Set ticketNewFolder = nutrioFolder.Folders.Add(ticketNumber)
    Item.Move (ticketNewFolder)

End Sub
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Posts: 5
Reputation: salman.paracha is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
salman.paracha salman.paracha is offline Offline
Newbie Poster

Re: Compile Error: Object Required

  #2  
Oct 8th, 2007
I found a bug, which has not solved the problem but just wanted to update the code

[code] Sub CustomMailMessageRule(Item As Outlook.MailItem)
Const folderInbox = 6

Dim objFolder As Outlook.Folder

Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objFolder = objNamespace.GetDefaultFolder(folderInbox)

Dim ticketsFolder As Outlook.Folder
Dim nutrioFolder As Outlook.Folder

ticketsFolder = objFolder.Folders("tickets")

If (ticketsFolder Is Nothing) Then
ticketsFolder = objFolder.Folders.Add(ticketsFldName)
nutrioFolder = ticketsFolder.Folders.Add(nutrioFldName)

ElseIf (ticketsFolder.Folders("nutrio") Is Nothing) Then
nutrioFolder = ticketsFolder.Folders.Add(nutrioFldName)

End If

Dim subjectLine As String
Set subjectLine = Item.Subject

Set begIndexHash = InStr(0, "#", subjectLine)
Set endIndexColon = InStr(0, ":", subjectLine)

Dim ticketNumber As String
If (begIndexHash = 0 Or endIndexColon = 0) Then
Return
Else
Set ticketNumber = Mid$(subjectLine, begIndexHash + 1, (endIndexColon - begIndexHash) - 1)
End If

Set ticketNewFolder = nutrioFolder.Folders.Add(ticketNumber)
Item.Move (ticketNewFolder)

End Sub
[\code]
Reply With Quote  
Join Date: Oct 2007
Posts: 145
Reputation: hopalongcassidy is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 13
hopalongcassidy's Avatar
hopalongcassidy hopalongcassidy is offline Offline
Junior Poster

Re: Compile Error: Object Required

  #3  
Oct 8th, 2007
The problem is that the variable named "subjectLine" is defined as a String. A String is not an Object. When you use the "Set" statement, the compiler expects that the thing you are "Setting" is an object.

Hoppy
Reply With Quote  
Join Date: Oct 2007
Posts: 5
Reputation: salman.paracha is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
salman.paracha salman.paracha is offline Offline
Newbie Poster

Re: Compile Error: Object Required

  #4  
Oct 8th, 2007
Thanks for the reply....I was just about to post that solution myself...was trying different approaches

 Sub CustomMailMessageRule()
    Const folderInbox = 6
    Const ticketsFldName = "tickets"
    Const nutrioFldName = "nutrio"

    Set objOutlook = CreateObject("Outlook.Application")
    Set objNamespace = objOutlook.GetNamespace("MAPI")
    Set objFolder = objNamespace.GetDefaultFolder(folderInbox)
    
    Dim ticketsFolder As Outlook.MAPIFolder
    Dim nutrioFolder As Outlook.MAPIFolder
    
    ticketsFolder = objFolder.Folders(ticketsFldName)
    
    If (ticketsFolder Is Nothing) Then
     ticketsFolder = objFolder.Folders.Add(ticketsFldName)
     nutrioFolder = ticketsFolder.Folders.Add(nutrioFldName)
    
    ElseIf (ticketsFolder.Folders(nutrioFldName) Is Nothing) Then
     nutrioFolder = ticketsFolder.Folders.Add(nutrioFldName)
    End If
    
    Dim subjectLine As String
    subjectLine = Item.Subject
    
    Dim begIndexHash As Integer
    Dim endIndexColon As Integer
    
    begIndexHash = InStr(0, "#", subjectLine)
    endIndexColon = InStr(0, ":", subjectLine)
    
    Dim ticketNumber As String
    If (begIndexHash = 0 Or endIndexColon = 0) Then
        Return
    Else
        ticketNumber = Mid$(subjectLine, begIndexHash + 1, (endIndexColon - begIndexHash) - 1)
    End If
    
    Dim ticketNewFolder As Outlook.MAPIFolder
    ticketNewFolder = nutrioFolder.Folders.Add(ticketNumber)
    
    Item.Move (ticketNewFolder)
    
End Sub

But now I am getting another wierd error...maybe you can help with this

I am getting Run Time Error:
"The operation Failed: An object could not be found" on
ticketsFolder = objFolder.Folders(ticketsFldName)

All help will be greatly appreciated
Reply With Quote  
Join Date: Oct 2007
Posts: 145
Reputation: hopalongcassidy is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 13
hopalongcassidy's Avatar
hopalongcassidy hopalongcassidy is offline Offline
Junior Poster

Re: Compile Error: Object Required

  #5  
Oct 9th, 2007
Once again, "ticketsFolder" IS and object and requires a "Set". This distinction in syntax is kind of a stupid rule in VB, but it's something you have to live with.

Hoppy
Reply With Quote  
Join Date: Oct 2007
Posts: 5
Reputation: salman.paracha is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
salman.paracha salman.paracha is offline Offline
Newbie Poster

Re: Compile Error: Object Required

  #6  
Oct 10th, 2007
Thanks for your help...Here is the final script that works...

Sub CustomMailMessageRule(Item As Outlook.MailItem)
    On Error GoTo PROBLEM
    
    Const folderInbox = 6
    Const ticketsFldName = "tickets"
    Const nutrioFldName = "nutrio"
    
    Dim objFolder As Outlook.Folder
    Dim ticketsFolder As Outlook.Folder
    Dim nutrioFolder As Outlook.Folder
    
    Set objOutlook = CreateObject("Outlook.Application")
    Set objNamespace = objOutlook.GetNamespace("MAPI")
    Set objFolder = objNamespace.GetDefaultFolder(folderInbox)

    
    
    Set ticketsFolder = Nothing
    Set nutrioFolder = Nothing
       
    Set ticketsFolder = objFolder.Folders(ticketsFldName)

    If (ticketsFolder Is Nothing) Then
        Set ticketsFolder = objFolder.Folders.Add(ticketsFldName)
        Set nutrioFolder = ticketsFolder.Folders.Add(nutrioFldName)
    Else
        Set nutrioFolder = ticketsFolder.Folders(nutrioFldName)
        If (nutrioFolder Is Nothing) Then
            Set nutrioFolder = ticketsFolder.Folders.Add(nutrioFldName)
        End If
        
    End If
        
    Dim subjectLine As String
    Dim begIndexHas As Integer
    Dim endIndexColon As Integer
    Dim ticketNumber As String
    
    subjectLine = Item.Subject
    begIndexHash = InStr(subjectLine, "#")
    endIndexColon = InStr(begIndexHash, subjectLine, ":")

    If (begIndexHash = 0 Or endIndexColon = 0) Then
        Exit Sub
    Else
        ticketNumber = Mid$(subjectLine, begIndexHash + 1, (endIndexColon - begIndexHash - 1))
    End If
    
    Dim ticketNewFolder As Outlook.Folder
    
    Set ticketNewFolder = Nothing
    Set ticketNewFolder = nutrioFolder.Folders(ticketNumber)
    
    If (ticketNewFolder Is Nothing) Then
        Set ticketNewFolder = nutrioFolder.Folders.Add(ticketNumber)
    End If
    
    Item.Move ticketNewFolder
    
PROBLEM:
    If (Err.Number = 424) Then
    MsgBox (Err.Description & Err.Number)
    End If
    
    Resume Next
End Sub
Reply With Quote  
Join Date: Oct 2007
Posts: 5
Reputation: salman.paracha is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
salman.paracha salman.paracha is offline Offline
Newbie Poster

Re: Compile Error: Object Required

  #7  
Oct 10th, 2007
If a folder that you are trying to access does not exists, it will throw an exception, and therefore you will have to catch that exception and resume to the next line, where you will create that folder...
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Visual Basic 4 / 5 / 6 Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum

All times are GMT -4. The time now is 4:22 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC