keerthana
keerthana

Reputation: 61

How to close file explorer window using lotus script

I'm writing a code to export the CSV using free file in lotus notes the code works fine but I'm facing an issue while closing the file explorer window. The scenario is if I don't need to export now and unknownly click export button it asks for fine name opening the file explorer window. The file explorer window it is not closing it asks for filename and its looping till i give the file name.

My code:

Sub Initialize 

    Dim ws As New NotesUIWorkspace
    Dim session As New NotesSession
    Dim source As NotesUIDocument
    Dim db As NotesDatabase
    Dim dc As NotesDocumentCollection
    Dim doc As NotesDocument
    Dim headerString As String
    Dim header As Variant
    Dim sno As Variant
    Dim vw As NotesView
    Dim flag As Boolean
    REM Get selected document
    Set db = session.CurrentDatabase
    Set dc = db.UnprocessedDocuments
    Set dc=db.Alldocuments
    Set doc = dc.GetFirstDocument
    sno=0
    Dim count As Variant
    count=0
    While Not doc Is Nothing
        filenames = ws.SaveFileDialog( _
        False,"File name",, "E:\samp", ".csv")
        If Not(IsEmpty(filenames)) Then
            REM Write Body item to file
            fileNum% = FreeFile()
            Open filenames(0) For Output As fileNum%
            headerString ="S.No,UNID,NAME,STATUS,TIME"  
            header = Split(UCase(headerString),",") 
            Print #fileNum%, headerString
            Do Until doc Is Nothing
                If (CStr(doc.Getitemvalue("Status")(0)="Accepted")) Then
                        sno=sno+1
                        d=sno+","+doc.Universalid+","+doc.Getitemvalue("Uname")(0)+","+doc.getitemvalue("Status")(0)+","+doc.Getitemvalue("Time")(0)
                        Print #fileNum%,d   
                        flag=0
                Else
                    flag=1
                End If  
                Set doc = dc.Getnextdocument(doc)
                count=count+1
            Loop    
            Else
                
        End If  
    
Wend
        If (flag="1") Then 
            MsgBox"no documents were accepted"
        Else
    MsgBox "Document exported successfully" 
    End If  
    Close fileNum%
End Sub 

Upvotes: 1

Views: 165

Answers (2)

Tode
Tode

Reputation: 12060

Your code is unnecessarily complex and has an additional loop that is not necessary at all: You have an outer "while"- loop that does... nothing and an inner "Do until"- loop that really does the looping.

Just replace the outer loop by an If, then you will only be prompted ONCE...

Instead of

While Not doc Is Nothing
    filenames = ws.SaveFileDialog( _
    False,"File name",, "E:\samp", ".csv")
    If Not(IsEmpty(filenames)) Then
        ...
        Do Until doc Is Nothing
          ...
        Loop
    End If
 Wend

Just write

If Not doc Is Nothing
    filenames = ws.SaveFileDialog( _
    False,"File name",, "E:\samp", ".csv")
    If Not(IsEmpty(filenames)) Then
        ...
        Do Until doc Is Nothing
          ...
        Loop
    End If
 End If

Upvotes: 1

Rob Mason
Rob Mason

Reputation: 1417

If Not(IsEmpty(filenames)) Then
    '<code removed for brevity>
Else
    set doc = nothing
End If
               

Put an else statement in as above, this way when you send no file name, the doc is set to nothing and the while loop exits.

Upvotes: 1

Related Questions