我是一名非常陌生的业余编码员,对C +有一点经验
我能够找到使用word中的宏来执行文档合并的代码,但是当我运行它时,我收到了这两个错误。
该行:
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