Advertisement
core_st

generateLicensesReportsToWord

Apr 11th, 2014
370
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 mailServer, FullName, intAddress, mailFile As String
  18.     Dim amailServer, aFullName As NotesName
  19.     Dim view As NotesView
  20.     Dim countOfReports As Integer
  21.    
  22.     ' New document, which will be created somewhere
  23.     Dim attSession As New NotesSession
  24.     Dim attDb As NotesDatabase
  25.     Dim attDoc As NotesDocument
  26.     Set attDb = attSession.CurrentDatabase
  27.     Set attDoc = New NotesDocument ( attDb )
  28.     attDoc.Form = "SaveAttachments"
  29.     Dim rtItem As NotesRichTextItem ' for storing attachments
  30.     ' end of init
  31.    
  32.    
  33.     Set db = session.CurrentDatabase
  34.     Set view = db.GetView("reportTemplate")
  35.     Set template = view.getfirstdocument
  36.     If template Is Nothing Then
  37.         MessageBox "No configuration document",16
  38.         Exit Sub
  39.     End If
  40.     Set rField1 = template.getfirstitem("template")
  41.     If rField1 Is Nothing Then
  42.         MessageBox "No confiuration document with template",16
  43.         Exit Sub
  44.     End If
  45.     Set embObject = rField1.GetEmbeddedObject("reporttemplate.doc")
  46.     If embObject Is Nothing Then
  47.         MessageBox "Configuration file was now found in configuration document",16
  48.         Exit Sub
  49.     End If
  50.  
  51.    
  52.     path = "C:\Users\volodymyr.stoyko\Desktop\reports\"
  53.    
  54.    
  55.     ' Initialize array of locations - those will be used as key to filter collected data
  56.     countries("Marocco") = "AWSM"
  57.     countries("Mexico") = "PUE"
  58.     countries("Tunisia") = "JEN"
  59.     countries("Germany") = "WOB|ING|BLN|CER|CHA|CHD|GRO|SHA|WUX|ZWI"
  60.     countries("Poland") = "GOR"
  61.     countries("Ukraine") = "TER|CHE"
  62.     countries("Bolgaria") = "KAR|MEZ"
  63.     countries("Slovakia") = "NIT|BRA"
  64.     countries("Romania") = "CRN"
  65.    
  66.     ForAll location In countries
  67.         pos = 0 'position in string with few locations
  68.        
  69.         'word variables
  70.         Dim wordapp As Variant
  71.         Dim worddoc As Variant
  72.         Dim actDoc As Variant
  73.         Dim table As Variant
  74.         Dim selection As Variant   
  75.        
  76.         templateName = Environ("TEMP") + "reporttemplate.doc"
  77.         fileName = path + ListTag(location) + ".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.         'Start collect data
  96.         Set collection = db.GetView("_Personen")
  97.        
  98.         If InStr(location, "|") <> 0 Then  
  99.            
  100.             While pos < Len(location)
  101.                
  102.                 ' count locations for
  103.                 'Start collect data
  104.                 Set collection = db.Getview("_Personen")
  105.                
  106.                 'If collection.Count  > 0 Then
  107.                 'MsgBox CStr(collection.Count) + " |  " + location
  108.                 'Беремо перший док
  109.                 Set CollectionDoc = collection.GetFirstDocument
  110.                
  111.                 table.Rows.Add ' add location string
  112.                 rowCounter = rowCounter + 1
  113.                 locNumber = rowCounter
  114.                 entryCounter = 0 ' count of docs for location
  115.                
  116.                 'поки не закінчилися дані
  117.                 Do While Not(CollectionDoc Is Nothing)
  118.                     'MsgBox "blabla"
  119.                     'if check if it is proper department
  120.                     mailServer = CollectionDoc.GetItemValue("MailServer")(0)
  121.                     FullName = CollectionDoc.GetItemValue("FullName")(0)
  122.                     intAddress = CollectionDoc.GetItemValue("InternetAddress")(0)
  123.                     mailFile = CollectionDoc.GetItemValue("MailFile")(0)
  124.                     Set amailServer = New NotesName(mailserver)
  125.                     Set aFullName = New NotesName(FullName)
  126.                    
  127.                     If mailServer <> "" And FullName <> "" And  intAddress <> "" Then
  128.                         If Right(FullName,Len(FullName) - InStr(FullName, "O=") - 1) = _
  129.                         Left(Right(location,Len(location)-pos), 3)  Then
  130.                             With table
  131.                                
  132.                                 .Rows.Add   ' add new row
  133.                                 rowCounter = rowCounter + 1
  134.                                 entryCounter = entryCounter + 1                            
  135.                                 ' NotesName abbreviated or this shit left-right-Instr
  136.                                 .Cell(rowCounter, 1).Range.InsertAfter(entryCounter)
  137.                                 .Cell(rowcounter, 2).Range.InsertAfter(aFullName.Abbreviated)
  138.                                 .Cell(rowCounter, 3).Range.InsertAfter(intAddress)
  139.                                 .Cell(rowCounter, 4).Range.InsertAfter(amailServer.Abbreviated)
  140.                                 .Cell(rowCounter, 5).Range.InsertAfter(mailFile)
  141.                                
  142.                             End With
  143.                         End If
  144.                        
  145.                     End If
  146.                     'наступний документ
  147.                     Set CollectionDoc  = collection.GetNextDocument(CollectionDoc)
  148.                 Loop
  149.                 'End If
  150.                
  151.                 table.Rows(locNumber).Cells.Merge
  152.                 table.Cell(locNumber,1).Range.InsertAfter("Location: " + _
  153.                 Left(Right(location,Len(location)-pos), 3) + _
  154.                 ". Quantity of users: " + CStr(entryCounter))
  155.                 table.Rows(locNumber).Range.Font.Bold = True
  156.                
  157.                 generalCount = generalCount + entryCounter
  158.                 pos = pos + 4
  159.             Wend
  160.             'general count of users count of rows - locations rows - first row
  161.             'generalCount = rowCounter - locCount - 1
  162.         Else
  163.  
  164.             Set collection = db.GetView("_Personen")           
  165.            
  166.             '       If collection.Count>0 Then
  167.             ' CStr(collection.Count) + " | " + location
  168.             'Беремо перший док
  169.             Set CollectionDoc  = collection.GetFirstDocument
  170.            
  171.             table.Rows.Add ' add location string
  172.             locNumber = table.Rows.Count
  173.             entryCounter = 0 ' count of docs for location
  174.             rowCounter = rowCounter + 1
  175.             'поки не закінчилися дані
  176.             Do While Not(CollectionDoc Is Nothing)
  177.                
  178.                 'if check if it is proper department
  179.                 mailServer = CollectionDoc.GetItemValue("MailServer")(0)
  180.                 FullName = CollectionDoc.GetItemValue("FullName")(0)
  181.                 intAddress = CollectionDoc.GetItemValue("InternetAddress")(0)
  182.                 mailFile = CollectionDoc.GetItemValue("MailFile")(0)
  183.                 Set amailServer = New NotesName(mailserver)
  184.                 Set aFullName = New NotesName(FullName)
  185.                
  186.                 If mailServer <> "" And FullName <> "" And  intAddress <> "" Then
  187.                     If Right(FullName,Len(FullName) - InStr(FullName, "O=") - 1) = _
  188.                     location  Then
  189.                         With table
  190.                             .Rows.Add   ' add new row
  191.                             rowCounter = rowCounter + 1
  192.                             entryCounter = entryCounter + 1
  193.                             ' NotesName abbreviated or this shit left-right-Instr
  194.                             .Cell(rowCounter, 1).Range.InsertAfter(entryCounter)
  195.                             .Cell(rowCounter, 2).Range.InsertAfter(aFullName.Abbreviated)
  196.                             .Cell(rowCounter, 3).Range.InsertAfter(intAddress)
  197.                             .Cell(rowCounter, 4).Range.InsertAfter(amailServer.Abbreviated)
  198.                             .Cell(rowCounter, 5).Range.InsertAfter(mailFile)
  199.                             '                   MsgBox "Fuck"
  200.                            
  201.                         End With
  202.                     End If
  203.                 End If
  204.                 'наступний документ
  205.                 'MsgBox location
  206.                 Set CollectionDoc=collection.GetNextDocument(CollectionDoc)
  207.             Loop
  208.            
  209.         End If     
  210.        
  211.         generalCount = entryCounter
  212.        
  213.         table.Rows(locNumber).Cells.Merge
  214.         table.Cell(locNumber,1).Range.InsertAfter("Location: " + location + _
  215.         ". Quantity of users: " + CStr(generalCount))
  216.         table.Rows(locNumber).Range.Font.Bold = True
  217.         'End If
  218.        
  219.         ' summary
  220.         Set table = wordapp.Selection.Document.tables(1)
  221.         table.Cell(2,1).Range.InsertAfter(" " + CStr(countOfReports))
  222.         table.Cell(2, 2).Range.InsertAfter(" " + ListTag(location))
  223.         table.Cell(2, 3).Range.InsertAfter(" " + Format(Now, "mmmm yyyy"))
  224.         'table.Cell(2, 3).Range.InsertAfter(" " + CStr(Day) + " " + CStr(Year))
  225.         table.Cell(3, 1).Range.InsertAfter(" " + CStr(generalCount))
  226.        
  227.         'MsgBox CStr(rowCounter)       
  228.         Call worddoc.Saveas(fileName)
  229.         worddoc.Close
  230.         Set wordDoc = Nothing
  231.         wordapp.Quit
  232.         Set wordapp = Nothing
  233.        
  234.         'add new counter to template doc
  235.         template.countOfReports = countOfReports
  236.         Call template.Save(True, False)
  237.        
  238.     End ForAll 
  239.  
  240.  
  241. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement