Академический Документы
Профессиональный Документы
Культура Документы
'------------------------------------------------------------------------' Module
: ReportGeneration by Jaime Diaz (unless otherwise stated)
' Author
: Jaime Diaz (c) 2016
' Created : 08-25-2016
' Purpose : Collects all the framework necessary for results reporting
'------------------------------------------------------------------------'Ensure the following references have been added
'
- Visual Basic For Applications
'
- OLE Automation
'
- Microsoft Excel 15.0 Object Library
'
- Microsoft Office 15.0 Object Library
'
- Microsoft Forms 2.0 Object Library
'
- Microsoft Visual Basic for Applications Extensibility 5.3
'
- Microsoft Scripting Runtime
'
- Microsoft ActiveX Data Objects 6.1 Library
'
- Microsoft ActiveX Data Objects Recordset 6.0 Library
Option Explicit
Sub CreateReport()
'------------------------------------------------------------------------' Procedure : CreateReport
' Company :
' Author
: Jaime Diaz
' Created : 08-25-2016
' Purpose : Creation of results based on Dash Board Data
'------------------------------------------------------------------------Dim x As Integer
Dim lastRow As Long, currRow As Long
Dim sApp As String, sFileName As String, sPath As String, xlPath As String
Dim wdApp As Word.Application, wdDoc As Word.Document, wdSelection As Word.S
election, wdPara As Word.Paragraph
Dim xlWB As Workbook, xlSH As Object, xlChart As Chart
'Esure the workbook is saved before starting a macro, this will prevent a lo
ss of data
'and programming time as well as the dashboard sheets if they have not been
saved
ThisWorkbook.Sheets("Start Here").Range("I50").value = "Start Time: " & Form
at(Now(), "yyyy-MM-dd hh:mm:ss")
ThisWorkbook.Sheets("Start Here").Range("I51").value = "End Time: Not comple
ted"
ThisWorkbook.Save
'If MS Word is not open, open Word
sApp = "Word.Application"
If Not aaaMyLibrary.IsAppRunning(sApp) = True Then
' Open Word, and get instance
Set wdApp = CreateObject(sApp)
Else
Set wdApp = GetObject(, sApp)
End If
'Word Application does not need to be visible for this to run.
'wdApp.Visible = True
' Data begins on line 3
currRow = 3
If aaaMyLibrary.IsIn(ThisWorkbook.Sheets, "Dash Board") Then ProcessMeterRep
orts wdApp:=wdApp, currRow:=currRow
If aaaMyLibrary.IsIn(ThisWorkbook.Sheets, "Line Dash Board") Then ProcessLin
eReports wdApp:=wdApp, currRow:=currRow
If Application.WorksheetFunction.IfError(Application.WorksheetFunction.V
Lookup(ThisWorkbook.Sheets("Dash Board").Range("A" & currRow).value, ThisWorkboo
k.Sheets("Dash Board").Range("A:B"), 2, False), 0) = 0 Then
GoTo SkipMeterLoop
End If
'Check if excel doc is open, if not open it
If Not aaaMyLibrary.IsIn(Workbooks, sFileName) Then
Set xlWB = Workbooks.Open(xlPath & "\" & sPath, False, True)
Else
Set xlWB = Workbooks(sFileName)
End If
'Search for report doc, if not create it
If Not aaaMyLibrary.IsIn(wdApp.Documents, Mid(sFileName, 1, InStr(sFileN
ame, ".") - 1) & ".docx") Then
'Open Template and popualte the header, the example document templat
e has all header and footer items needed and 2 fields, one for the Title and one
for the date
Set wdDoc = wdApp.Documents.Open(ThisWorkbook.Path & "\PGE Template.
dotx", False, True)
wdDoc.SelectContentControlsByTitle("Title")(1).Range.Text = Mid(sFil
eName, 1, InStr(sFileName, ".") - 1)
wdDoc.SelectContentControlsByTitle("Date")(1).Range.Text = Format(No
w, "MMMM dd, yyyy")
wdDoc.SaveAs2 fileName:=xlPath & "\Reports\" & Mid(sFileName, 1, InS
tr(sFileName, ".") - 1) & ".docx", FileFormat:=Word.wdFormatXMLDocument
Else
Set wdDoc = wdApp.Documents(Mid(sFileName, 1, InStr(sFileName, ".")
- 1) & ".docx")
End If
Set wdSelection = wdApp.Selection
wdSelection.TypeParagraph
'Add the title and skip two lines
wdSelection.Style = wdDoc.Styles("Title")
wdSelection.TypeText Mid(sFileName, 1, InStr(sFileName, ".") - 1) & " Re
sults"
wdSelection.TypeParagraph
wdSelection.TypeParagraph
'Begin by Stating the Overall results of the tested Set
wdSelection.Font.Name = "Arial"
wdSelection.Font.size = 10
wdSelection.TypeText "Overall analysis results are as follows:"
wdSelection.TypeParagraph
wdUseBullets wdApp, wdDoc, wdSelection, False
wdSelection.TypeText Text:="Net Result:" & vbTab & ThisWorkbook.Sheets("
Dash Board").Range("AB" & currRow).value
wdSelection.TypeParagraph
wdEndBullets wdSelection
wdSelection.TypeParagraph
'Add a TabStop at 3"
wdSelection.ParagraphFormat.TabStops.Add Position:=wdApp.InchesToPoints(
3.01), Alignment:=Word.wdAlignTabLeft, Leader:=Word.wdTabLeaderSpaces
wdSelection.TypeText "Results per Test Performed:"
wdSelection.TypeParagraph
wdUseBullets wdApp, wdDoc, wdSelection, False
addTestResultGroup wdSelection, "Instant Flow", "E", currRow
addTestResultGroup wdSelection, "Current Hour", "H", currRow
addTestResultGroup wdSelection, "Current Day", "K", currRow
addTestResultGroup wdSelection, "Current Month", "N", currRow
addTestResultGroup wdSelection, "Previous Hour", "Q", currRow
addTestResultGroup wdSelection, "Previous Day", "T", currRow
wdSelection.TypeParagraph
selectDesiredGraph xlWB, "Pack / Draft"
End Select
Set xlChart = xlWB.Charts("Analysis Chart")
xlChart.ChartArea.Copy
'Continue by adding the charts images to the document
wdSelection.PasteSpecial Link:=False, DataType:=Word.wdPasteEnhanced
Metafile, Placement:=wdInLine, DisplayAsIcon:=False 'Works
wdSelection.TypeParagraph
wdSelection.InsertBreak Type:=Word.wdPageBreak
wdSelection.MoveDown
Next x
'Create an appendix for the raw data for the Set validated (Citect / Tel
vent)
'Create a PDF file for distribution
SaveDoc2PDF wdDoc:=wdDoc
'Close & Save the Document
wdDoc.Close SaveChanges:=True
'Close Excel Workbook
xlWB.Close SaveChanges:=False
SkipLineLoop:
Next currRow
Application.StatusBar = vbNullString
End Sub
Sub SaveDoc2PDF(wdDoc As Word.Document)
'------------------------------------------------------------------------' Procedure : SaveDoc2PDF Created by Jaime Diaz
' Company :
' Author
: Jaime Diaz
' Created : 09-01-2016
' Purpose : Converts Word Document to PDF
'------------------------------------------------------------------------With wdDoc
.ExportAsFixedFormat OutputFileName:=.Path & "\" & Mid(.Name, 1, InStr(.
Name, ".") - 1) & ".pdf", _
ExportFormat:=Word.wdExportFormatPDF, OpenAfterExport:=False, Op
timizeFor:=Word.wdExportOptimizeForPrint, _
Item:=Word.wdExportDocumentContent, CreateBookmarks:=Word.wdExpo
rtCreateHeadingBookmarks
End With
End Sub
Sub selectDesiredGraph(xlWB As Workbook, sGraphName As String)
'------------------------------------------------------------------------' Procedure : selectDesiredGraph Created by Jaime Diaz
' Company :
' Author
: Jaime Diaz
' Created : 09-01-2016
' Purpose : Updates Graph on Analysis report to the desider one
'------------------------------------------------------------------------Dim x As Integer, y As Integer
For x = 1 To UBound(xlWB.Charts("Analysis Chart").DropDowns("cmbChartDD").Li
st)
If xlWB.Charts("Analysis Chart").DropDowns("cmbChartDD").List(x) = sGrap
hName Then Exit For
Next x
wdDoc.DefaultTabStop = wdApp.InchesToPoints(0.5)
wdSelection.ParagraphFormat.TabStops.Add Position:=wdApp.InchesToPoints(2.5)
, Alignment:=Word.wdAlignTabLeft, Leader:=Word.wdTabLeaderSpaces
wdSelection.TypeParagraph
wdSelection.Font.Bold = True
wdSelection.TypeText "Gas SCADA:"
wdSelection.Font.Bold = False
wdSelection.TypeText vbTab & "______________________________________________
____"
wdSelection.TypeParagraph
wdSelection.ParagraphFormat.TabStops.ClearAll
wdDoc.DefaultTabStop = wdApp.InchesToPoints(0.5)
wdSelection.ParagraphFormat.TabStops.Add Position:=wdApp.InchesToPoints(3.5)
, Alignment:=Word.wdAlignTabLeft, Leader:=Word.wdTabLeaderSpaces
wdSelection.TypeText vbTab & "Signature / Date / LAN ID"
wdSelection.InsertBreak Type:=Word.wdPageBreak
wdSelection.MoveLeft Unit:=wdCharacter, Count:=2
wdSelection.TypeBackspace
wdSelection.MoveDown
wdSelection.ParagraphFormat.TabStops.ClearAll
wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Format = wdCurrParaFormat
wdSelection.Font.size = wdCurrFontSize
wdDoc.DefaultTabStop = wdApp.InchesToPoints(0.5)
Set wdCurrParaFormat = Nothing
End Sub
Sub addTestResultGroup(wdSelection As Word.Selection, sTitle As String, sResultC
ol As String, currRow As Long, Optional ByVal isLine As Boolean = False)
'------------------------------------------------------------------------' Procedure : addTestResultGroup Created by Jaime Diaz
' Company :
' Author
: Jaime Diaz
' Created : 09-01-2016
' Purpose : Add a test result group of lines with corresponding formatting
'------------------------------------------------------------------------Dim xlSheet As Worksheet
If isLine Then Set xlSheet = ThisWorkbook.Sheets("Line Dash Board") Else Set
xlSheet = ThisWorkbook.Sheets("Dash Board")
With xlSheet
wdSelection.TypeText Text:=sTitle & ":" & vbTab & .Range(sResultCol & cu
rrRow).value
wdSelection.TypeParagraph
wdSelection.Range.ListFormat.ListIndent
wdSelection.TypeText Text:="Valid Samples:" & vbTab & .Range(Chr(Asc(sRe
sultCol) - 2) & currRow).value
wdSelection.TypeParagraph
wdSelection.TypeText Text:="Pass Ratio:" & vbTab & .Range(Chr(Asc(sResul
tCol) - 1) & currRow).value
wdSelection.TypeParagraph
wdSelection.Range.ListFormat.ListOutdent
End With
End Sub
Sub wdAddSamuelHeader(wdApp As Word.Application, wdDoc As Word.Document, wdSelec
tion As Word.Selection)
'------------------------------------------------------------------------' Procedure : wdAddSamuelHeader Created by Jaime Diaz
' Company :
' Author
: Jaime Diaz
.LinkedStyle = ""
End With
wdApp.ListGalleries(Word.wdBulletGallery).ListTemplates(1).Name = ""
wdSelection.Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
wdApp.ListGalleries(Word.wdBulletGallery).ListTemplates(1), ContinuePrev
iousList:= _
ContinuePrevList, ApplyTo:=Word.wdListApplyToWholeList, DefaultListBehav
ior:= _
Word.wdWord10ListBehavior
End Sub
Sub wdEndBullets(wdSelection As Word.Selection)
'------------------------------------------------------------------------' Procedure : wdEndBullets Created by Jaime Diaz
' Company :
' Author
: Jaime Diaz
' Created : 09-01-2016
' Purpose : Terminates the use bullets
'------------------------------------------------------------------------wdSelection.Range.ListFormat.RemoveNumbers NumberType:=wdNumberParagraph
End Sub