PDA

View Full Version : Solved: type mismatch problem



gibbo1715
01-22-2006, 05:57 AM
All

I am trying to populate a word form from an access database using vb6

Can any one tell me why im getting a type mismatch at the line Set new_range = doc.Range please

cheers

Gibbo



Dim db_name As String
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim new_field As ADODB.Field
Dim txt As String
Dim new_range As Range
Dim ws As Word.Application
Dim doc As Word.Document

Set ws = New Word.Application
Set doc = ws.Documents.Open(App.Path & "\Test.doc")

' Compose the database name.
db_name = "C:\\Smythe.gbo"

' Connect to the database.
Set conn = New ADODB.Connection
conn.Mode = adModeRead
conn.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & db_name & ";Jet OLEDB:Database Password=Password;"
conn.Open

' Select all of the records in the Customers table.
Set rs = conn.Execute( _
"SELECT Customers_No, Ref_No, " & _
"Current_Location " & _
"FROM Customers Where On_List = Yes")

' Add a row containing the field names.
For Each new_field In rs.Fields
txt = txt & vbTab & new_field.Name
Next new_field
txt = Mid$(txt, 2) & vbCrLf ' Remove leading tab.

' Get the Recordset's data as a single string
' with vbTab between fields and vbCrLf between rows.
txt = txt & rs.GetString( _
ColumnDelimeter:=vbTab, _
RowDelimeter:=vbCrLf, _
NullExpr:="<null>")

' Close the Recordset and Connection.
rs.Close
conn.Close
With doc
' Make a Range at the end of the Word document.
Set new_range = doc.Range
new_range.Collapse wdCollapseEnd

' Insert the text and convert it to a table.
new_range.InsertAfter txt
new_range.ConvertToTable vbTab

' Autofit to the contents.
new_range.Tables(1).AutoFitBehavior wdAutoFitContent

' Add a blank line.
Set new_range = doc.Range
new_range.Collapse wdCollapseEnd
new_range.InsertParagraph
new_range.Collapse wdCollapseEnd
new_range.InsertParagraph
End With

gibbo1715
01-22-2006, 06:48 AM
figured it out


the following works for me

Gibbo


Dim db_name As String
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim new_field As ADODB.Field
Dim txt As String
Dim new_range As Range
Dim ws As Word.Application
Dim doc As Word.Document

Set ws = New Word.Application
Set doc = ws.Documents.Open(App.Path & "\Test.doc")

' Compose the database name.
db_name = "C:\\Smythe.gbo"

' Connect to the database.
Set conn = New ADODB.Connection
conn.Mode = adModeRead
conn.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & db_name & ";Jet OLEDBhttp://www.visualbasicforum.com/x_images/images/smilies/biggrin.gifatabase Password=Password;"
conn.Open

' Select all of the records in the Customers table.
Set rs = conn.Execute( _
"SELECT Customers_No, Ref_No, " & _
"Current_Location " & _
"FROM Customers Where On_List = Yes")

' Add a row containing the field names.
For Each new_field In rs.Fields
txt = txt & vbTab & new_field.Name
Next new_field
txt = Mid$(txt, 2) & vbCrLf ' Remove leading tab.

' Get the Recordset's data as a single string
' with vbTab between fields and vbCrLf between rows.
txt = txt & rs.GetString( _
ColumnDelimeter:=vbTab, _
RowDelimeter:=vbCrLf, _
NullExpr:="<null>")

' Close the Recordset and Connection.
rs.Close
conn.Close


With doc
' Make a Range at the end of the Word document.
Set new_range = .Range
new_range.Collapse wdCollapseEnd

' Insert the text and convert it to a table.
new_range.InsertAfter txt
new_range.ConvertToTable vbTab

' Autofit to the contents.
new_range.Tables(1).AutoFitBehavior wdAutoFitContent

' Add a blank line.
Set new_range = doc.Range
new_range.Collapse wdCollapseEnd
new_range.InsertParagraph
new_range.Collapse wdCollapseEnd
End With