I am trying to create a word doc. table from an array. My code is as follows. Everything appears to be working, I pass the array to the function, read the data, but I am not creating the table at the end of the function, and therefore am not passing the table back to the sub.
Thanks for any help in advance.
BusMgr
Sub PrintReportWithWord(frmDay As Form_frmDay)
On Error GoTo PrintReportWithWord_Err
Dim objWord As New Word.Application
Dim dbs as Database
Dim strSQL As String
Dim strSQL1 As String
Dim rst As Recordset
Dim rst1 As Recordset
Dim IntCtr As Integer
Dim vardata As Variant
Dim intCount As Integer
Dim intI As Integer
Dim intJ As Integer
Dim strOPS As String
Set dbs = CurrentDb()
DoCmd.SetWarnings False
On Error Resume Next
strSQL = "DELETE * FROM tblDailyOpsRpt"
DoCmd.RunSQL strSQL
For IntCtr = 1 To 24
strSQL = "INSERT INTO tblDailyOpsRpt (DayID, Operation, Detail, FromDepth, ToDepth, WOB, GPM) SELECT Operations.DayID, Operations.Operation" & IntCtr & ", Operations.Detail" & IntCtr & ",Operations.FromDepth" & IntCtr & ",Operations.ToDepth" & IntCtr & ",Operations.WOB" & IntCtr & ", Operations.GPM" & IntCtr & " FROM Operations WHERE (((Operations.DayID)=[Forms]![frmDay]![Status].[Form]![DayID]))"
DoCmd.RunSQL strSQL
Next IntCtr
DoCmd.SetWarnings True
strSQL = "SELECT * FROM tblDailyOpsRpt where tblDailyOpsRpt.DayID =" & Forms!frmDay!Status.Form.DayID
Set rst = dbs.OpenRecordset(strSQL, dbOpenSnapshot)
vardata = rst.GetRows(10000)
intCount = UBound(vardata, 2) + 1
rst.Close
'Launch Word and load the Report Template
Set objWord = New Word.Application
objWord.Documents.Add _
Application.CurrentProject.Path & "\MgrsRpt.dot"
objWord.Visible = True
'Add header information using predefined bookmarks
With objWord.ActiveDocument.Bookmarks
.Item("WellName"
.Range.Text = frmDay.WellName
.Item("County"
.Range.Text = frmDay.County
.Item("WellNo"
.Range.Text = frmDay.WellNo
.Item("State"
.Range.Text = frmDay.State
.Item("RptDate"
.Range.Text = Forms!frmDay!Status.Form!Date
.Item("OART"
.Range.Text = Forms!frmDay!Status.Form!OART
.Item("CumCost"
.Range.Text = FormatCurrency(Forms!frmDay!Status.Form!txtCasingIntanCum, 2)
.Item("DailyCost"
.Range.Text = FormatCurrency(Forms!frmDay!Status.Form!txtCasingIntanTotal, 2)
.Item("DOW"
.Range.Text = "Day " & Forms!frmDay!Status.Form!DaysOnWell
.Item("MD"
.Range.Text = Forms!frmDay!Status.Form!DepthAt & "'"
.Item("Ftg"
.Range.Text = Forms!frmDay!Status.Form![DrilledIn24] & "'"
.Item("RI"
.Range.Text = FormatNumber(Forms!frmDay.RevInt, 1)
.Item("WI"
.Range.Text = FormatNumber(Forms!frmDay.WorkInt, 1)
.Item("TWC"
.Range.Text = FormatCurrency(Forms!frmDay.WetHoleCost, 2)
.Item("DHC"
.Range.Text = FormatCurrency(Forms!frmDay.DryHoleCost, 2)
.Item("StrOps"
.Range.Text = strOPS
End With
'**********************************************************************************
'Get details from DB and create a table in the document
'Build SQL string for bit details
strSQL1 = "SELECT * from [Bit] WHERE [Bit].[DayID] =" & [Forms]![frmDay]![Status].[Form]![DayID]
'Get details from database and create a table
'in the document
Set rst1 = dbs.OpenRecordset(strSQL1, dbOpenSnapshot)
vardata = rst1.GetRows(10000)
intCount = UBound(vardata, 2) + 1
With CreateTableFromRecordset1(objWord.ActiveDocument.Bookmarks("Details"
.Range, vardata1, intCount, False)
'Add rows for bit information
With .Rows.Add
.Cells(1).Range.Text = "Bit No:"
.Cells(2).Range.Text = (Forms![frmDay]![Status].Form![Bit subform1].Form![Bit No])
End With
'Apply Formatting
.AutoFormat wdTableFormatProfessional
.AutoFitBehavior wdAutoFitContent
'Fix up paragraph alignment
.Range.ParagraphFormat.Alignment = wdAlignParagraphRight
.Columns(1).Select
objWord.Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
objWord.Selection.MoveDown
End With
PrintReportWithWord_Err:
'If a field on the form is empty, remove the bookmark text, and continue.
If Err.Number = 94 Then
objWord.Selection.Text = ""
Resume Next
End If
End Sub
Function CreateTableFromRecordset1( _
rngAny As Word.Range, _
vardata As Variant, _
intCount As Integer, _
Optional fIncludeFieldNames As Boolean = False) _
As Word.Table
Dim objTable As Word.Table
Dim fldAny As Field
Dim strBookmark As String
'Get the data from the Recordset &
'Create the table
With rngAny
'Creating the basic table should be easy!,
'just insert the tab-delimited text
'and convert it to a table
.InsertAfter vardata
Set objTable = .ConvertToTable()
End With
Set CreateTableFromRecordset1 = objTable
End Function
Thanks for any help in advance.
BusMgr
Sub PrintReportWithWord(frmDay As Form_frmDay)
On Error GoTo PrintReportWithWord_Err
Dim objWord As New Word.Application
Dim dbs as Database
Dim strSQL As String
Dim strSQL1 As String
Dim rst As Recordset
Dim rst1 As Recordset
Dim IntCtr As Integer
Dim vardata As Variant
Dim intCount As Integer
Dim intI As Integer
Dim intJ As Integer
Dim strOPS As String
Set dbs = CurrentDb()
DoCmd.SetWarnings False
On Error Resume Next
strSQL = "DELETE * FROM tblDailyOpsRpt"
DoCmd.RunSQL strSQL
For IntCtr = 1 To 24
strSQL = "INSERT INTO tblDailyOpsRpt (DayID, Operation, Detail, FromDepth, ToDepth, WOB, GPM) SELECT Operations.DayID, Operations.Operation" & IntCtr & ", Operations.Detail" & IntCtr & ",Operations.FromDepth" & IntCtr & ",Operations.ToDepth" & IntCtr & ",Operations.WOB" & IntCtr & ", Operations.GPM" & IntCtr & " FROM Operations WHERE (((Operations.DayID)=[Forms]![frmDay]![Status].[Form]![DayID]))"
DoCmd.RunSQL strSQL
Next IntCtr
DoCmd.SetWarnings True
strSQL = "SELECT * FROM tblDailyOpsRpt where tblDailyOpsRpt.DayID =" & Forms!frmDay!Status.Form.DayID
Set rst = dbs.OpenRecordset(strSQL, dbOpenSnapshot)
vardata = rst.GetRows(10000)
intCount = UBound(vardata, 2) + 1
rst.Close
'Launch Word and load the Report Template
Set objWord = New Word.Application
objWord.Documents.Add _
Application.CurrentProject.Path & "\MgrsRpt.dot"
objWord.Visible = True
'Add header information using predefined bookmarks
With objWord.ActiveDocument.Bookmarks
.Item("WellName"
.Item("County"
.Item("WellNo"
.Item("State"
.Item("RptDate"
.Item("OART"
.Item("CumCost"
.Item("DailyCost"
.Item("DOW"
.Item("MD"
.Item("Ftg"
.Item("RI"
.Item("WI"
.Item("TWC"
.Item("DHC"
.Item("StrOps"
End With
'**********************************************************************************
'Get details from DB and create a table in the document
'Build SQL string for bit details
strSQL1 = "SELECT * from [Bit] WHERE [Bit].[DayID] =" & [Forms]![frmDay]![Status].[Form]![DayID]
'Get details from database and create a table
'in the document
Set rst1 = dbs.OpenRecordset(strSQL1, dbOpenSnapshot)
vardata = rst1.GetRows(10000)
intCount = UBound(vardata, 2) + 1
With CreateTableFromRecordset1(objWord.ActiveDocument.Bookmarks("Details"
'Add rows for bit information
With .Rows.Add
.Cells(1).Range.Text = "Bit No:"
.Cells(2).Range.Text = (Forms![frmDay]![Status].Form![Bit subform1].Form![Bit No])
End With
'Apply Formatting
.AutoFormat wdTableFormatProfessional
.AutoFitBehavior wdAutoFitContent
'Fix up paragraph alignment
.Range.ParagraphFormat.Alignment = wdAlignParagraphRight
.Columns(1).Select
objWord.Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
objWord.Selection.MoveDown
End With
PrintReportWithWord_Err:
'If a field on the form is empty, remove the bookmark text, and continue.
If Err.Number = 94 Then
objWord.Selection.Text = ""
Resume Next
End If
End Sub
Function CreateTableFromRecordset1( _
rngAny As Word.Range, _
vardata As Variant, _
intCount As Integer, _
Optional fIncludeFieldNames As Boolean = False) _
As Word.Table
Dim objTable As Word.Table
Dim fldAny As Field
Dim strBookmark As String
'Get the data from the Recordset &
'Create the table
With rngAny
'Creating the basic table should be easy!,
'just insert the tab-delimited text
'and convert it to a table
.InsertAfter vardata
Set objTable = .ConvertToTable()
End With
Set CreateTableFromRecordset1 = objTable
End Function