Link to home
Start Free TrialLog in
Avatar of pharris01
pharris01

asked on

Export to CVS file

I am trying to export from a view to a CVS file in Excel. I can export all records in the view, but what I want to do is export only the records I have selected. I don't want to go to File/Print/ to do this. Can anyone help.

pharris01
Avatar of AndrewJayPollack
AndrewJayPollack

lotuscript would do this, but it would be easier to create a folder based on that view (creat a folder, choose the view you have as the basis for the design-- its all menu driven) then just drag those documents into that folder and export.

ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Why does this have to be so hard? When you go to File -> Export and then choose a filename, it automatically asks you if you want to export All Documents or Selected Documents!
Avatar of pharris01

ASKER

Zvenko
I tested this code and in the spreadsheet, it put the data in one Column without any header.Sorry I didn't give more details from the start.

Look this
John,Hand,Super

Col 1    Col 2    Col 3
Fname    Lname    Position  
John     Hand     Super

My upper LotusScript code does not look to view order.
It simply get a NotesDocumentCollection containing all Documents selected from the view.
For every document you can take those NotesItems which you like to have in your report.
You can rearrange the order of the items and can print a label row as first line.

But what absolutely does not make sense is that I am adapting this small script.
Better approach is that you ask what you do not understand on this script.

Don't you think so?

????????

Guys, guys, you're making this to difficult. Just try File -> Export, and enter a filename. I gaurantee it will ask you if you want to export All Documents or Selected Documents Only.

Or have I misunderstood the original question?

Regards,

Scott
Scott, you missed the CSV word.

Thank you for your help. The code you posted was the right answer to the question I asked. I do understand the code and can follow it. It will not export in the format I am looking for. I did find this code on the Lotus Website when I looked in the sandbox I can across this code. I hope someone else can also use it.

