Advertisement
core_st

generateLicensesReportsToWordV2

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