Private Function BuildAndSaveReport(ByRef xmlForReport As XmlDocument) As String
Try
Dim avmReportWord As New Word.Application
Dim avmReportDoc As Word.Document
Dim avmReportDRILogo As Word.Paragraph
Dim avmReportTitle As Word.Paragraph
Dim avmReportReportDate As Word.Paragraph
'Start Word and open the document template
avmReportWord.Visible = False
avmReportDoc = avmReportWord.Documents.Add
'Add a dark blue border to the page
With avmReportDoc.Range.Sections(1)
With .Borders(Word.WdBorderType.wdBorderLeft)
.LineStyle = Word.WdLineStyle.wdLineStyleSingle
.LineWidth = Word.WdLineWidth.wdLineWidth225pt
.Color = Word.WdColor.wdColorDarkBlue
End With
With .Borders(Word.WdBorderType.wdBorderRight)
.LineStyle = Word.WdLineStyle.wdLineStyleSingle
.LineWidth = Word.WdLineWidth.wdLineWidth225pt
.Color = Word.WdColor.wdColorDarkBlue
End With
With .Borders(Word.WdBorderType.wdBorderTop)
.LineStyle = Word.WdLineStyle.wdLineStyleSingle
.LineWidth = Word.WdLineWidth.wdLineWidth225pt
.Color = Word.WdColor.wdColorDarkBlue
End With
With .Borders(Word.WdBorderType.wdBorderBottom)
.LineStyle = Word.WdLineStyle.wdLineStyleSingle
.LineWidth = Word.WdLineWidth.wdLineWidth225pt
.Color = Word.WdColor.wdColorDarkBlue
End With
With .Borders
.DistanceFrom = Word.WdBorderDistanceFrom.wdBorderDistanceFromPageEdge
.AlwaysInFront = True
.SurroundHeader = True
.SurroundFooter = True
.JoinBorders = False
.DistanceFromTop = 24
.DistanceFromLeft = 24
.DistanceFromBottom = 24
.DistanceFromRight = 24
.Shadow = False
.EnableFirstPageInSection = True
.EnableOtherPagesInSection = True
.ApplyPageBordersToAllSections()
End With
End With
'Insert a paragraph that contains the logo
avmReportDRILogo = avmReportDoc.Content.Paragraphs.Add
avmReportDRILogo.Range.InlineShapes.AddPicture("\\Dri-sql-mail2\G_SHARE\inetpub\dritest\images\dri_logo_w-text.gif", False, True)
avmReportDRILogo.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
avmReportDRILogo.Range.InsertParagraphAfter()
'Insert a paragraph that contains the title
avmReportTitle = avmReportDoc.Content.Paragraphs.Add
avmReportTitle.Range.Text = "AVM Report"
avmReportTitle.Range.Font.Bold = True
avmReportTitle.Range.Font.Size = 22
avmReportTitle.Range.Font.Name = "Verdana"
avmReportTitle.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
avmReportTitle.Range.InsertParagraphAfter()
'Insert a paragraph that contains the date the report was generated
avmReportReportDate = avmReportDoc.Content.Paragraphs.Add
If TimeZone.CurrentTimeZone.IsDaylightSavingTime(Now()) Then
avmReportReportDate.Range.Text = "Report Generated on " & Right("0" & Day(Now()), 2) & "-" & UCase(Left(MonthName(Month(Now())), 3)) & "-" & Year(Now()) & " at " & Hour(Now()) & ":" & Minute(Now()) & ":" & Second(Now()) & " CDT"
Else
avmReportReportDate.Range.Text = "Report Generated on " & Right("0" & Day(Now()), 2) & "-" & UCase(Left(MonthName(Month(Now())), 3)) & "-" & Year(Now()) & " at " & Hour(Now()) & ":" & Minute(Now()) & ":" & Second(Now()) & " CST"
End If
avmReportReportDate.Range.Font.Italic = True
avmReportReportDate.Range.Font.Size = 10
avmReportReportDate.Range.Font.Name = "Verdana"
avmReportReportDate.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
avmReportReportDate.Range.InsertParagraphAfter()
'Make sure the directory where we're going to save the report exists
If Not Directory.Exists("\\Dri-sql-mail2\G_SHARE\inetpub\[URL unfurl="true"]wwwroot\Documents\Completed[/URL] Deeds & Docs\" & avmOrderID & "\web") Then
Dim avmReportDI As DirectoryInfo = Directory.CreateDirectory("\\Dri-sql-mail2\G_SHARE\inetpub\[URL unfurl="true"]wwwroot\Documents\Completed[/URL] Deeds & Docs\" & avmOrderID & "\web")
avmReportDI = Nothing
End If
'Generate the filename: AVM-CLIENTID-ORDERID.doc
Dim avmReportDocFileName As String = "AVM-" & Clean(avmClientID) & "-" & avmOrderID & ".doc"
Try
'Save the document
avmReportDoc.SaveAs("\\Dri-sql-mail2\G_SHARE\inetpub\[URL unfurl="true"]wwwroot\Documents\Completed[/URL] Deeds & Docs\" & avmOrderID & "\web\" & avmReportDocFileName)
Catch wordSaveEx As Exception
'The Word Report could not generate
emailError("While attempting to save the MS Word AVM report for OrderID #" & avmOrderID & " using MS Word: " & wordSaveEx.Message & " -in- " & wordSaveEx.Source & CRLF & CRLF & "Save-To Path: " & "\\Dri-sql-mail2\G_SHARE\inetpub\[URL unfurl="true"]wwwroot\Documents\Completed[/URL] Deeds & Docs\" & avmOrderID & "\web\" & avmReportDocFileName)
End Try
'Quit Word
avmReportDoc.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
avmReportWord.Application.Quit()
While System.Runtime.InteropServices.Marshal.ReleaseComObject(avmReportWord) <> 0
'Wait until the memory is released
End While
'Destroy objects
avmReportDRILogo = Nothing
avmReportTitle = Nothing
avmReportReportDate = Nothing
avmReportDoc = Nothing
avmReportWord = Nothing
BuildAndSaveReport = avmReportDocFileName
Catch wordEx As Exception
'The Word Report could not generate
emailError("While attempting to create the AVM report for OrderID #" & avmOrderID & " using MS Word: " & wordEx.Message & " -in- " & wordEx.Source)
End Try
End Function