G
Greg
I am trying to export data from a Word form to a Excel spread sheet.
The Word form has three data fields. I want to export the form data to
a new row directly under existing rows in the spreadsheet (i.e., as
each new form is created I can run the macro and export the data to the
growing spreadsheet).
I have this code:
Sub ExportToExcel()
Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim myWB As String
Dim oFF As FormField
Dim i As Long
myWB = "E:\My Documents\Word\Word Documents\Word Tips\Macros\Working
With Access And Excel\myExportBook1.xls"
Set oXL = New Excel.Application
On Error GoTo Err_Handler
Set oWB = oXL.Workbooks.Open(FileName:=myWB)
Set oSheet = oWB.Sheets("Sheet1")
Dim LastRow As Long
LastRow = oSheet.Cells(Rows.Count, "C").End(xlUp).Row
i = 1
For Each oFF In ActiveDocument.FormFields
oSheet.Cells(LastRow + 1, i).Value = oFF.Result
i = i + 1
Next oFF
Set oSheet = Nothing
Set oWB = Nothing
Set oXL = Nothing
Excel.Application.Quit
Exit Sub
Err_Handler:
MsgBox myWB & " caused a problem. " & Err.Description, vbCritical,
"Error: " _
& Err.Number
End Sub
It is working in part. By that I mean, when it works it does export
the data as expected. The problem is that it keeps throwing errors
(normal Error 462) on the "Lastrow .... line.
The problem seems to be the the Excel.Application.Quit command is not
working. Even when I get the thing to work by stepping through the
code, the code sticks on the "Exit Sub" line. If I go to the Windows
Task Manager I can see instances of Excel processes still ongoing.
The process seems like it should be simple and straigtforward but
obviously I am missing a big piece. Thanks for your help.
The Word form has three data fields. I want to export the form data to
a new row directly under existing rows in the spreadsheet (i.e., as
each new form is created I can run the macro and export the data to the
growing spreadsheet).
I have this code:
Sub ExportToExcel()
Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim myWB As String
Dim oFF As FormField
Dim i As Long
myWB = "E:\My Documents\Word\Word Documents\Word Tips\Macros\Working
With Access And Excel\myExportBook1.xls"
Set oXL = New Excel.Application
On Error GoTo Err_Handler
Set oWB = oXL.Workbooks.Open(FileName:=myWB)
Set oSheet = oWB.Sheets("Sheet1")
Dim LastRow As Long
LastRow = oSheet.Cells(Rows.Count, "C").End(xlUp).Row
i = 1
For Each oFF In ActiveDocument.FormFields
oSheet.Cells(LastRow + 1, i).Value = oFF.Result
i = i + 1
Next oFF
Set oSheet = Nothing
Set oWB = Nothing
Set oXL = Nothing
Excel.Application.Quit
Exit Sub
Err_Handler:
MsgBox myWB & " caused a problem. " & Err.Description, vbCritical,
"Error: " _
& Err.Number
End Sub
It is working in part. By that I mean, when it works it does export
the data as expected. The problem is that it keeps throwing errors
(normal Error 462) on the "Lastrow .... line.
The problem seems to be the the Excel.Application.Quit command is not
working. Even when I get the thing to work by stepping through the
code, the code sticks on the "Exit Sub" line. If I go to the Windows
Task Manager I can see instances of Excel processes still ongoing.
The process seems like it should be simple and straigtforward but
obviously I am missing a big piece. Thanks for your help.