Thursday, April 2, 2009

THAT Makes Sense – Lotusscript to Excel (a vent and some tips)

I was asked to take some data from a Notes application and make it into a pretty report in Excel. Okay, no problem. A little lotusscript here, a little lotusscript there, a dash of VB. And VOILA, lovely report in Excel with shaded cells, borders, date formatting. Except for one little problem. I couldn’t get the Page Setup.FitToPagesWide to work.

I checked and re-checked my code. Went home and came back this morning and checked it again. It was correct and yet it didn’t work. Huh? A quick Google search gave me this

“In Microsoft Excel, you cannot use the FitToPagesWide or FitToPagesTall property in Microsoft Visual Basic for Applications code to change the Adjust To option in page setup.

To change the scaling from Adjust To to Fit To in page setup, set the Zoom property to false as in the following example:

With ActiveSheet.PageSetup
.Zoom = False
.FitToPagesTall = 1
.FitToPagesWide = 1
End With


SERIOUSLY?! THAT makes sense. Of course! Why didn’t I think of that? [end sarcasm] I have to explicitly turn off the zoom in order to set the Adjust To option, even though every other parameter that Excel spits out for PageSetup can be ignored.

Sigh. Deep breath.

Anyway, I know other blogs have covered this, but I thought I would share a bit of the code for creating a “pretty” Excel sheet from Lotus.

Use Lotusscript in the normal manner to get some data. When you’re ready…

‘We need to launch Excel from Lotus
Dim excelApp As Variant
Set excelApp = CreateObject ("Excel.Application")
‘We need to create a new Workbook
Dim excelWB As Variant
Set excelWB = excelApp.Workbooks.Add()
‘And of course, get a handle on a Worksheet
Dim excelS As Variant
Set excelS = excelWB.ActiveSheet


Now given the handle to the Excel Worksheet we can use a little VB to simply populate fields…

‘Either with direct text
excelS.Range("A1").Value = "Hello World"
‘Or with Lotusscript
excelS.Range("A1").Value = udoc.FieldGetText("greetingField")



We can have some fun with the VB, by inserting Lotusscript. I used the following to increment the row in the sheet each time I looped through a NotesViewEntryCollection.

excelS.Range("A"+Cstr(y)).Value = viewentry.ColumnValues(2)

Now let’s get our Format groove on. One thing I found is that code like this won’t work…

.Orientation = xlLandscape

You need the code for “xlLandscape”. Lucky for me, I found Joe Litton’s old post on a quick and easy way to get the code. I was already recording macros in Excel and poaching the VB to use in my Lotusscript, but he showed this gem…

MsgBox "The value of xlPaperLetter is <" & xlPaperLetter & ">"

Ahhh. A thing of beauty. By putting that in an Excel macro, I could get the code for anything I needed for my formatting. So this:

With excelS.Rows(“2:2”)
.Font.Bold = True
.WrapText = True
.HorizontalAlignment = xlRight
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeLeft).Weight = xlThin
.Borders(xlEdgeLeft).ColorIndex = 17
End With


Is now this…

With excelS.Rows("2:2")
.Font.Bold = True
.WrapText = True
.HorizontalAlignment = -4152
.Borders(7).LineStyle = 1
.Borders(7).Weight = 2
.Borders(7).ColorIndex = 17
End with


And it works! So pretty.

One last tip, it’s a good idea to make Excel visible at the beginning of your code while developing, so you can see what’s happening and what’s not happening and when. But when you are done, you should probably move “excelApp.Visible = True” down to the end of your code so users aren’t watching the screen make every little format change. While magical to the user, it is slower.

10 comments:

  1. Check out some samples of XML/XSL transforms to do your exports. I used VB way back in the day, but XSL is much more flexible.

    ReplyDelete
  2. That's on my list of things to do! :) This was just supposed to be a quick and dirty for me, and didn't require me to learn anything new! But you're right, I gotta get on the XML train.

    ReplyDelete
  3. Nice post Kathy. Your frustration with the excel constants made me remember there is a "Microsoft Office Constants" database on OpenNTF that would probably have alleviated most of that frustration.

    http://www.openntf.org/Projects/pmt.nsf/0/1F6C5C7B16317E218625726D004A31A9

    What it doesn't have, unfortunately, is a ready-to-use script library with all of them listed. It's just a reference db essentially. However, I suspect a little more looking (http://www.google.com/search?q=microsoft+office+constants+library&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a) would uncover something like that.

    You could also create a custom view in that reference db with one column with a formula like:

    "Const " + ConConstant + " = " + ConValue

    You'll need to fiddle with the application and maybe the version in the view selection (i.e. Excel 2000 as opposed to all versions). To avoid duplicate constant names you could categorize the column with the above formula.

    Then just select all the *categories* in the view (not the documents themselves), do an "Edit - Copy Selected as Table", and paste into excel, remove the blank first column, and paste what's left into Notepad. Then Copy that into a script library.

    The reason I know you *could* do this is that I already did part of it and will post it on OpenNTF shortly :-). Link to follow in later comment...

    ReplyDelete
  4. Ah yes. Joe Litton's post had a link that didn't work, and I was too lazy to go look. :) I look forward to seeing what you post!

    ReplyDelete
  5. Hat's off to you for your resourcefulness. I've run into things with MS and LN product objects that just seem to have arbitrary restrictions like that - really annoying, as you clearly know. It's a side effect of closed source... oftentimes we know exactly what we need to do, it's just that the person responsible for designing the public interfaces to the closed objects never thought of that or, as in this case, had a conflict with other available controls that had to be resolved in some (usually unintuitive) way.

    Count it as another notch in your lipstick case or whatever contemporary equivalent to a Pat Benatar reference there could be - you're earning your chops!

    ReplyDelete
  6. Oh there is NO equivalent to Pat Benatar, contemporary or otherwise! :)

    ReplyDelete
  7. Microsoft Office Constants Database (Enhanced Version) now in the OpenNTF code bin: http://www.openntf.org/projects/codebin/codebin.nsf/CodeByDate/D9D3B6E9590AE80D8625758C006215BF?OpenDocument

    ReplyDelete
  8. Hey there Kathy.

    I realize this is too little too late for you (considering you've already got your export working); but I posted an "Export any Notes View to MS Excel" template on OpenNTF a while back. http://www.openntf.org/Projects/pmt.nsf/ProjectLookup/Export%20any%20View%20to%20MS%20Excel

    It pulls everything from the view into Excel for you -except alternating row colors. I wasn't able to grab that via LotusScript. However, there is an Export Options dialog wherein the user can select any alternating row color they choose.

    Hope this helps!

    ReplyDelete
  9. Thanks Devin! I actually saw the template on OpenNTF this morning. It wouldn't have worked for me anyway, since Notes is where the user inputs the data into a form and we use the Excel output for "pretty" client-ready reporting. It has Title Bars and the client name and all kinds of "pretty" stuff I haven't done in Notes. The output isn't even in a view in Notes!

    Also, even if it was, I was doing some extra with lookups and filtering on the data. :) We don't have 8.5, so I couldn't wait for the DBLookups in Xpages! :)

    ReplyDelete
  10. Hi Cathy,
    Thanks for taking the time to log this solution.
    It saved me a lot of time and grey hairs.

    Daniel,
    form Montréal, Canada
    contexte@rocketmail com

    ReplyDelete