Я начинающий программист, плохо знакомый с VBA, с небольшим опытом работы с C +
. Мне удалось найти код для выполнения слияния документов, используя макросы в слове, но когда я его запускаю, я получаю эти 2 ошибки.
Строка:
MsgBox Err.Number & ": " & Err.Description, _
vbOKOnly, "Error"
возвращает в диалоговом окне «Ошибка» следующую ошибку:
5941: Запрошенный элемент коллекции несуществовать.
Строка:
cnn.Close
возвращает следующую ошибку в диалоговом окне Microsoft Visual Basic:
Ошибка времени выполнения '91': переменная объекта или Спеременная блока не установлена
Вот код, используемый
Буду признателен за любую помощь, которую может предоставить
Sub TransferToExcel()
'Transfer a single record from the form fields to an Excel workbook.
Dim doc As Document
Dim strObligee As String
Dim strJob As String
Dim strSQL As String
Dim cnn As ADODB.Connection
'Get data.
Set doc = ActiveDocument
On Error GoTo ErrHandler
strObligee = Chr(39) & doc.FormFields("txtObligee").Result & Chr(39)
strJob = Chr(39) & doc.FormFields("txtJob").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$]" _
& " (Obligee, Job)" _
& " VALUES (" _
& strObligee & ", " _
& strJob _
& ")"
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=C:\Users\Ntrojan\Documents\word to excel project\fieldheaders.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