View Single Post
 
Old 03-28-2017, 10:04 PM
Seb Seb is offline Windows 10 Office 2016
Novice
 
Join Date: Mar 2017
Posts: 7
Seb is on a distinguished road
Default Macro for exporting Word data to an excel register

Hi everyone,

I have been searching for quite some time on how to solve this problem and create a macro that will export certain information from a word document and place it into an excel register.

I have found information on how to do this at techrepublic:
http://www.techrepublic.com/blog/10-...an-excel-sheet

However I cannot get the macro to work. It returns two errors.

The first is "5941: The requested member of the collection does not exist"

The second is "Runt-time error '91': Object variable or With block variable not set"

The macro code in use is:

Code:
Private Sub Export_Click()
'Transfer a single record from the form fields to an Excel workbook.
  Dim doc As Document
  Dim strCARNumber As String
  Dim strLocation As String
  Dim strDate As Date
  Dim strRisk As String
  Dim strCategory As String
  Dim strDetails As String
  Dim strInitiator As String
  Dim strResponsible As String
  Dim strDepartment As String
  Dim strDateComplete As String
  Dim strDateVerified As Date
  Dim strSQL As Date
  Dim cnn As ADODB.Connection
  'Get data.
  Set doc = ActiveDocument
  On Error GoTo ErrHandler
  strCARNumber = Chr(39) & doc.FormFields("txtCARNumber").Result & Chr(39)
  strLocation = Chr(39) & doc.FormFields("txtLocation").Result & Chr(39)
  strDate = Chr(39) & doc.FormFields("txtDate").Result & Chr(39)
  strRisk = Chr(39) & doc.FormFields("txtRisk").Result & Chr(39)
  strCategory = Chr(39) & doc.FormFields("txtCategory").Result & Chr(39)
  strDetails = Chr(39) & doc.FormFields("txtDetails").Result & Chr(39)
  strInitiator = Chr(39) & doc.FormFields("txtInitiator").Result & Chr(39)
  strResponsible = Chr(39) & doc.FormFields("txtResponsible").Result & Chr(39)
  strDepartment = Chr(39) & doc.FormFields("txtDepartment").Result & Chr(39)
  strDateComplete = Chr(39) & doc.FormFields("txtDateComplete").Result & Chr(39)
  strDateVerified = Chr(39) & doc.FormFields("txtDateVerified").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 [Sheet1$]" _
    & " (CAR_No., Category, Department, Site_Location, Initiator, Responsible_Party, Date_Raised, Details, Priority, Date_completed, Date_verified)" _
    & " VALUES (" _
    & strCARNumber & ", " _
    & strLocation & ", " _
    & strDate & ", " _
    & strRisk & ", " _
    & strCategory & ", " _
    & strDetails & ", " _
    & strInitiator & ", " _
    & strResponsible & ", " _
    & strDepartment & ", " _
    & strDateComplete & ", " _
    & strDateVerified _
    & ")"
  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=(withheld);" & _
      "Extended Properties=Excel 8.0; HDR=No"
    .Open
    'Transfer data.
    .Execute strSQL
  End With
  Set doc = Nothing
  Set cnn = Nothing
  End 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
I have withheld the data source information.

The debug highlight is coloured red.

Any help on this would be greatly welcomed. I cannot find an answer online and it has been quite some years since I last used VB.

If more information is required I can attached the files that I am trying to export from and export to.

Last edited by Seb; 03-28-2017 at 10:06 PM. Reason: correcting web link
Reply With Quote