Dumps all fields in a IBM Notes database to Excel
Lotus Domino script snippet.
Specify in a profile document:
– Relative database file paths
– Excel output directory
– Checkbox to include unused forms
– Checkbox to include content: how many documents, how many characters per content column
– (String of) characters not regarded as content
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 |
%Include "LSCONST.LSS" %Include "LSERR.LSS" 'Option Public Option Declare Option Compare NoCase Option Base 0 Dim AgtLog As NotesLog Dim strErrMsg As String Dim Forms List As FormStats Dim Dt As String Public sn As NotesSession Public ldbCurrent As NotesDatabase Public cntColumns As Integer Public cntChar As Integer Public blIncludeContent As Boolean Public blIncludeUnused As Boolean Public Itm As NotesItem Public regType As String Public DumpPath As String Public Nm As NotesName Public varSpaceField As Variant Public frmName As String Public Type FieldStats Count As Long Values As Integer Type As String iHasContent As Long strContent As String End Type Class FormStats Public Count As Long Public Fields List As FieldStats Public NumOut As Integer Public FileName As String Public i As Integer Public iHasContent As Long Public strContent As String %REM Sub RegisterFieldFromForm(FieldName As String, iFieldType As Long) Me.Fields(FieldName).Count = 0 Me.Fields(FieldName).Values = 0 Call GetFieldType(FieldName, iFieldType) End Sub %END REM Sub RegisterFieldFromTmpDoc(Itm As NotesItem) Dim FieldName As String FieldName = Itm.Name Me.Fields(FieldName).Count = 0 Me.Fields(FieldName).Values = 0 Call GetFieldType(FieldName, itm.Type) End Sub Sub RegisterField(Itm As NotesItem, NoContentChars) Dim FieldName As String Dim Content As Variant Dim Dummy(0) As Variant On Error GoTo 0 FieldName = Itm.Name If LeadTrailSpace(FieldName) Then Exit Sub End If If IsArray(Itm.Values) Then Content = Itm.Values Else Dummy(0) = Itm.Values Content = Dummy End If 'count # field on form Me.Fields(FieldName).Count = Me.Fields(FieldName).Count + 1 If Me.Fields(FieldName).Values <= UBound(Content) Then Me.Fields(FieldName).Values = UBound(Content) + 1 End If 'count # field with value For i = 0 To UBound(Content) If CStr(Content(i)) > "" Then If IsNull (ArrayGetIndex(NoContentChars, Content(i))) Then Me.Fields(Fieldname).iHasContent = Me.Fields(Fieldname).iHasContent + 1 If blIncludeContent Then If Me.Fields(Fieldname).iHasContent < cntColumns + 1 Then If Me.Fields(Fieldname).strContent = "" Then Me.Fields(Fieldname).strContent = Left(CStr(Content(i)), cntChar) Else Me.Fields(Fieldname).strContent = Me.Fields(Fieldname).strContent & "~~~" & Left(CStr(Content(i)), cntChar) End If End If End If Exit For End If End If Next Call GetFieldType(FieldName, itm.Type) End Sub Sub GetFieldType(FieldName As String, iType As Long) 'The GetFieldType method in the LotusScript class NotesForm does not return the correct Type values for 'Names, Authors and Readers fields. Select Case iType Case 16 Me.Fields(FieldName).Type = "ACTIONCD" Case 17 Me.Fields(FieldName).Type = "ASSISTANTINFO" Case 1084 Me.Fields(FieldName).Type = "ATTACHMENT" Case 1076 Me.Fields(FieldName).Type = "AUTHORS" Case 1024 Me.Fields(FieldName).Type = "DATETIMES" Case 1025 Me.Fields(FieldName).Type = "DATETIMERANGE" Case 1090 Me.Fields(FieldName).Type = "EMBEDDEDOBJECT" Case 256 Me.Fields(FieldName).Type = "ERRORITEM" Case 1536 Me.Fields(FieldName).Type = "FORMULA" Case 21 Me.Fields(FieldName).Type = "HTML" Case 6 Me.Fields(FieldName).Type = "ICON" Case 20 Me.Fields(FieldName).Type = "LSOBJECT" Case 25 Me.Fields(FieldName).Type = "MIME_PART" Case 1074 Me.Fields(FieldName).Type = "NAMES" Case 7 Me.Fields(FieldName).Type = "NOTELINKS" Case 4 Me.Fields(FieldName).Type = "NOTEREFS" Case 768 Me.Fields(FieldName).Type = "NUMBERS" Case 769 Me.Fields(FieldName).Type = "NUMBERRANGE" Case 1085 Me.Fields(FieldName).Type = "OTHEROBJECT" Case 15 Me.Fields(FieldName).Type = "QUERYCD" Case 1075 Me.Fields(FieldName).Type = "READERS" Case 1 Me.Fields(FieldName).Type = "RICHTEXT" Case 8 Me.Fields(FieldName).Type = "SIGNATURE" Case 1280 Me.Fields(FieldName).Type = "TEXT" Case 1281 Me.Fields(FieldName).Type = "TEXTLIST" Case 512 Me.Fields(FieldName).Type = "UNAVAILABLE" Case 0 Me.Fields(FieldName).Type = "UNKNOWN" Case 14 Me.Fields(FieldName).Type = "USERDATA" Case 1792 Me.Fields(FieldName).Type = "USERID" Case 18 Me.Fields(FieldName).Type = "VIEWMAPDATA" Case 19 Me.Fields(FieldName).Type = "VIEWMAPLAYOUT" Case Else Me.Fields(FieldName).Type = "-- NOT IDENTIFIED (" & CStr(iType) & ") --" End Select End Sub End Class Sub DumpToTxt(ByVal DumpDb As String, strTrigger As String, NoContentChars) Dim sn As New NotesSession Dim ldbCurrent As NotesDatabase Dim Db As NotesDatabase Dim Coll As NotesDocumentCollection Dim Doc As NotesDocument Dim HulpVar As FormStats Dim columnheadings As String Dim strContentPrint As String Dim strTmp As String Dim tmpFN As String Dim blHeaderCreated As Integer Dim Nr As Integer Dim x As Integer Dim CountDown As Long Dim varContent As Variant On Error GoTo ErrorHandler 'Open database to be dumped Set ldbCurrent = sn.CurrentDatabase Set Db = New NotesDatabase("", "") Call Db.Open(ldbCurrent.Server, DumpDb) If Not Db.IsOpen Then Print "Database " & DumpDb & " could not be openend. Resuming next database..." Exit Sub End If Set Coll = Db.AllDocuments Erase Forms CountDown = Coll.Count Print "Database " & DumpDb & ": " & CountDown & " documents to process..." Dt = Format$(Now, "yyyymmddhhnnss") Set Doc = Coll.GetFirstDocument Do While Not (Doc Is Nothing) Set HulpVar = GrabFormStats(Db, Doc.Form(0)) frmName = Doc.Form(0) 'Print #HulpVar.NumOut, "FORM" & Sep(Sep1) & Doc.UniversalID 'Form dump HulpVar.Count = HulpVar.Count + 1 ForAll Item In Doc.Items Call HulpVar.RegisterField(Doc.GetFirstItem(Item.Name), NoContentChars) 'only 'Item' doesnt work End ForAll NextDoc: CountDown = CountDown - 1 If (CountDown/100) = Int(CountDown/100) Then Print CountDown & " documents to process..." End If Set Doc = Coll.GetNextDocument(Doc) Loop 'Generate dump for forms that are not used in documents If blIncludeUnused Then Call GetFormsNotUsed(Db) End If 'Create output Set Nm = New NotesName(Db.Server) tmpFN = "Inventory" _ & "_" & StrLeft(Nm.Common & "-", "-") _ & "_" & StrLeft(Db.FileName, ".") _ & "_" & Dt tmpFN = DumpPath & StrConv(tmpFN, 1) 'uppercase Nr = FreeFile Open tmpFN & ".TXT" For Output As #Nr Print "Generate report for " & DumpDb blHeaderCreated = 0 ForAll FormType In Forms 'Set Itm = docNew.GetFirstItem("Velden") Set HulpVar = Forms(ListTag(FormType)) 'Create header for txt file If blHeaderCreated = 0 Then columnheadings = "Form" & " " & "Field" & " " & "Field Type" & " " & "Hits" & " " & "Has content" & " " & "Elements" If blIncludeContent Then For x = 1 To cntColumns columnheadings = columnheadings & " " & "Content" & CStr(x) Next End If Print #Nr, columnheadings blHeaderCreated = 1 End If If blIncludeContent Then ForAll FieldType In HulpVar.Fields varContent = Explode(FieldType.strContent, "~~~" ) strContentPrint = "" If IsEmpty(varContent) Then strContentPrint = "" Else For x = 0 To UBound(varContent) strTmp = varContent(x) strTmp = ReplaceSubString(strTmp, Chr(13), " ") strTmp = ReplaceSubString(strTmp, Chr(10), " ") strTmp = FullTrim(strTmp) strContentPrint = strContentPrint & strTmp & " " Next End If Print #Nr, ListTag(FormType) & " " & ListTag(FieldType) & " " & FieldType.Type & " " & FieldType.Count & " " & FieldType.iHasContent & " " & FieldType.Values & " " & strContentPrint End ForAll Else ForAll FieldType In HulpVar.Fields Print #Nr, ListTag(FormType) & " " & ListTag(FieldType) & " " & FieldType.Type & " " & FieldType.Count & " " & FieldType.iHasContent & " " & FieldType.Values End ForAll End If End ForAll Call WriteToExcel(Db.FileName, tmpFN) GoTo TheEnd ErrorHandler: strErrMsg = "Error in DumpToTxt: Error " & Str(Err) & " at line " & CStr(Erl) & ": " & Error$ If Not Doc Is Nothing Then Print strErrMsg & ". Resuming next document..." Resume NextDoc Else Print strErrMsg Call RunTimeError(agtLog, ldbCurrent, strErrMsg) Exit Sub End If Exit Sub TheEnd: End Sub Function Explode(inputString As String, delimiter As String) As Variant Dim workingString As String Dim array() As String workingString = inputString ReDim array(0) Dim pos As Integer Dim nextPos As Integer pos = InStr(workingString, delimiter) While pos <> 0 array(UBound(array)) = Left(workingString, pos - 1) workingString = Right(workingString, Len(workingString) - Len(delimiter) - pos + 1) pos = InStr(workingString, delimiter) ReDim Preserve array(UBound(array) + 1) Wend array(UBound(array)) = workingString Explode = array End Function Sub Start(strTrigger As String) Dim sn As New NotesSession Dim docProf As NotesDocument Dim PathList As Variant Dim DbCount As Integer Dim NoContentChars As Variant Dim strIncludeContent As String Dim strIncludeUnused As String Dim nnServer As NotesName Set ldbCurrent = sn.CurrentDatabase Set docProf = ldbCurrent.GetProfileDocument("fmConfiguration") If strTrigger = "Scheduled" Then If docProf Is Nothing Then Print "Database " & ldbCurrent.Title & " does not contain a profile document. Agent aborted." Exit Sub End If If Not docProf.HasItem("DbPath") Then Print "Database " & ldbCurrent.Title & " does not contain a profile document. Agent aborted." Exit Sub End If Else If Exists(docProf) = False Then Exit Sub End If End If 'Logging: Set AgtLog = New NotesLog("Agent log") AgtLog.LogActions = True AgtLog.OpenAgentLog AgtLog.LogAction("Agent Dump To Excel started") Call RunTimeError(agtLog, ldbCurrent, "# Agent Dump To Excel started (" & strTrigger & ")") Print "Agent Dump To Excel started (" & strTrigger & ")" If strTrigger = "Manual" Then Dim ws As New NotesUIWorkspace 'Call ws.ReloadWindow End If Set nnServer = New NotesName(ldbCurrent.Server) Call RunTimeError(agtLog, ldbCurrent, "# Server " & nnServer.Abbreviated & " is authorized to run this agent") ReDim varSpaceField(0) PathList = docProf.DbPath DumpPath = docProf.DumpPath(0) NoContentChars = docProf.NoContentChars strIncludeContent = docProf.IncludeContent(0) strIncludeUnused = docProf.IncludeUnusedForms(0) cntColumns = docProf.cntColumns(0) cntChar = docProf.cntChar(0) blIncludeContent = False If strIncludeContent <> "" Then If cntColumns > 0 Then If cntChar > 0 Then blIncludeContent = True End If End If End If blIncludeUnused = False If strIncludeUnused <> "" Then blIncludeUnused = True End If WinCreateFolder DumpPath For DbCount = 0 To UBound(PathList) Call DumpToTxt(FullTrim(PathList(DbCount)), strTrigger, NoContentChars) Next If varSpaceField(0) <> "" Then Call RunTimeError(agtLog, ldbCurrent, "# Fields that were not dumped because they contain spaces: " & Join(varSpaceField, ";")) End If Finish: AgtLog.LogAction("Agent Dump To Excel finished") Call RunTimeError(agtLog, ldbCurrent, "# Agent Dump To Excel finished (" & strTrigger & ")") Print "Agent Dump To Excel finished (" & strTrigger & ")" If strTrigger = "Manual" Then Call ws.ReloadWindow End If End Sub Public Sub WinCreateFolder(psFolder As String) Dim lsDrive As String Dim lsTemp As String Dim liPos As Integer Dim liLastPos As Integer If Mid$(psFolder,2,1)<>":" Then Error 9000,"No drive specified" End If lsDrive=Left$(psFolder,2) lsTemp=lsDrive+"\" liLastPos=4 liPos=InStr(4,psFolder,"\") Do Until liPos=0 lsTemp=lsTemp+Mid$(psFolder,liLastPos,(liPos-liLastPos)) If Dir$(lsTemp,ATTR_DIRECTORY)="" Then 'it exists but is not a directory MkDir lsTemp End If liLastPos=liPos+1 liPos=InStr(liLastPos,psFolder,"\") lsTemp=lsTemp+"\" Loop lsTemp=lsTemp+Mid$(psFolder,liLastPos) If Dir$(lsTemp,ATTR_DIRECTORY)="" Then MkDir lsTemp End If End Sub %REM Sub DumpToExcel Description: Main function for (Dump To Excel) Agent %END REM Public Sub DumpToExcel Dim ws As New NotesUIWorkspace Dim askme As Integer askme = ws.Prompt (PROMPT_YESNO, "Start Excel Dump", "You are about to start a manual dump to Excel." & Chr(13) _ & "Do you want to continue?" ) If askme = 1 Then Call Start("Manual") Else Print "Excel Dump aborted" End If End Sub Function GrabFormStats(Db As NotesDatabase, FormName As String) As FormStats Dim Nm As NotesName Dim FileName As String Dim Nr As Integer If Not IsElement(Forms(FormName)) Then Set Forms(FormName) = New FormStats End If If Forms(FormName).Count = 0 Then Set Nm = New NotesName(Db.Server) FileName = StrLeft(Nm.Common & "-", "-") _ & "_" & StrLeft(Db.FileName, ".") _ & "_" & Dt _ & "_" & FormName _ & ".txt" FileName = StrConv(FileName, 1) ' uppercase Nr = FreeFile Forms(FormName).NumOut = Nr Forms(FormName).FileName = FileName End If Set GrabFormStats = Forms(FormName) End Function Sub RunTimeError(agtLog As NotesLog, ldbCurrent As NotesDatabase, strErrMsg As String) Dim docError As NotesDocument On Error GoTo handler Set docError = ldbCurrent.CreateDocument docError.Form = "ErrorLog" docError.TimeDate = Now If Left(strErrMsg, 1) = "#" Then docError.ErrorLog = strErrMsg Else agtLog.LogAction(Err() & ": " & Error()) docError.ErrorLog = strErrMsg & Erl() & ", code " & Err() & ", message: " & Error() End If Call docError.Save(True, False) strErrMsg = "" Exit Sub handler: strErrMsg = "Error in RunTimeError: Error " & Str(Err) & " at line " & CStr(Erl) & ": " & Error$ Print strErrMsg Exit Sub End Sub Sub GetFormsNotUsed(Db As NotesDatabase) Dim HulpVar As FormStats Dim docTmp As NotesDocument If Not IsEmpty(db.Forms) Then ForAll form In db.Forms If Not form.IsSubForm Then If IsEmpty(form.Aliases) Then Set HulpVar = GrabFormStats(Db, form.Name) Else Set HulpVar = GrabFormStats(Db, form.Aliases(0)) End If If HulpVar.Count = 0 Then %REM Forall field In form.Fields Call HulpVar.RegisterFieldFromForm(field, form.GetFieldType(field)) End Forall %END REM 'Create a temporary document to get the field type, GetFieldType does not work correctly from the Forms class... Set docTmp = db.CreateDocument docTmp.Form = form.Name Call docTmp.ComputeWithForm(True, False) ForAll Item In docTmp.Items Call HulpVar.RegisterFieldFromTmpDoc(docTmp.GetFirstItem(Item.Name)) End ForAll Call docTmp.RemovePermanently(True) End If End If End ForAll End If End Sub Function LeadTrailSpace(FieldName As String) As Boolean If Left(FieldName, 1) = " " Or Right(FieldName, 1) = " " Then If varSpaceField(0) = "" Then varSpaceField(0) = frmName & " - " & FieldName Else If IsNull(ArrayGetIndex(varSpaceField, frmName & " - " & FieldName, 5)) Then ReDim Preserve varSpaceField(UBound(varSpaceField) + 1) varSpaceField(UBound(varSpaceField)) = frmName & " - " & FieldName End If End If LeadTrailSpace = True End If End Function Function Exists(doc As NotesDocument) As Boolean Dim ws As New NotesUIWorkspace Exists = True If doc Is Nothing Then GoTo Dialg ElseIf Not doc.HasItem("DbPath") Then Dialg: Exists = False If ws.DialogBox("fmConfiguration", False, False, False, False, False, False, "- Please configure the Notes Field Dump section before running the dump process -", doc, False, False, True) = False Then Call doc.Remove(True) Call ws.ViewRefresh Else Call doc.Save(True, False) End If End If End Function Sub WriteToExcel(strDbFileName As String, strFilePath As String) Dim entryvalues As Variant, handle As Variant, wbook As Variant, wsheet As Variant, viewcolumns As Variant Dim values As String On Error GoTo ErrorHandler 'Create Excel sheet from txt file Set handle = CreateObject("Excel.Application") handle.visible = False Set wbook = handle.Workbooks.Open(strFilePath & ".TXT", ,False) Set wsheet = handle.Application.Workbooks(1).Worksheets(1) 'Format sheet wsheet.Name = strDbFileName wsheet.Cells.Font.Size = 8 wsheet.Rows("1:1").Select wsheet.Rows("1:1").AutoFilter wsheet.Rows("1:1").Font.Bold = True wsheet.Rows("1:1").RowHeight = 18 wsheet.Cells.EntireColumn.Autofit wsheet.Range("A2").Select handle.ActiveWindow.FreezePanes = 1 'Add comments wsheet.Range("D1").AddComment "Number of times this field was found" wsheet.Range("D1").Comment.Visible = False wsheet.Range("E1").AddComment "Number of fields that have content" wsheet.Range("E1").Comment.Visible = False wsheet.Range("F1").AddComment "Maximum number of elements found in this field" wsheet.Range("F1").Comment.Visible = False 'SORTING... ' Excel Class XlSortOrder Const xlAscending = 1 Const xlDescending = 2 ' Excel Class XlSortType (Pivot only) Const xlSortValues = 1 Const xlSortLabels = 2 ' Excel Class XlYesNoGuess (Header) Const xlGuess = 0 Const xlYes = 1 Const xlNo = 2 ' Excel Class Constants Const xlTopToBottom = 1 Const xlLeftToRight = 2 ' Excel Class XlSortOrientation ' Don't use this! Probably wrong. Use xlTopToBottom ' and xlLeftToRight defined in Excel Class Constants. ' Even Excel 2002 (Office XP) uses xlTopToBottom and ' xlLeftToRight in recorded macros. Const xlSortColumns = 1 Const xlSortRows = 2 ' Excel Class XlSortMethod Const xlPinYin = 1 Const xlStroke = 2 ' Excel Class XlSortDataOption Const xlSortNormal = 0 Const xlSortTextAsNumbers = 1 Call wsheet.Cells.Select Call handle.Selection.Sort(wsheet.Range("A1"), xlAscending, Null, Null, Null, Null, Null, xlYes, 1, False, xlTopToBottom, xlPinYin, xlSortNormal, xlSortNormal, xlSortNormal) wsheet.Range("A1").Select Call wbook.SaveAs(strFilePath & ".xls", -4143, "", "", False, False) Call wbook.Close handle.quit Set handle = Nothing ' free up the memory Exit Sub ErrorHandler: strErrMsg = "Error in WriteToExcel: Error " & Str(Err) & " at line " & CStr(Erl) & ": " & Error$ Print strErrMsg Call RunTimeError(agtLog, ldbCurrent, strErrMsg) Exit Sub End Sub Function ReplaceSubString (ByVal SrcStr As String, ByVal OrigSubStr As String,ByVal RplcSubString As String) As String Dim NextPos As Long Dim OrigSubStringLen As Integer Dim RplcSubStringLen As Integer Dim NStr As String NStr = SrcStr OrigSubStringLen = Len(OrigSubStr) RplcSubStringLen = Len(RplcSubString) NextPos = InStr(NStr, OrigSubStr) Do Until NextPos = 0 NStr = Left$(NStr, NextPos-1) + RplcSubString + Mid$(NStr, NextPos + OrigSubStringLen ) NextPos = InStr(NextPos+RplcSubStringLen, NStr, OrigSubStr) Loop ReplaceSubString = NStr End Function |