Subject: RE: Any sample coding to export categorised view into excel?
Will,Click on my name and send me an email with your contact information.
Exporting to excel is really a simple process, however by your most recent reply I’m not sure you know a couple building blocks these things rest upon.
I can touch base with you and get these items cleared up in just a few minutes.
In case I don’t hear from you:
This creates and opens an excel workbook:
Dim xlApp As Variant
Dim oWorkbook As Variant
Set xlApp = CreateObject(“Excel.Application”)
xlApp.Visible = True 'without this you can’t see it’s running
Set oworkbook = xlApp.Workbooks 'creates instance
oworkbook.Add
This one opens an existing workbook, which I think is better because you can have a blank one ready to use, which already has the font colors and/or headers… dates or whatever… so you only need to populate it from your lookup(s).
Dim xlApp As Variant
Dim oWorkbook As Variant
Set xlApp = CreateObject(“Excel.Application”)
xlApp.Visible = True 'without this you can’t see it’s running
Set oworkbook = xlApp.Workbooks 'creates instance
oworkbook.Open(“AM_Template.xls”) '(your template)
Of course if you want to ‘browse’ to open it, well that’s easy enough to include, but if you’re going to use a template to make things easier, you don’t want to make the user go find a template… so if you have the template attached to a document in your database, then you only need to find that document, remove the attachment to a known spot THEN run the code above that uses it… and that is what I included in my example the other day… a bit too much at once I suspect.
Couple other tricks:
This is how you put something into a cell:
xlApp.cells(1,2).Value = “123.45” 'row 1, col 2
So if you use LotusScript to open a view:
Dim s As New NotesSession
Dim db As NotesDatabase
Set db = s.currentdatabase
Dim viewdoc As NotesDocument
Dim view As NotesView
Set view = db.GetView( “SalesView” )
Then go through each row in the view and get a salesAmount
Set viewdoc = view.GetFirstDocument
While Not ( viewdoc Is Nothing )
Salesperson= viewdoc.GetItemValue( “First_Name” )(0)
Set viewdoc = view.getnextdocument(viewdoc)
Wend
You could just as easily populate the cells in the workbook if you increment the row number (irow = irow + 1):
xlApp.cells(irow,2).Value = Salesperson
So let’s put it all together (if you want to see this work, you only need to change the view’s name to one in your database (uncategorized view) and change the fieldname to one that exists in your document.
Dim irow as Integer
Dim Salesperson as String
Dim xlApp As Variant
Dim oWorkbook As Variant
Set xlApp = CreateObject(“Excel.Application”)
xlApp.Visible = True 'without this you can’t see it’s running
Set oworkbook = xlApp.Workbooks
oworkbook.Add
Dim s As New NotesSession
Dim db As NotesDatabase
Set db = s.currentdatabase
Dim viewdoc As NotesDocument
Dim view As NotesView
Set view = db.GetView( “Sales” )
view.AutoUpdate = False
Set viewdoc = view.GetFirstDocument
irow = 2
While Not ( viewdoc Is Nothing )
Salesperson = viewdoc.GetItemValue( “First_Name” )(0)
xlApp.cells(irow,2).Value = Salesperson
irow = irow + 1
Set viewdoc = view.getnextdocument(viewdoc)
Wend
view.AutoUpdate = True
Create an Agent in your database with these properties:
Trigger: On Event
Action menu selection
Target: None
When you run the agent it will go thorough every document in your view and get the First_Name field and put in into the excel worksheet. I didn’t include any error handling, so if there is an error you’ll need to do something… oh, whatever field you decide to use, make sure it exists in every document … maybe use that as part of your view’s selection criteria: First_Name != “”
Enjoy