我在 MS Access 表单中使用 vba 将 Access 中的数据插入到 MS Word 内容控件中。该代码适用于文本字段、日期字段和复选框字段。但是我在将数据插入下拉字段时遇到了麻烦。我在 MS Word 中的下拉列表属性使用“显示名称”和值来存储数据(例如,显示名称=Adirondack 和值=1,显示名称=布法罗和值=2)。这是与尝试插入下拉列表时产生的错误消息一起使用的代码。您的帮助将不胜感激!
Private Sub Command179_Click()
' subroutine for exporting referral data from MS Access table to MS Word form content controls
Dim CC As ContentControl
Dim objLE As ContentControlListEntry
Dim fc As Field
Dim ccInfo As String
Dim Female As String
Dim appWord As Word.Application
Dim doc As Word.Document
Dim strDocName As String
Dim blnQuitWord As Boolean
On Error GoTo ErrorHandling
strPath = "C:\Users\AlbanyHiker\Documents\Custom Office Templates\INTAKE FORM\INTAKE BLANK FORM.docm"
strDocName = strPath
Set appWord = GetObject(, "Word.Application")
appWord.Visible = True
Set doc = appWord.Documents.Open(strDocName)
If IsNull(Me!ref_referral_dt) Then
MsgBox "REFERRAL DATE IS MISSING, COMPLETE FORM BEFORE EXPORT"
Me!ref_referral_dt = #1/1/9999#
End If
For Each CC In doc.ContentControls
ccInfo = "<> ID= " & CC.ID & " Title = " & CC.Title & " tag = " & CC.Tag & " Text = " & CC.Range.Text & vbCrLf
Debug.Print ccInfo
Select Case CC.Tag:
Case "frm_referral_dt"
CC.Range.Text = Me!ref_referral_dt
Case "frm_referral_number"
CC.Range.Text = Me!ref_referral_number
Case "frm_part_name_first"
CC.Range.Text = Me!ref_part_name_first
Case "frm_part_name_last"
CC.Range.Text = Me!ref_part_name_last
Case "frm_part_address1"
CC.Range.Text = Me!ref_part_address1
Case "frm_part_address2"
CC.Range.Text = Me!ref_part_address2
Case "frm_mailing_current"
If Me!ref_mailing_current = "-1" Then CC.Checked = True
Case "frm_part_city"
CC.Range.Text = Me!ref_part_city
Case "frm_part_zip"
CC.Range.Text = Me!ref_part_zip
Case "frm_part_telephone"
CC.Range.Text = Me!ref_part_telephone
' Next case statement throws the following error message
' 6124: you are not allowed to edit this selection because it is protected
Case "frm_part_region"
CC.Range.Text = Me!ref_part_region
End Select
Next
MsgBox "INTAKE Report Data Was Successfully Exported, Remember to Save the Word-Fillable File Using a Different Name"
Cleanup:
'do something here to cleanup stuff
Exit Sub
ErrorHandling:
Select Case Err.Number
Case -2147022986, 429
Set appWord = CreateObject("Word.Application")
blnQuitWord = True
Resume Next
Case -2147352571
MsgBox "There is a Type Mismatch Error indicating that a date may have been mistyped" _
& " No data imported. PLEASE CHECK DATA ENTRY ON ALL DATES ", vbOKOnly, _
" Please check the date entries on the form"
Case 5121, 5174
MsgBox "You must select a valid Word Document. " _
& " No data was imported.", vbOKOnly, _
" Document Not Found"
Case 5491
MsgBox "The document you selected does not" _
& " contain the required form fields." _
& " No data exported.", vbOKOnly, _
" Fields Not Found"
Case Else
MsgBox Err & ": " & Err.Description
End Select
GoTo Cleanup
ExitSubError:
Set rs = Nothing
'..and set it to nothing
MsgBox "Export failed, correct problems and export again"
Exit Sub
End Sub