Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete emails with same subject 1

Status
Not open for further replies.

MikeAJ

Programmer
May 22, 2002
108
US
Hi,

Newbie here trying to write a script that runs when I click to open a folder, inside the "queryopen" event, which will delete any old emails that have the same subject as the new email that just came in. I have the folder sorted by date desc.

Code:
Sub Queryopen(Source As Notesuiview, Continue As Variant)
	Dim s As New NotesSession
	Dim dc As NotesDocumentCollection
	Dim doc As NotesDocument
	Dim subj As String
	
	Set dc = Source.Documents
	Set doc = dc.GetFirstDocument
	subj = doc.Subject
	Do Until (doc Is Nothing)
		Set doc = dc.GetNextDocument(doc)
		If doc.Subject = subj Then
			Msgbox(subj)
			'doc.DeletedBy = s.CommonUserName
			'doc.DeletedOn = Now
			'Call Doc.Save(True, False)
		End If
	Loop
End Sub

The "subj = doc.Subject" line is failing though, and I can't figure out why. Does anyone have a script which will delete older emails with the same subject, or does anyone see what's wrong with this one?

Thanks,
Mike
 
Hmm, so you're trying to run a script each time you open a view. That is fine in itself, but when you open the view, you haven't selected any documents.

Why is that important ? Because if you check the Documents properties of the Notesuiview object, you will notice that Documents returns a collection of selected documents, and when you open a view, you haven't selected any yet.

So your collection is empty, which is why your script is failing. That is also why it is always useful to include failure controls when getting objects. For example, if you had written a count check on your collection, like this :
Code:
    Set dc = Source.Documents
    if dc.count > 0 then
        Set doc = dc.GetFirstDocument
        ...
        Loop
    else
        Print "No documents selected"
    end if
Then you would have noted that your collection was empty.

Anyway, your collection is empty. I would suggest that you remove your code from the view and put it in an Agent, preferably an If New Mail agent.

By the way, I hope you're not doing this in your mail file. It would really be bad if your Calendar meeting updates could not find the source document.

Pascal.
 
Thanks for your insight, Pascal. This is my first attempt to write anything for Notes so I have everything to learn. Would it be possible to replace "Set dc = Source.Documents" with something else that would get a collection of emails inside a certain folder? I'm not trying to ignore your advice about a mail agent, it's just that I tried that way already and didn't get anywhere. This seemed like an easier way to go.

Thanks,
Mike
 
Of course. All you need to do is open the backend folder from a NotesDatabase object, then cycle through the documents therein.

The issue in your code is that you used the front end, the NotesUI objects. Basically, the frontend deals with the user interface, or what the user is using. The backend deals with the existing documents, outside of user choices. One of the most basic things a Notes developer needs to know is where to decide for backend or frontend objects. It's not always easy when you start :).

If you want to cycle through a collection of documents in a view or folder, it is quite simple to do :
Code:
Dim session as New NotesSession
Dim db as NotesDatabase
Dim view as NotesView
Dim doc as NotesDocument
Dim olddoc as NotesDocument

Set db = session.currentDatabase
Set view = db.getview("[i]viewname[/i]")
Set doc = view.getfirstdocument
Do while not(Doc Is Nothing)
   ...
   Set olddoc = doc
   Set doc = view.getNextDocument(olddoc)
   Delete olddoc
Loop

I hope this can help you.

Pascal.
 
Thanks Pascal! Here's the code I ended up with to delete old emails.

Code:
Sub Queryopen(Source As Notesuiview, Continue As Variant)
	
	Dim session As New NotesSession
	Dim db As NotesDatabase
	Dim view As NotesView
	Dim doc As NotesDocument
	Dim olddoc As NotesDocument
	Dim subj As String
	Dim subj2 As String
	
	Set db = session.currentDatabase
	
	Set view = db.getview("SPRs")
	Set doc = view.getfirstdocument
	subj = doc.Subject(0)
	
	Set olddoc = doc
	Set doc = view.getNextDocument(olddoc)
	
	Do While Not(doc Is Nothing)
		subj2 = doc.Subject(0)
		If subj = subj2 Then
			doc.removeFromFolder "SPRs"
		End If
		Set olddoc = doc
		Set doc = view.getNextDocument(olddoc)
	Loop
	
	view.Refresh
	
End Sub

I'm curious, where does the email go when I call:
doc.removeFromFolder "SPRs"? Is it actually deleted from my account? It disappears from the folder as it should.

Thanks,
Mike
 
By using the RemoveFromFolder method you are removing the document from the folder index - not deleting it from the database.
Understand that a document in any Notes database can be present in any number of views and folders. Views and folders are indexes, a bit like SQL queries - static for views, dynamic for folders.
But a document that is not present in any view or folder can still be present in the database itself. The existence of the document record and its presence in an index are entirely different things.

Now, I don't know whether this is what you want to do or not, but if your goal is to delete the document from the database, you need to use the Remove method. Doing so will automatically remove all references to that document in every index where it is present.

Pascal.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top