Here's the source for the aforementioned problem
===================================
Public Sub ExportDEPBugSheetToMSWord(sYear As String, Optional lSampleID As
Long)
'Declare variables
Dim sSQL As String
Dim rs As Recordset
Dim blRSOpen As Boolean
Dim lCurrentRecord As Long
Dim lRecordCount As Long
Dim rs2 As Recordset
Dim blRS2Open As Boolean
Dim lRecordCount2 As Long
Dim dblModBeck As Double
Dim dblEPTTaxa As Double
Dim dblTaxaRich As Double
Dim dblShannon As Double
Dim dblHilsenhoff As Double
Dim dblPctIntolerant As Double
Dim dblBeck4 As Double
Dim dblCaddisfly As Double
Dim dblMayfly As Double
Dim dblIBIScore As Double
Dim lRow As Long
Dim lCol As Long
Dim sStatusBar As String
Dim oApp As Word.Application
Dim oWord As Word.Document
Dim oSelection As Word.Selection
Dim oSec As Word.Section
Dim oRange As Word.Range
Dim otblBlueBox As Word.Table
Dim otblIdentifier As Word.Table
Dim otblMetrics As Word.Table
Dim sTest As String
Dim otblTaxa As Word.Table
Dim blWrapTaxaTable As Boolean
Dim sCurrentTaxaOrderGroup As String
Dim lTaxaRow As Long
Dim lLoop As Long
Dim otblStandEq As Word.Table
Dim sHeader As String
Dim sDocName As String
Dim sDocQualifier As String
On Error GoTo ExportDEPBugSheetToMSWord_EH
'Initialize IBI Score values
dblModBeck = 0
dblEPTTaxa = 0
dblTaxaRich = 0
dblShannon = 0
dblHilsenhoff = 0
dblPctIntolerant = 0
dblBeck4 = 0
dblCaddisfly = 0
dblMayfly = 0
dblIBIScore = 0
'Set mouse to hourglass
Screen.MousePointer = gcHourGlass
'Set prep message in status bar
SysCmd acSysCmdInitMeter, "Gathering " & sYear & _
" Benthic Macroinvertebrate Sample Data for Word Document", 3
'Get record
SysCmd acSysCmdUpdateMeter, 1
sSQL = "SELECT SiteLocation, StationID, "
sSQL = sSQL & "sample_id, method_id, MethodDesc, SiteID "
sSQL = sSQL & "FROM qryDEPBugSheet "
If lSampleID > 0 Then
sSQL = sSQL & "WHERE sample_id=" & lSampleID & " "
Else
sSQL = sSQL & "WHERE SampleYear=" & sYear & " "
End If 'lSampleID > 0
sSQL = sSQL & "ORDER BY SampleDtTm "
Set rs = modData.GetRecordset(sSQL)
blRSOpen = True
If Not rs.EOF Then
rs.MoveLast
rs.MoveFirst
lCurrentRecord = 0
lRecordCount = rs.RecordCount
'Get the sample-specific qualifier string
If lSampleID > 0 Then
sDocQualifier = rs.Fields("SiteID") & "-"
Else
sDocQualifier = ""
End If 'lSampleID > 0
'Create new Executive Summary Document
Set oApp = _
CreateObject("Word.Application")
Set oWord = oApp.Documents.Add
'Page setup
oWord.PageSetup.TopMargin = InchesToPoints(1)
oWord.PageSetup.BottomMargin = InchesToPoints(1)
oWord.PageSetup.LeftMargin = InchesToPoints(1)
oWord.PageSetup.RightMargin = InchesToPoints(1)
oWord.PageSetup.Gutter = InchesToPoints(0)
oWord.PageSetup.HeaderDistance = InchesToPoints(0.5)
oWord.PageSetup.FooterDistance = InchesToPoints(0.5)
oWord.PageSetup.PageWidth = InchesToPoints(8.5)
oWord.PageSetup.PageHeight = InchesToPoints(11)
Set oSelection = oApp.Selection
'Write data to MS Word
While Not rs.EOF
'Set default paragraph formatting
oSelection.ParagraphFormat.Space1
oSelection.ParagraphFormat.LineSpacingRule = wdLineSpaceSingle
oSelection.ParagraphFormat.SpaceAfter = 0
oSelection.ParagraphFormat.SpaceBefore = 0
'Blue Header Box
oSelection.Font.Size = 12
oSelection.Font.Bold = True
oSelection.Font.Color = wdColorWhite
Set otblBlueBox = oWord.Tables.Add(oSelection.Range, 5, 1)
'Style
otblBlueBox.PreferredWidthType = wdPreferredWidthPercent
otblBlueBox.Columns(1).PreferredWidth = 100
otblBlueBox.Shading.BackgroundPatternColor = -553582593
'Office 2007 Dark Azure-Blue
otblBlueBox.Borders(wdBorderHorizontal).LineStyle =
wdLineStyleNone
otblBlueBox.Borders(wdBorderDiagonalDown).LineStyle =
wdLineStyleNone
otblBlueBox.Borders(wdBorderDiagonalUp).LineStyle =
wdLineStyleNone
otblBlueBox.Borders(wdBorderLeft).LineStyle =
wdLineStyleEmboss3D
otblBlueBox.Borders(wdBorderLeft).LineWidth = wdLineWidth300pt
otblBlueBox.Borders(wdBorderRight).LineStyle =
wdLineStyleEngrave3D
otblBlueBox.Borders(wdBorderRight).LineWidth =
wdLineWidth300pt
otblBlueBox.Borders(wdBorderTop).LineStyle =
wdLineStyleEmboss3D
otblBlueBox.Borders(wdBorderTop).LineWidth = wdLineWidth300pt
otblBlueBox.Borders(wdBorderBottom).LineStyle =
wdLineStyleEngrave3D
otblBlueBox.Borders(wdBorderBottom).LineWidth =
wdLineWidth300pt
'Title
oSelection.Font.Size = 18
otblBlueBox.Cell(1, 1).Range.InsertAfter "Macroinvertebrate
Sample Summary"
oSelection.Font.Size = 12
'Identification Section
Set otblIdentifier = oWord.Tables.Add(otblBlueBox.Cell(3,
1).Range, 3, 2)
If otblIdentifier.Rows.Count = 1 Then
otblIdentifier.Rows.Add
otblIdentifier.Rows.Add
End If 'otblIdentifier.Rows.Count = 1
otblIdentifier.PreferredWidthType = wdPreferredWidthPoints
otblIdentifier.Columns(1).PreferredWidth = InchesToPoints(1)
otblIdentifier.Columns(2).PreferredWidth = InchesToPoints(6.5)
otblIdentifier.Cell(1, 1).Range.InsertAfter "Station ID:"
otblIdentifier.Cell(1, 2).Range.InsertAfter
rs.Fields("StationID")
otblIdentifier.Cell(2, 1).Range.InsertAfter "Method:"
otblIdentifier.Cell(2, 2).Range.InsertAfter
rs.Fields("MethodDesc")
otblIdentifier.Cell(3, 1).Range.InsertAfter "Location:"
otblIdentifier.Cell(3, 2).Range.InsertAfter
rs.Fields("SiteLocation")
Set otblIdentifier = Nothing
'Metrics Table
Set otblMetrics = oWord.Tables.Add(otblBlueBox.Cell(5,
1).Range, 2, 3)
If otblMetrics.Rows.Count = 1 Then
otblMetrics.Rows.Add
End If 'otblMetrics.Rows.Count = 1
'''otblMetrics.PreferredWidthType = wdPreferredWidthPoints
sSQL = "SELECT tblSampleTests.test_id,
tblSampleTests.sample_id, "
sSQL = sSQL & "tblSampleTests.test_type_id,
LU_TestTypes.Test_Type AS Test, "
sSQL = sSQL & "IIf(Nz(LU_TestTypes.Test_Units,'')='List',"
sSQL = sSQL &
"GetListScore(LU_TestTypes.Test_Type,Nz(tblSampleTests.Score)),"
sSQL = sSQL & "IIf(Nz(LU_TestTypes.Test_Units,'')='Ratio',"
sSQL = sSQL & "IIf(Nz(tblSampleTests.test_type_id,0)=8,"
sSQL = sSQL & "Shredder2Insect(tblSampleTests.sample_id),"
sSQL = sSQL & "Decimal2Ratio(Nz(tblSampleTests.Score,0))),"
sSQL = sSQL &
"IIf(Nz(LU_TestTypes.Test_Units,'')='Percentage',"
sSQL = sSQL & "(Round((tblSampleTests.Score*100),2) & '%'),"
sSQL = sSQL & "Round(tblSampleTests.Score,2)))) AS Score, "
sSQL = sSQL & "LU_TestTypes.Test_Units AS Unit,
tblSampleTests.Notes "
sSQL = sSQL & "FROM tblSampleTests LEFT JOIN LU_TestTypes "
sSQL = sSQL & "ON
tblSampleTests.test_type_id=LU_TestTypes.test_type_id "
sSQL = sSQL & "WHERE LU_TestTypes.test_category_id=1 "
sSQL = sSQL & "AND tblSampleTests.sample_id=" &
rs.Fields("sample_id") & " "
sSQL = sSQL & "ORDER BY tblSampleTests.test_type_id "
Set rs2 = modData.GetRecordset(sSQL)
blRS2Open = True
While Not rs2.EOF
If rs.Fields("method_id") = 1 Then
'6-DFrame/Riffle-Run
Select Case rs2.Fields("test_type_id")
Case 87 'Modified Beck's Index
lRow = 1
lCol = 1
sTest = "Modified Beck's Index"
dblModBeck = rs2.Fields("Score")
Case 2 'Total Taxa
lRow = 1
lCol = 2
sTest = "Total Taxa"
dblTaxaRich = rs2.Fields("Score")
Case 6 'EPT Index
lRow = 1
lCol = 3
sTest = "EPT Taxa Richness"
dblEPTTaxa = rs2.Fields("Score")
Case 88 'Shannon Diversity Index
lRow = 2
lCol = 1
sTest = "Shannon Diversity"
dblShannon = rs2.Fields("Score")
Case 5 'Modified Hilsenhoff Biotic Index
lRow = 2
lCol = 2
sTest = "Hilsenhoff Biotic Index"
dblHilsenhoff = rs2.Fields("Score")
Case 7 'Percent Intolerant Taxa
lRow = 2
lCol = 3
sTest = "Percent Intolerant Individuals"
dblPctIntolerant = Left(rs2.Fields("Score"),
(InStr(rs2.Fields("Score"), "%") - 1))
Case Else
lRow = 0
lCol = 0
sTest = ""
End Select 'rs2.Fields("test_type_id")
Else
'10-DFrame/Multihabitat
Select Case rs2.Fields("test_type_id")
Case 88 'Shannon Diversity Index
lRow = 1
lCol = 1
sTest = "Shannon Diversity Index"
dblShannon = rs2.Fields("Score")
Case 2 'Total Taxa
lRow = 1
lCol = 2
sTest = "Taxa Richness"
dblTaxaRich = rs2.Fields("Score")
Case 6 'EPT Index
lRow = 1
lCol = 3
sTest = "EPT Taxa Richness"
dblEPTTaxa = rs2.Fields("Score")
Case 91 '# of Mayfly Taxa
lRow = 2
lCol = 1
sTest = "# Mayfly Taxa"
dblMayfly = rs2.Fields("Score")
Case 90 '# of Caddisfly Taxa
lRow = 2
lCol = 2
sTest = "# Caddisfly Taxa"
dblCaddisfly = rs2.Fields("Score")
Case 89 'Beck4
lRow = 2
lCol = 3
sTest = "Beck4"
dblBeck4 = rs2.Fields("Score")
Case 5 'Modified Hilsenhoff Biotic Index
lRow = 0
lCol = 0
sTest = ""
dblHilsenhoff = rs2.Fields("Score")
Case 7 'Percent Intolerant Taxa
lRow = 0
lCol = 0
sTest = ""
dblPctIntolerant = Left(rs2.Fields("Score"),
(InStr(rs2.Fields("Score"), "%") - 1))
Case 87 'Modified Beck's Index
lRow = 0
lCol = 0
sTest = ""
dblModBeck = rs2.Fields("Score")
Case Else
lRow = 0
lCol = 0
sTest = ""
End Select 'rs2.Fields("test_type_id")
End If 'rs.Fields("method_id") = 1
If lRow > 0 And lCol > 0 Then
otblMetrics.Cell(lRow, lCol).Range.InsertAfter _
sTest & ": " & rs2.Fields("Score")
End If 'lRow > 0 And lCol > 0
rs2.MoveNext
Wend 'rs2.EOF
rs2.Close
Set rs2 = Nothing
blRS2Open = False
Set otblMetrics = Nothing
Set otblBlueBox = Nothing
oSelection.EndKey Unit:=wdStory
oSelection.Font.Size = 10
oSelection.Font.Bold = False
oSelection.Font.Color = wdColorBlack
oSelection.TypeParagraph
'Taxa Table
sSQL = "SELECT GetSciOrderGroup(lT.taxa_id, lT.SciClassCd) AS
OrderGroup, "
sSQL = sSQL & "IIf(Nz(lT.SciClassCd,'')='','',"
sSQL = sSQL & "IIf(Nz(lT.SciClassCd,'')='P','Phylum ',"
sSQL = sSQL & "IIf(Nz(lT.SciClassCd,'')='C','Class ',"
sSQL = sSQL & "IIf(Nz(lT.SciClassCd,'')='O','Order ',"
sSQL = sSQL & "IIf(Nz(lT.SciClassCd,'')='F','Family ',"
sSQL = sSQL & "IIf(Nz(lT.SciClassCd,'')='G','Genus ',"
sSQL = sSQL & "IIf(Nz(lT.SciClassCd,'')='S','Species ',"
sSQL = sSQL & "'SubSpecies '))))))) & lT.TaxaName AS Taxon, "
sSQL = sSQL & "ST.TaxaCount, lT.Tolerance, lT.TrophicCd, "
sSQL = sSQL & "SciClassSort(lT.SciClassCd) AS ScientificOrder "
sSQL = sSQL & "FROM tblSampleTaxa AS ST INNER JOIN LU_Taxa AS lT "
sSQL = sSQL & "ON ST.taxa_id = lT.taxa_id "
sSQL = sSQL & "WHERE sample_id=" & rs.Fields("sample_id") & " "
sSQL = sSQL & "ORDER BY GetSciOrderGroup(lT.taxa_id,
lT.SciClassCd), "
sSQL = sSQL & "SciClassSort(lT.SciClassCd), lT.TaxaName "
Set rs2 = modData.GetRecordset(sSQL)
blRS2Open = True
If Not rs2.EOF Then
rs2.MoveLast
rs2.MoveFirst
lRecordCount2 = rs2.RecordCount
'Set a maximum of 14 content rows (not including column
headers)
If lRecordCount2 < 15 Then
Set otblTaxa = oWord.Tables.Add(oSelection.Range,
(lRecordCount2 + 1), 5)
blWrapTaxaTable = False
Else
Set otblTaxa = oWord.Tables.Add(oSelection.Range,
(Fix((lRecordCount2 + 1) / 2) + 1), 10)
blWrapTaxaTable = True
End If 'lRecordCount2 < 15
'Enter table data
otblTaxa.Rows(1).Range.Font.Bold = True
otblTaxa.Cell(1, 1).Range.Font.Size = 11
otblTaxa.Cell(1, 1).Range.InsertAfter "Taxa"
otblTaxa.Cell(1, 3).Range.Font.Size = 8
otblTaxa.Cell(1, 3).Range.InsertAfter "Number"
otblTaxa.Cell(1, 4).Range.Font.Size = 8
otblTaxa.Cell(1, 4).Range.InsertAfter "Tolerance"
otblTaxa.Cell(1, 5).Range.Font.Size = 8
otblTaxa.Cell(1, 5).Range.InsertAfter "Trophic Code"
If blWrapTaxaTable = True Then
otblTaxa.Cell(1, 8).Range.Font.Size = 8
otblTaxa.Cell(1, 8).Range.InsertAfter "Number"
otblTaxa.Cell(1, 9).Range.Font.Size = 8
otblTaxa.Cell(1, 9).Range.InsertAfter "Tolerance"
otblTaxa.Cell(1, 10).Range.Font.Size = 8
otblTaxa.Cell(1, 10).Range.InsertAfter "Trophic Code"
End If 'blWrapTaxaTable = True
lRow = 2
While Not rs2.EOF
If lRow <= otblTaxa.Rows.Count Then
otblTaxa.Cell(lRow, 1).Range.Font.Size = 8
If sCurrentTaxaOrderGroup <>
Nz(rs2.Fields("OrderGroup"), "") Then
otblTaxa.Cell(lRow, 1).Range.Font.Bold = True
otblTaxa.Cell(lRow, 1).Range.InsertAfter
Nz(rs2.Fields("OrderGroup"), "")
End If 'sCurrentTaxaOrderGroup <>
Nz(rs2.Fields("OrderGroup"), "")
otblTaxa.Cell(lRow, 2).Range.Font.Size = 8
otblTaxa.Cell(lRow, 2).Range.InsertAfter
Nz(rs2.Fields("Taxon"), "")
otblTaxa.Cell(lRow, 3).Range.Font.Size = 8
otblTaxa.Cell(lRow, 3).Range.InsertAfter
Nz(rs2.Fields("TaxaCount"), "")
otblTaxa.Cell(lRow, 4).Range.Font.Size = 8
otblTaxa.Cell(lRow, 4).Range.InsertAfter
Nz(rs2.Fields("Tolerance"), "")
otblTaxa.Cell(lRow, 5).Range.Font.Size = 8
otblTaxa.Cell(lRow, 5).Range.InsertAfter
Nz(rs2.Fields("TrophicCd"), "")
Else
otblTaxa.Cell((lRow - (otblTaxa.Rows.Count - 1)),
6).Range.Font.Size = 8
If (lRow - (otblTaxa.Rows.Count - 1)) = 2 Then
'Repeat current group header when wrapping
otblTaxa.Cell((lRow - (otblTaxa.Rows.Count -
1)), 6).Range.Font.Bold = True
otblTaxa.Cell((lRow - (otblTaxa.Rows.Count -
1)), 6).Range.InsertAfter Nz(rs2.Fields("OrderGroup"), "")
Else
If sCurrentTaxaOrderGroup <>
Nz(rs2.Fields("OrderGroup"), "") Then
otblTaxa.Cell((lRow - (otblTaxa.Rows.Count -
1)), 6).Range.Font.Bold = True
otblTaxa.Cell((lRow - (otblTaxa.Rows.Count -
1)), 6).Range.InsertAfter Nz(rs2.Fields("OrderGroup"), "")
End If 'sCurrentTaxaOrderGroup <>
Nz(rs2.Fields("OrderGroup"), "")
End If '(lRow - Fix((lRecordCount2 + 1) / 2)) = 2
otblTaxa.Cell((lRow - (otblTaxa.Rows.Count - 1)),
7).Range.Font.Size = 8
otblTaxa.Cell((lRow - (otblTaxa.Rows.Count - 1)),
7).Range.InsertAfter Nz(rs2.Fields("Taxon"), "")
otblTaxa.Cell((lRow - (otblTaxa.Rows.Count - 1)),
8).Range.Font.Size = 8
otblTaxa.Cell((lRow - (otblTaxa.Rows.Count - 1)),
8).Range.InsertAfter Nz(rs2.Fields("TaxaCount"), "")
otblTaxa.Cell((lRow - (otblTaxa.Rows.Count - 1)),
9).Range.Font.Size = 8
otblTaxa.Cell((lRow - (otblTaxa.Rows.Count - 1)),
9).Range.InsertAfter Nz(rs2.Fields("Tolerance"), "")
otblTaxa.Cell((lRow - (otblTaxa.Rows.Count - 1)),
10).Range.Font.Size = 8
otblTaxa.Cell((lRow - (otblTaxa.Rows.Count - 1)),
10).Range.InsertAfter Nz(rs2.Fields("TrophicCd"), "")
End If 'lRow <= otblTaxa.Rows.Count
lRow = lRow + 1
sCurrentTaxaOrderGroup = Nz(rs2.Fields("OrderGroup"), "")
rs2.MoveNext
Wend
rs2.Close
Set rs2 = Nothing
blRS2Open = False
'Style the table
otblTaxa.PreferredWidthType = wdPreferredWidthPoints
otblTaxa.Columns(1).Width = InchesToPoints(0.8)
otblTaxa.Columns(2).Width = InchesToPoints(0.8)
otblTaxa.Columns(3).Width = InchesToPoints(0.5)
otblTaxa.Columns(4).Width = InchesToPoints(0.6)
otblTaxa.Columns(5).Width = InchesToPoints(0.5)
otblTaxa.Borders(wdBorderDiagonalDown).LineStyle =
wdLineStyleNone
otblTaxa.Borders(wdBorderDiagonalUp).LineStyle =
wdLineStyleNone
otblTaxa.Borders(wdBorderLeft).LineStyle = wdLineStyleSingle
otblTaxa.Borders(wdBorderLeft).LineWidth = wdLineWidth025pt
otblTaxa.Borders(wdBorderRight).LineStyle = wdLineStyleSingle
otblTaxa.Borders(wdBorderRight).LineWidth = wdLineWidth025pt
otblTaxa.Borders(wdBorderTop).LineStyle = wdLineStyleSingle
otblTaxa.Borders(wdBorderTop).LineWidth = wdLineWidth025pt
otblTaxa.Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
otblTaxa.Borders(wdBorderBottom).LineWidth = wdLineWidth025pt
otblTaxa.Borders(wdBorderHorizontal).LineStyle =
wdLineStyleSingle
otblTaxa.Borders(wdBorderHorizontal).LineWidth =
wdLineWidth025pt
otblTaxa.Borders(wdBorderVertical).LineStyle =
wdLineStyleSingle
otblTaxa.Borders(wdBorderVertical).LineWidth =
wdLineWidth025pt
otblTaxa.Columns(1).Borders(wdBorderLeft).LineStyle =
wdLineStyleNone
otblTaxa.Columns(1).Borders(wdBorderHorizontal).LineStyle =
wdLineStyleNone
otblTaxa.Columns(1).Borders(wdBorderTop).LineStyle =
wdLineStyleNone
otblTaxa.Columns(1).Borders(wdBorderBottom).LineStyle =
wdLineStyleNone
otblTaxa.Columns(2).Borders(wdBorderLeft).LineStyle =
wdLineStyleNone
otblTaxa.Columns(2).Borders(wdBorderHorizontal).LineStyle =
wdLineStyleNone
otblTaxa.Columns(2).Borders(wdBorderTop).LineStyle =
wdLineStyleNone
otblTaxa.Columns(2).Borders(wdBorderBottom).LineStyle =
wdLineStyleNone
If blWrapTaxaTable = True Then
otblTaxa.Columns(6).Width = InchesToPoints(0.8)
otblTaxa.Columns(6).Borders(wdBorderHorizontal).LineStyle = wdLineStyleNone
otblTaxa.Columns(6).Borders(wdBorderTop).LineStyle =
wdLineStyleNone
otblTaxa.Columns(6).Borders(wdBorderBottom).LineStyle =
wdLineStyleNone
otblTaxa.Columns(7).Width = InchesToPoints(0.8)
otblTaxa.Columns(7).Borders(wdBorderLeft).LineStyle =
wdLineStyleNone
otblTaxa.Columns(7).Borders(wdBorderHorizontal).LineStyle = wdLineStyleNone
otblTaxa.Columns(7).Borders(wdBorderTop).LineStyle =
wdLineStyleNone
otblTaxa.Columns(7).Borders(wdBorderBottom).LineStyle =
wdLineStyleNone
otblTaxa.Columns(8).Width = InchesToPoints(0.5)
otblTaxa.Columns(9).Width = InchesToPoints(0.6)
otblTaxa.Columns(10).Width = InchesToPoints(0.5)
End If 'blWrapTaxaTable = True
otblTaxa.AutoFitBehavior wdAutoFitContent
Set otblTaxa = Nothing
End If 'Not rs2.EOF
oSelection.EndKey Unit:=wdStory
oSelection.TypeParagraph