Option Public
Option Declare

      
      Dim workspace As NotesUIWorkspace
      Dim uiview As NotesUIView
      Dim view As NotesView
      Dim column As NotesViewColumn
      Dim viewentries As NotesViewEntryCollection
      Dim viewnav As NotesViewNavigator
      Dim viewentry As NotesViewEntry
      Dim session As NotesSession
      Dim db As NotesDatabase
      Dim dc As NotesDocumentCollection            
      Dim doc As NotesDocument
      Dim entryvalues As Variant, handle As Variant, wbook As Variant, wsheet As Variant, viewcolumns As Variant
      Dim currententry As String, currentprocess As String, viewname As String, filename As String
      Dim columnheadings As String, columnvalues As String, columntitle As String
      Dim counter As Integer, x As Integer, y As Integer ,slashpos As Integer, spacepos As Integer
      Dim hyphenpos As Integer, filenum As Integer, mycounter As Integer, commapos As Integer      
      On Error Goto processerror
      
      'set objects
      currentprocess = "setting objects"
      Set workspace = New NotesUIWorkspace
      Set uiview = workspace.CurrentView
      Set view = uiview.View
      Set viewnav = view.CreateViewNav()
      Set session = New NotesSession
      Set db = session.CurrentDatabase
      
      'get the current view's name and replace all backslashes with a hyphen
      currentprocess = "getting the view name and replacing backslashes with hyphens"
      viewname = view.Name
      slashpos = Instr(viewname, "\")
      If slashpos > 0 Then
            Do While slashpos > 0
                  Mid(viewname, slashpos) = "-"
                  slashpos = Instr(viewname, "\")                  
            Loop
      End If
      
      'now replace all forward slashes with a hyphen
      currentprocess = "replacing all forward slashes in the view name with hyphens"
      slashpos = Instr(viewname, "/")
      If slashpos > 0 Then
            Do While slashpos > 0
                  Mid(viewname, slashpos) = "-"
                  slashpos = Instr(viewname, "/")
            Loop
      End If      
      
      'reduce view name to a maximum of 31 characters but keep whole words only (cut at first space or hyphen encountered)
      currentprocess = "truncating the view name to 31 characters (whole words only)"
      If Len(viewname) > 31 Then
            viewname = Right(viewname, 31)
            spacepos = Instr(viewname, " ")
            hyphenpos = Instr(viewname, "-")
            If spacepos < hyphenpos Then
                  viewname = Right(viewname, Len(viewname) - spacepos)
            Else
                  viewname = Right(viewname, Len(viewname) - hyphenpos)
            End If
      End If
      
      'collect the selected documents
      currentprocess = "collecting the selected documents"
      Set dc = db.UnprocessedDocuments
      
      'check that documents have been selected at all
      currentprocess = "checking that documents were selected at all"
      If dc.count = 0 Then
            Msgbox "You must select the documents you wish to export. Press CTRL+A to select all documents", 0 + 48, "Error !"
            Exit Sub
      End If            
      
      'if documents have been selected create text file
      currentprocess = "creating a text file for output"
      filenum = Freefile()
      filename = "c:\" & viewname & ".csv"
      Open filename For Output As filenum
      
      'create header row in text file
      currentprocess = "recreating the column names as header in the text file"
      viewcolumns = view.Columns
      Set column = viewcolumns(Lbound(viewcolumns))
      columnheadings = column.Title
      For x = (Lbound(viewcolumns) + 1) To Ubound(viewcolumns)
            Set column = viewcolumns(x)
            columnheadings = columnheadings & "," & column.Title
      Next
      Print #filenum, columnheadings
      
      'access each selected document in turn
      currentprocess = "starting to process each document in turn"
      Set doc = dc.GetFirstDocument
      mycounter = 0
      counter = 1
      Do
            counter = counter + 1
            currentprocess = "accessing the view entry corresponding to the current document"
            'get the view entry corresponding to the current selected document
            Set viewentry = viewnav.GetEntry(doc)
            
            If viewentry Is Nothing Then
                  Print #filenum, "Document ID " & doc.UniversalID & _
                  " appears under multiple categories. Unable to export, please transfer the data manually."
            Else
                  Redim entryvalues(0)
                  entryvalues = viewentry.ColumnValues
                  If Isarray(entryvalues) Then
                        currentprocess = "creating each column value in its respective cell"
                        'create each column value in its respective cell
                        columnvalues = entryvalues(Lbound(entryvalues))
                        For y = (Lbound(entryvalues)+1) To Ubound(entryvalues)
                              currentprocess = "replacing any comma in the entry with a semicolon"
                              'seek and replace commas in entry
                              currententry = entryvalues(y)
                              commapos = Instr(currententry, ",")
                              If commapos > 0 Then
                                    Do While commapos > 0
                                          Mid(currententry, commapos) = ";"
                                          commapos = Instr(currententry, ",")
                                    Loop
                                    entryvalues(y) = currententry
                              End If      
                              columnvalues = columnvalues & "," & entryvalues(y)
                        Next
                        currentprocess = "writing the current view entry to the file"
                        Print #filenum, columnvalues
                  End If
            End If
            
            'reporting how many documents of how many in total have been exported so far
            currentprocess = "reporting progress in status bar"            
            mycounter = mycounter + 1
            Print "Exporting " & Cstr(mycounter) & "/" & dc.Count & " documents."
            
            currentprocess = "accessing the next selected document in the list"
            'get the next selected document            
            Set doc = dc.GetNextDocument(doc)
      Loop Until (doc Is Nothing)
      
      currentprocess = "closing the file"
      Close filenum
      
      'create Excel sheet
      currentprocess = "creating an Excel spreadsheet"
      Set handle = CreateObject("Excel.Application")
      handle.visible = True
      handle.Workbooks.Open(filename)
      Set wsheet = handle.Application.Workbooks(1).Worksheets(1)
      
      'format spreadsheet
      currentprocess = "formatting the spreadsheet"
      wsheet.Name = viewname
      wsheet.Cells.Font.Size = 8
      wsheet.Rows("1:1").Select
      wsheet.Rows("1:1").Font.Bold = True
      wsheet.Cells.EntireColumn.Autofit
      
      'return to cell A1 for tidyness
      wsheet.Range("A1").Select
      
      currentprocess = "terminating the export job"
      Exit Sub
      
processerror:
      If Err = 208 Then
            Msgbox "It appears you do not have Microsoft Excel on your computer. " & _
            "Although they won't be displayed on screen the exported data are still available " & _
            "in " & filename, 0 + 64, "Warning !"
      Else
            Msgbox "Error " & Err & " occurred whilst " & currentprocess      & ", execution aborted.", 0 + 48, "Error !"            
      End If
      
      Exit Sub