Advertisement
core_st

Export report to Word v3

Apr 14th, 2014
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. %REM
  2.     Agent Export to Word
  3.     Created Apr 8, 2014 by Volodymyr Stoyko/TER
  4.     Description: agent collect information from _Personen view
  5.                  and create new reports, based on template, which is
  6.                  stored in document by form Word Report Template (view reportTemplate)
  7.                  In this doc also stored field with count of reports, which is incremented
  8.                  every time, when new reports are generated
  9.                  
  10.                  Report of service consumption for SLA - use this view
  11. %END REM
  12. Option Public
  13. Option Declare
  14.  
  15. Sub Initialize
  16.     Dim ws As New NotesUIWorkspace
  17.     Dim session As New NotesSession
  18.     Dim db As NotesDatabase
  19.     Dim template As NotesDocument
  20.     Dim rField1 As NotesRichTextItem
  21.     Dim rField2 As NotesRichTextItem
  22.     Dim embObject As NotesEmbeddedObject
  23.     Dim path, fileName, templateName As String ' path to temp file, template for new files
  24.     Dim countries List As String
  25.     Dim pos, locNumber, locCount, rowCounter, entryCounter, generalCount As Integer
  26.    
  27.     'collecting data
  28.     Dim collection As NotesView
  29.     Dim CollectionDoc As NotesDocument
  30.     Dim amailServer, aFullName As NotesName
  31.     Dim view As NotesView
  32.     Dim countOfReports As Integer
  33.    
  34.     ' New document, which will be created somewhere (RecLib)?
  35.     %REM
  36.     Dim attSession As New NotesSession
  37.     Dim attDb As NotesDatabase
  38.     Dim attDoc As NotesDocument
  39.     Set attDb = attSession.CurrentDatabase
  40.     Set attDoc = New NotesDocument ( attDb )
  41.     attDoc.Form = "SaveAttachments"
  42.     Dim rtItem As NotesRichTextItem ' for storing attachments
  43.     ' end of init
  44.     %End Rem
  45.    
  46.     'Get report tamplate
  47.     Set db = session.CurrentDatabase
  48.     Set view = db.GetView("reportTemplate")
  49.     Set template = view.getfirstdocument
  50.     If template Is Nothing Then
  51.         MessageBox "No configuration document",16
  52.         Exit Sub
  53.     End If
  54.     Set rField1 = template.getfirstitem("template")
  55.     If rField1 Is Nothing Then
  56.         MessageBox "No confiuration document with template",16
  57.         Exit Sub
  58.     End If
  59.     Set embObject = rField1.GetEmbeddedObject("reporttemplate.doc")
  60.     If embObject Is Nothing Then
  61.         MessageBox "Configuration file was now found in configuration document",16
  62.         Exit Sub
  63.     End If
  64.  
  65.     ' temporary path for storing tamplates
  66.     'set static comment/uncomment
  67.     path = "C:\Users\volodymyr.stoyko\Desktop\reports\"
  68.    
  69.     'check path
  70.     If IsEmpty(path) Then
  71.         MsgBox "Set correct path in 'path' variable"
  72.         Exit Sub
  73.     End If
  74.    
  75.     ' Initialize array of locations - those will be used as key to filter collected data
  76.     countries("Marocco") = "AWSM"
  77.     countries("Mexico") = "PUE"
  78.     countries("Tunisia") = "JEN"
  79.     countries("Germany") = "WOB|ING|BLN|CER|CHA|CHD|GRO|SHA|WUX|ZWI|NIN|PAM"
  80.     countries("Poland") = "GOR"
  81.     countries("Ukraine") = "TER|CHE"
  82.     countries("Bolgaria") = "KAR|MEZ"
  83.     countries("Slovakia") = "NIT|BRA"
  84.     countries("Romania") = "CRN"
  85.    
  86.     ForAll location In countries
  87.         pos = 0 'position in string with few locations
  88.        
  89.         'word variables
  90.         Dim wordapp As Variant
  91.         Dim worddoc As Variant
  92.         Dim actDoc As Variant
  93.         Dim table As Variant
  94.         Dim selection As Variant   
  95.        
  96.         templateName = Environ("TEMP") + "reporttemplate.doc"
  97.         fileName = path + "SCLN_ServiceConsumptionEvidence_" + ListTag(location) + _
  98.                             Format(Now, "_ddmmyyyy")+".doc"
  99.         Call embObject.Extractfile(templateName)
  100.         Set wordapp = CreateObject("word.application")
  101.         ' one way - extrct document and opent it for editing.
  102.         ' second way - store the dot template and add this template for new docs
  103.         Set worddoc = wordapp.documents.open(templateName)
  104.         wordapp.visible = False
  105.        
  106.         countOfReports = template.GetItemValue("countOfReports")(0)
  107.         countOfReports = countOfReports + 1
  108.        
  109.         rowCounter = 1
  110.         generalCount = 0
  111.        
  112.         Set table = wordapp.Selection.Document.tables(2)
  113.        
  114.         ' there insert location and count of user in the en    
  115.        
  116.         If InStr(location, "|") <> 0 Then  
  117.            
  118.             While pos < Len(location)
  119.                
  120.                 ' count locations for
  121.                 'Start collect data
  122.                 'Set collection = db.Getview("_Personen")
  123.                 Set collection = db.Getview("Report of service consumption for SLA")
  124.                
  125.                 'If collection.Count  > 0 Then
  126.                 'MsgBox CStr(collection.Count) + " |  " + location
  127.                 'Беремо перший док
  128.                 Set CollectionDoc = collection.GetFirstDocument
  129.                
  130.                 table.Rows.Add ' add location string
  131.                 rowCounter = rowCounter + 1
  132.                 locNumber = rowCounter
  133.                 entryCounter = 0 ' count of docs for location
  134.                
  135.                 Do While Not(CollectionDoc Is Nothing)
  136.                     'if check if it is proper department
  137.                     Set amailServer = New NotesName(CollectionDoc.GetItemValue("MailServer")(0))
  138.                     Set aFullName = New NotesName(CollectionDoc.GetItemValue("FullName")(0))
  139.                    
  140.                     If aFullName.Organization = _
  141.                         Left(Right(location,Len(location)-pos), 3)  Then
  142.                             With table                             
  143.                                 .Rows.Add   ' add new row
  144.                                 rowCounter = rowCounter + 1
  145.                                 entryCounter = entryCounter + 1                            
  146.                                 ' NotesName abbreviated or this shit left-right-Instr
  147.                                 .Cell(rowCounter, 1).Range.InsertAfter(entryCounter)
  148.                                 .Cell(rowcounter, 2).Range.InsertAfter(aFullName.Abbreviated)
  149.                                 .Cell(rowCounter, 3).Range.InsertAfter(CollectionDoc.GetItemValue("InternetAddress")(0))
  150.                                 .Cell(rowCounter, 4).Range.InsertAfter(amailServer.Abbreviated)
  151.                                 .Cell(rowCounter, 5).Range.InsertAfter(CollectionDoc.GetItemValue("MailFile")(0))
  152.                                
  153.                             End With
  154.                         End If
  155.  
  156.                     Set CollectionDoc  = collection.GetNextDocument(CollectionDoc)
  157.                 Loop
  158.                 'End If
  159.                
  160.                 ' check if no records - delete created organization row
  161.                 ' else - add info
  162.                
  163.                 If entryCounter = 0 Then
  164.                     'MsgBox Left(Right(location,Len(location)-pos), 3)
  165.                     table.Rows(locNumber).Delete
  166.                     rowCounter = rowCounter -1
  167.                 Else
  168.                     table.Rows(locNumber).Cells.Merge
  169.                     table.Cell(locNumber,1).Range.InsertAfter("Location: " + _
  170.                     Left(Right(location,Len(location)-pos), 3) + _
  171.                     ". Quantity of users: " + CStr(entryCounter))
  172.                     table.Rows(locNumber).Range.Font.Bold = True
  173.                     generalCount = generalCount + entryCounter
  174.                 End If
  175.                 'move to next organization
  176.                 pos = pos + 4
  177.             Wend
  178.  
  179.         Else
  180.  
  181.             'Set collection = db.GetView("_Personen")
  182.             Set collection = db.Getview("Report of service consumption for SLA")           
  183.            
  184.             Set CollectionDoc  = collection.GetFirstDocument
  185.            
  186.             table.Rows.Add ' add location string
  187.             locNumber = table.Rows.Count
  188.             entryCounter = 0 ' count of docs for location
  189.             rowCounter = rowCounter + 1
  190.             'поки не закінчилися дані
  191.             Do While Not(CollectionDoc Is Nothing)
  192.                
  193.                 'if check if it is proper department
  194.                 Set amailServer = New NotesName(CollectionDoc.GetItemValue("MailServer")(0))
  195.                 Set aFullName = New NotesName(CollectionDoc.GetItemValue("FullName")(0))
  196.                
  197.                 If aFullName.Organization = location Then
  198.                     With table                     
  199.                         .Rows.Add   ' add new row
  200.                         rowCounter = rowCounter + 1
  201.                         entryCounter = entryCounter + 1                            
  202.                         ' NotesName abbreviated or this shit left-right-Instr
  203.                         .Cell(rowCounter, 1).Range.InsertAfter(entryCounter)
  204.                         .Cell(rowcounter, 2).Range.InsertAfter(aFullName.Abbreviated)
  205.                         .Cell(rowCounter, 3).Range.InsertAfter(CollectionDoc.GetItemValue("InternetAddress")(0))
  206.                         .Cell(rowCounter, 4).Range.InsertAfter(amailServer.Abbreviated)
  207.                         .Cell(rowCounter, 5).Range.InsertAfter(CollectionDoc.GetItemValue("MailFile")(0))
  208.                        
  209.                     End With
  210.                 End If
  211.  
  212.                 Set CollectionDoc=collection.GetNextDocument(CollectionDoc)
  213.             Loop           
  214.        
  215.         generalCount = entryCounter
  216.        
  217.         table.Rows(locNumber).Cells.Merge
  218.         table.Cell(locNumber,1).Range.InsertAfter("Location: " + location + _
  219.         ". Quantity of users: " + CStr(generalCount))
  220.         table.Rows(locNumber).Range.Font.Bold = True
  221.        
  222.         End If 
  223.        
  224.         ' summary
  225.         Set table = wordapp.Selection.Document.tables(1)
  226.         table.Cell(2,1).Range.InsertAfter(" " + CStr(countOfReports))
  227.         table.Cell(2, 2).Range.InsertAfter(" " + ListTag(location))
  228.         table.Cell(2, 3).Range.InsertAfter(" " + Format(Now, "mmmm yyyy"))
  229.         table.Cell(3, 1).Range.InsertAfter(" " + CStr(generalCount))
  230.        
  231.  
  232.         Call worddoc.Saveas(fileName)
  233.         worddoc.Close
  234.         Set wordDoc = Nothing
  235.         wordapp.Quit
  236.         Set wordapp = Nothing
  237.        
  238.    
  239.         'add files to attachment-doc and remove from disc
  240.         ' uncomment code with attDoc init first, then code below to upload file to new document
  241.         ' in some external DB (RecLib for example)
  242.         %Rem
  243.         Set rtitem =  New NotesRichTextItem( attDoc, "saveAttachment" )
  244.         Call rtitem.EmbedObject ( EMBED_ATTACHMENT, "", fileName)
  245.         Kill fileName ' deleted file after adding
  246.         %End Rem
  247.     End ForAll 
  248.  
  249.     'add new counter to template doc
  250.     template.countOfReports = countOfReports
  251.     Call template.Save(True, False)
  252.    
  253.     ' Save document with attachments (uncomment if you upload reports to document in notes DB)
  254.     'Call attDoc.save(True, False)
  255. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement