Hi team, I am chasing some help on this one. I have a word document template with Text Form Fields where data is to be put. I am trying to set it up so that when these fields are filled out in work, you can press a button and the information gets transferred into a related table in excel. Each time word is filled out, the data goes to the new row.
My VBA skills are novice and am chasing some help with where I am at.
The word 2010 document is called Report2.docm
The excel 2010 spreadsheet is called Transfer.xlsx
Here is what I have been able to come across so far.
The error I am getting is "5941: the request member of the collection does not exist" followed by "Run-Time error '91' - Object variable or with block variable not set"
It is then highlighting the 4th last line of code "cnn.close"
If this is not even close, I'm happy to start again.
Any help would be greatly appreciated
My VBA skills are novice and am chasing some help with where I am at.
The word 2010 document is called Report2.docm
The excel 2010 spreadsheet is called Transfer.xlsx
Here is what I have been able to come across so far.
Code:
Private Sub CommandButton1_Click()
'Transfer a single record from the form fields to an Excel workbook.
Dim doc As Document
Dim strCompanyName As String
Dim strPhone As String
Dim strSQL As String
Dim cnn As ADODB.Connection
'Get data.
Set doc = ThisDocument
On Error GoTo ErrHandler
strNearMissID = Chr(39) & doc.FormFields("txtNearMissID").Result & Chr(39)
strEventDate = Chr(39) & doc.FormFields("txtEventDate").Result & Chr(39)
strEventTime = Chr(39) & doc.FormFields("txtEventTime").Result & Chr(39)
strEventLocation = Chr(39) & doc.FormFields("txtEventLocation").Result & Chr(39)
strWorkstation = Chr(39) & doc.FormFields("txtWorkstation").Result & Chr(39)
strReportedTo = Chr(39) & doc.FormFields("txtReportedTo").Result & Chr(39)
strRecordedHistory = Chr(39) & doc.FormFields("txtRecordedHistory").Result & Chr(39)
strNCOInvolved = Chr(39) & doc.FormFields("txtNCOInvolved").Result & Chr(39)
strTCLOnDuty = Chr(39) & doc.FormFields("txtTCLOnDuty").Result & Chr(39)
strDescription = Chr(39) & doc.FormFields("txtDescription").Result & Chr(39)
strOnShift = Chr(39) & doc.FormFields("txtOnShift").Result & Chr(39)
strImmediateAction = Chr(39) & doc.FormFields("txtImmediateAction").Result & Chr(39)
strEvidence = Chr(39) & doc.FormFields("txtEvidence").Result & Chr(39)
'Define sql string used to insert each record in the destination workbook.
'Don't omit the $ in the sheet identifier.
strSQL = "INSERT INTO [Transfer$]" _
& " (NearMissID, EventDate, EventTime, EventLocation, Workstation, ReportedTo, RecordedHistory, NCOInvolved, TCLOnDuty, Decription, OnShift, ImmediateAction, Evidence)" _
& " VALUES (" _
& strNearMissID & ", " _
& strEventDate _
& strEventTime _
& strEventLocation _
& strWorkstation _
& strReportedTo _
& strRecordedHistory _
& strNCOInvloved _
& strTCLOnDuty _
& strDescription _
& strOnShift _
& strImmediateAction _
& strEvidence _
& ")"
Debug.Print strSQL
'Define connection string and open connection to destination workbook file.
Set cnn = New ADODB.Connection
With cnn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=\\location\Transfer.xlsx" & _
"Extended Properties=Excel 8.0;"
.Open
'Transfer data.
.Execute strSQL
End With
Set doc = Nothing
Set cnn = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Number & ": " & Err.Description, _
vbOKOnly, "Error"
On Error GoTo 0
On Error Resume Next
cnn.Close
Set doc = Nothing
Set cnn = Nothing
End Sub
The error I am getting is "5941: the request member of the collection does not exist" followed by "Run-Time error '91' - Object variable or with block variable not set"
It is then highlighting the 4th last line of code "cnn.close"
If this is not even close, I'm happy to start again.
Any help would be greatly appreciated