Exporting selected documents in view to Excel

Users would like to export only selected documents in a view to Excel.

I tried to create a view action button with the following code but when I try to run it I get “Object Variable not set”

Sub Click(Source As Button)

Dim s As New notessession

Dim db As notesdatabase

Dim view As notesview

Dim dc As notesdocumentcollection

Dim doc As notesdocument

Dim vcols As Variant

Dim Uvcols As Integer

Set db = s.currentdatabase

Set dc = db.unprocesseddocuments

Set view = db.getview("PC\Lease Requests\Master")

Uvcols = Ubound(view.Columns)

Dim xlApp As Variant

Dim xlsheet As Variant

Set xlApp = CreateObject("Excel.Application")

xlApp.StatusBar = "Creating WorkSheet. Please be patient..."

xlApp.Visible = True

xlApp.Workbooks.Add

xlApp.ReferenceStyle = 2

Set xlsheet = xlApp.Workbooks(1).Worksheets(1)

xlsheet.Name = "PC Lease "

Dim rows As Integer

rows = 1

Dim cols As Integer

cols = 1

Dim maxcols As Integer

For x=0 To Ubound(view.Columns)

	

	If view.Columns(x).IsHidden = False Then

		If view.Columns(x).Title <> "" Then

			xlsheet.Cells(rows,cols).Value = view.Columns(x).Title

			cols = cols + 1

		End If

	End If

Next

maxcols = cols - 1



Set doc = dc.getfirstdocument

Dim fieldname As String

Dim fitem As notesitem

rows=2

cols=1



Do While Not (doc Is Nothing)

	For x=0 To Ubound(view.Columns)

		

		If view.Columns(x).IsHidden = False Then

			If view.Columns(x).Title <> "" Then

				fieldname = view.Columns(x).Itemname

				Set fitem = doc.getFirstItem(fieldname)

				xlsheet.Cells(rows,cols).Value = fitem.Text

				cols = cols+1

			End If

		End If

	Next

	rows = rows+1

	cols =1

	Set doc = dc.getnextdocument(doc)

Loop

xlApp.Rows("1:1").Select

xlApp.Selection.Font.Bold = True

xlApp.Range(xlsheet.Cells(1,1), xlsheet.Cells(rows,maxcols)).Select

xlApp.Selection.Font.Name = "Arial"

xlApp.Selection.Font.Size = 9

xlApp.Selection.Columns.AutoFit

With xlApp.Worksheets(1)

	.PageSetup.Orientation = 2

	.PageSetup.centerheader = "Report - Confidential"

	.Pagesetup.RightFooter = "Page &P" & Chr$(13) & "Date: &D"

	.Pagesetup.CenterFooter = ""

End With

xlApp.ReferenceStyle = 1

xlApp.Range("A1").Select

End Sub

Any help would be greatly appreciated. Thanks!

Subject: Script is OK, the problem is probably in exported document’s content…

I tried your script on my test view and it functions like a charm, but following two conditions have to be met:

  1. All columns in view should have name of filed in it’s name (last tab in Column properties, Name)

2)All items - fields have to be available in the exported documents

When one of above mentioned condition is not fulfilled, the error (not existing item) is displayed…

Subject: Exporting selected documents in view to Excel

What line of code is the error occurring on?

Turn on the debugger and run it, then you should be able to figure it out yourself.

Subject: Exporting selected documents in view to Excel

I ran debugger. I had the wrong view name but that was it.I am still getting the error.

Does anyone have different code for an action or an agent that will export only selected documents to Excel?

Subject: RE: Exporting selected documents in view to Excel

Which line of code is the error occurring on? What are the values in the variables for that line of code that would cause the error? This should not be difficult to diagnose.

Subject: Billions and billions of posts for how to export to excel

OK, maybe not billions but many many.

When you post here, one of the links at the top of the page is to an FAQ. One of the FAQ’s is called ‘FAQ of FAQs’. In that is an entire section devoted to exporting to excel.

http://www-10.lotus.com/ldd/46dom.nsf/7e6c7e584a76331b85256a46006f083e/1c653d514d1db0ae852569620057ef1f?OpenDocument

OpenNTF.Org has examples and code for exporting to excel.

Yes, there are a lot of resources available to you that can provide you any number of ways to get data out of Notes and into Excel.

My absolute favorite is one written Ken Pespisa (kpespisa@attbi.com) which is available in the ‘Sandbox’ which is no longer on line but is being maintained here:

http://www.bananahome.com/users/bananahome/blog.nsf/d6plinks/PSTL-894R89

Enjoy.