ник: ROM!MDB
Вот хороший пример неумелого взаимствования возможно хороших наработок..
Пытаюсь разобраться в проблеме формирования таблицы, многое не понятно. Когда текст формируется закладками все работает, а вот при попытке заставить его сформировать таблицу - возникает ошибка мол: не правильное использование ключевого слова...в тексте кода указана строка - ругается на использование New Recordset
Может проблема в версии - пример кода под Access 2000 - , а я его пытаюсь под под 2003 использовать..
Подскажите кто разумеет если не сложно. Вот Код:
Function CreateTableFromRecordset( _
rngAny As Word.Range, _
rstAny As ADODB.Recordset, _
Optional fIncludeFieldNames As Boolean = False) _
As Word.Table
Dim objTable As Word.Table
Dim fldAny As ADODB.Field
Dim varData As Variant
Dim strBookmark As String
Dim cField As Long
' Get the data from the recordset
varData = rstAny.GetString()
' Create the table
With rngAny
' Creating the basic table is easy,
' just insert the tab-delimted text
' add convert it to a table
.InsertAfter varData
Set objTable = .ConvertToTable()
' Field names are more work since
' you must do them one at a time
If fIncludeFieldNames Then
With objTable
' Add a new row on top and make it a heading
.Rows.Add(.Rows(1)).HeadingFormat = True
' Iterate through the fields and add their
' names to the heading row
For Each fldAny In rstAny.Fields
cField = cField + 1
.Cell(1, cField).Range.Text = fldAny.Name
Next
End With
End If
End With
Set CreateTableFromRecordset = objTable
End Function
Sub PrintInvoiceWithWord(F_REPORT_PR As Form_F_REPORT_PR)
Dim objWord As Word.Application
Dim rst As Recordset
Dim strSQL As String
' Launch Word and load the invoice template
Set objWord = New Word.Application
objWord.Documents.Add _
Application.CurrentProject.Path & "\OPRED.dot"
objWord.Visible = True
' Add header information using predefined bookmarks
With objWord.ActiveDocument.Bookmarks
.Item("ГодГОЗ").Range.Text = F_REPORT_PR.YEAR_GOZ
.Item("Наимпр").Range.Text = F_REPORT_PR.Полное_наименование
.Item("Адрес").Range.Text = F_REPORT_PR.Адрес
.Item("date0").Range.Text = F_REPORT_PR.DATE_BEGIN
.Item("date1").Range.Text = F_REPORT_PR.DATE_END
.Item("AKbl_TEXT").Range.Text = F_REPORT_PR.INT_Kbl_1
.Item("AKtl_TEXT").Range.Text = F_REPORT_PR.INT_Ktl1
.Item("AKal_TEXT").Range.Text = F_REPORT_PR.INT_Kal1
.Item("AKobos_TEXT").Range.Text = F_REPORT_PR.INT_Koboms
.Item("AKa_text").Range.Text = F_REPORT_PR.AKa_text1
.Item("AKzs_text").Range.Text = F_REPORT_PR.AKzs_text
.Item("AKosos_TEXT").Range.Text = F_REPORT_PR.AKosos1_text
.Item("S_text").Range.Text = F_REPORT_PR.S_text
End With
' Build SQL string for details
strSQL = "SELECT [NIOKR], [ZAKL], [ZAKL_W], " & _
"Disc, Extended FROM [Z_LOT_USL_ZAK_ISP] " & _
"WHERE ID_PR = " & F_REPORT_PR.[KOD_PR] And "WHERE YEAR = " & F_REPORT_PR.[T_LOT.YEAR]
' Get details from database and create a table
' in the document
Set rst = New Recordset
rst.Open strSQL, CurrentProject.Connection
With CreateTableFromRecordset( _
objWord.ActiveDocument.Bookmarks("tbl").Range, rst, True)
' 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
' We're done
Set objWord = Nothing
End Sub
|
Да! Есть еще класс следующего содержания:
Private WithEvents mobjWordApp As Word.Application
Private Sub Class_Initialize()
Set mobjWordApp = New Word.Application
mobjWordApp.Visible = True
End Sub
Private Sub Class_Terminate()
Set mobjWordApp = Nothing
End Sub
Private Sub mobjWordApp_DocumentBeforeClose(ByVal Doc As Word.Document, Cancel As Boolean)
Debug.Print "Document being closed: " & Doc.Name
End Sub
Private Sub mobjWordApp_DocumentBeforeSave(ByVal Doc As Word.Document, SaveAsUI As Boolean, Cancel As Boolean)
Debug.Print "Document being saved: " & Doc.Name
End Sub
Private Sub mobjWordApp_DocumentChange()
On Error Resume Next
Debug.Print "Document change. New document is " & _
mobjWordApp.ActiveDocument.Name
End Sub
Private Sub mobjWordApp_DocumentOpen(ByVal Doc As Word.Document)
Debug.Print "Document being opened: " & Doc.Name
End Sub
Private Sub mobjWordApp_NewDocument(ByVal Doc As Word.Document)
Debug.Print "Document being created: " & Doc.Name
End Sub
Private Sub mobjWordApp_Quit()
Debug.Print "Oh no! Word is quitting!"
End Sub