Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 03-28-2017, 10:04 PM
Seb Seb is offline Macro for exporting Word data to an excel register Windows 10 Macro for exporting Word data to an excel register Office 2016
Novice
Macro for exporting Word data to an excel register
 
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
 

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exporting multiple data sets from a single Word Form into Excel Tom Saylor Word VBA 6 09-11-2016 05:22 AM
Macro for exporting Word data to an excel register Exporting specific data fields from MS Word 2013 to a MS Excel 2013 spreadsheet Labyrinth Word 7 07-19-2016 01:35 PM
Macro for exporting Word data to an excel register Exporting Resource Usage data to Excel samg54 Project 1 08-02-2013 05:28 AM
Macro for exporting Word data to an excel register Macro: Exporting Data to a LEGIBLE Excel Spreadsheet jeffcoleky Word VBA 6 05-08-2012 08:24 AM
Macro for exporting Word data to an excel register * Exporting Access Data to Excel djreyrey Excel Programming 1 03-23-2012 10:03 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:09 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft