mwvmathewson
New member
- Local time
- Today, 10:39
- Joined
- Aug 14, 2012
- Messages
- 3
Hi! I wonder if anyone can help work out what is going wrong with this code. I am trying to fill a Word document (Insurance Form) by putting text fields in their table document and filling it from a stored procedure. Every request is a single one so I request the relevant numbers using input boxes. The stored procedure returns the data required but Word element sees no form fields. I have been trying to sort this for days! :banged:
Everything runs fine but nothing is returned to the document. Data is fine when debug.print used on it. Problem I think is the document
Code:
Private Sub VCFOALperCertificate_Click()
Dim intCertNum As Integer
Dim intDeclaration As Integer
'Get user input for foal
intCertNum = InputBox("Enter the Certificate Number")
intDeclaration = InputBox("Enter the Declaration Number")
On Error GoTo Err_VCFOALperCertificate_Click
'Print Veterinary Certificate for chosen foal
Dim appWord As Word.Application
Dim doc As Word.Document
'Avoid error 429 when Word isn't open.
On Error Resume Next
Err.Clear
'Set appWord object variable to running instance of Word
Set appWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then
'If Word isn't open, create a new instance of it.
Set appWord = New Word.Application
End If
Set doc = appWord.Documents.Open("Document Path", , False)
' Initialize variables.
Dim cn As New ADODB.Connection
Dim ServerName As String, DatabaseName As String
' Put text box values into connection variables.
ServerName = "MyServer"
DatabaseName = "dbName"
' Specify the OLE DB provider.
cn.Provider = "sqloledb"
' Set SQLOLEDB connection properties.
cn.Properties("Data Source").Value = ServerName
cn.Properties("Initial Catalog").Value = DatabaseName
' Windows NT authentication.
cn.Properties("Integrated Security").Value = "SSPI"
' Open the database.
cn.Open
Dim str As String
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
str = "EXEC spVetCert " & intCertNum & " , " & intDeclaration & ""
Set doc = ActiveDocument
If doc.ProtectionType <> wdNoProtection Then doc.Unprotect
rst.Open str, cn
With doc
.FormFields("Insured").Result = rst("Insured")
.FormFields("Certificate").Result = rst("Identifier")
.FormFields("Sire").Result = rst("Sire")
.FormFields("Dam").Result = rst("Dam")
.FormFields("Breed").Result = rst("Breed")
.FormFields("Sex").Result = rst("Sex")
.Visible = True
.Activate
End With
Set doc = Nothing
Set appWord = Nothing
Exit Sub
Err_VCFOALperCertificate_Click:
MsgBox Err.Number & ": " & Err.Description
End Sub
Everything runs fine but nothing is returned to the document. Data is fine when debug.print used on it. Problem I think is the document
Code:
Private Sub VCFOALperCertificate_Click()
Dim intCertNum As Integer
Dim intDeclaration As Integer
'Get user input for foal
intCertNum = InputBox("Enter the Certificate Number")
intDeclaration = InputBox("Enter the Declaration Number")
On Error GoTo Err_VCFOALperCertificate_Click
'Print Veterinary Certificate for chosen foal
Dim appWord As Word.Application
Dim doc As Word.Document
'Avoid error 429 when Word isn't open.
On Error Resume Next
Err.Clear
'Set appWord object variable to running instance of Word
Set appWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then
'If Word isn't open, create a new instance of it.
Set appWord = New Word.Application
End If
Set doc = appWord.Documents.Open("Document Path", , False)
' Initialize variables.
Dim cn As New ADODB.Connection
Dim ServerName As String, DatabaseName As String
' Put text box values into connection variables.
ServerName = "MyServer"
DatabaseName = "dbName"
' Specify the OLE DB provider.
cn.Provider = "sqloledb"
' Set SQLOLEDB connection properties.
cn.Properties("Data Source").Value = ServerName
cn.Properties("Initial Catalog").Value = DatabaseName
' Windows NT authentication.
cn.Properties("Integrated Security").Value = "SSPI"
' Open the database.
cn.Open
Dim str As String
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
str = "EXEC spVetCert " & intCertNum & " , " & intDeclaration & ""
Set doc = ActiveDocument
If doc.ProtectionType <> wdNoProtection Then doc.Unprotect
rst.Open str, cn
With doc
.FormFields("Insured").Result = rst("Insured")
.FormFields("Certificate").Result = rst("Identifier")
.FormFields("Sire").Result = rst("Sire")
.FormFields("Dam").Result = rst("Dam")
.FormFields("Breed").Result = rst("Breed")
.FormFields("Sex").Result = rst("Sex")
.Visible = True
.Activate
End With
Set doc = Nothing
Set appWord = Nothing
Exit Sub
Err_VCFOALperCertificate_Click:
MsgBox Err.Number & ": " & Err.Description
End Sub