Problem getting selected documents from a view

LNDeveloper
LNDeveloper used Ask the Experts™
on
I have written code(lotusscript) to do some function on the selected documents in a view. This works fine, unless i run the action without selecting any documents. If i do this, the code still runs on the document that is highlighted!

If no documents are selected then i do not want the code to run at all.

Is there a way to do this?

I can't seem to get it to ignore the highlighted document.

Much appreciated

Stephen
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
In LS you probably used a NotesDocumentCollection object and filled it with an UnprocessedDocuments property. That's what you're running your script on, right?

Add following:

If Collection.Count>=1 then
 <your current code>
End if

You could also use "Exit Sub" or things like that if the count property returns 0.

Author

Commented:
tried that already.....

i've put a messagebox line in to tell me how many documents are in the collection. Even when i haven't selected any it is telling me there is 1 (the highlighted document)

Author

Commented:
i've found that the script works for a view that holds any of my documents/reponses except a company document.

Those views with company documents in them count the highlighted document!

The 'Company' document has the same properties as a 'Contact' (for example) except that the contact document isnt included in the menu bar and it inherits from selected documents. Both of wish shouldn't matter to my problem!

So what else could it be thats treating them differently?
Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

Have you used the UnprocessedDocuments property right in that action button, or does the action launch an agent? If the latter is the case, your agent's settings may be wrong....

Please add the (basics of) the code (and agent details if involved).
Commented:
The view where you say it is working is probably a categorized view. When you have the selection bar highlighting a category, there is really no document selected (a category does not count as a document) and that is why the UnprocessedDocuments property returns 0 documents.

The view where you can't get it to work is probably an uncategorized view, so you always have at least one document selected because you can't ever get rid of the selection bar, that is why UnprocessedDocuments returns at least one document.

I don't think there is any way around this problem, except, as CRAK suggested, to check for collection.Count > 1. But then you lose the ability to run your code on just a single document. Unfortunately I don't think there is any way in Notes to make the selection bar disappear so you will always have this problem.

Scott
Huh???
Scott, yhat's why I suggested ">=1"... not ">1"!
Could have used ">0" just as well.

Commented:
CRAK, sorry, yes, you did put >= 1. Unfortunately, this will still cause the agent to run when a document is highlighted but not selected via a checkmark, because the collection.Count will still equal 1, and LNDeveloper wanted the agent not to run in this case.

My overall point was that you really can't get the agent to not run when it is set to run on "Selected Documents" because you can never get rid of the highlighter bar - it is always highlighting SOMETHING. The only way it will work as LNDeveloper wants is if it happens to be highlighting a category or a total or something like that, since these are not actual documents. But not every view has a category or total, so you can't guarantee this.

Scott
Your're right. Good point!
So LNDeveloper has to do an additional check:

If not (Doc Is Nothing) then
<code>
End If

Easiest would be to to use a for-next loop to cycle through documents. Categories may be anywhere in the collection - never tried it!- If a document is "nothing", you'd still be able to continue. With a GetNextDocument, you'd have a hard time!

Another option:
An action button to perform a first check and launch the agent.

@If(
  Form="";
  @Prompt([ok]; "Warning"; "Select document(s) first");
  @Command([ToolsRunMacro]; "<the agent>")
  )

Commented:
Or, let the user decide for himself/herself:

If collection.Count = 0 Then
     Continue = False
Elseif collection.Count = 1 Then
     Dim answer As Integer
     answer = 7     'Initially set to No
     answer = Messagebox("One document is selected. " & _
     "Notes cannot determine whether this is intentional or not. " & _
     "Do you wish to continue?",4 + 32,"Continue?")
     If answer = 6 Then     'Indicates a Yes
          Continue = True
     End If
Elseif collection.Count > 1 Then
     Continue = True
Else     'This should not occur
     Exit Sub
End If

If Continue = False Then
     Exit Sub
End If

Cheers

Scott
guys, I think you're missing the key point here.

When a document is highlighted in a view, even if it does not contain a checkmark, it -IS- selected.

Checkmarks are only a way of selecting MULTIPLE documents.
Don't agree.... depends on the user!
In a categorized view you can select one or more categories, like scottrma suggests, and the code would fail! Only if the user recognises the difference in categories and documents and selects documents only (as you assume), the cause would lie in the code.
Haven't seen any code yet though, LNDeveloper!....

Commented:
"When a document is highlighted in a view, even if it does not contain a checkmark, it -IS- selected."

No, I don't think I missed that point - that was the very point I was trying to make! Andrew, you may have made the point more eloquently than I did, though.

Why not use the code I gave above and check for collection.Count = 1 and if this returns True, then prompt the user if they wanna continue or not. Over the course of time, the user will learn for himself/herself that whatever they have highlighted is what the agent runs on, if no other documents have a checkmark next to them.

Cheers

Scott
agreed -- I ALSO missed the point that it was also failing when a category was selected.  

I always code like this:

if not collection.count = 0 then set doc = collection.getfirstdocument
while not doc is nothing
....
Scottrma,

I just did a litte test in a categorized view, using an action button and an agent (running on selected docs; same code as action):

Sub Click(Source As Button)
     Dim Session As New NotesSession
     Dim Db As NotesDatabase
     Dim Coll As NotesDocumentCollection
     Dim Doc As NotesDocument
     Dim CollCount As Integer
     Dim HitCount As Integer
     Dim Count
     
     Set Db = Session.CurrentDatabase
     Set Coll = Db.UnprocessedDocuments
     HitCount = 0
     CollCount = Coll.Count
     
     Messagebox CollCount & " Documents appear selected"
     
     If CollCount=0 Then
          Messagebox "No documents selected"
          Exit Sub
     End If
     
     For Count = 1 To CollCount
          Set Doc = Coll.GetNthDocument(Count)
          If Not (Doc Is Nothing) Then
               HitCount = HitCount + 1
               Print "Document"
          Else
               Print "Category"
          End If
     Next
     
     Messagebox HitCount & " Documents from selected " & CollCount & " processed."
End Sub


The problem that you describe (and I know from the past) does not seem to occur!
Can you please verify? I'm running 5.07 here....

Author

Commented:
Cheers everyone for your time on this.
Sorted the problem.
Cheers scottma for the first comment you made, thats what helped.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial