|
这种情况在EXCEL中,一个VLookup函数就可以搞定了。
Word与Excel不同。如果使用内容控件,可以利用光标离开内容控件事件来进行,如下:
Option Explicit
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
If StrComp(ContentControl.Tag, "名字", vbTextCompare) = 0 Then
Me.ContentControls(2).Range.Text = GetWeightByName(ContentControl.Range.Text)
End If
End Sub
Private Function GetWeightByName(strFindName As String) As String
Dim tblSource As Word.Table
Dim oCell As Word.Cell
Dim strName As String
Set tblSource = Me.Tables(1)
For Each oCell In tblSource.Columns(1).Cells
strName = Replace$(oCell.Range.Text, Chr$(13) & Chr$(7), Empty)
If StrComp(strName, strFindName, vbTextCompare) = 0 Then
GetWeightByName = Replace$(oCell.Next.Range.Text, Chr$(13) & Chr$(7), Empty)
Exit For
End If
Next
If oCell Is Nothing Then GetWeightByName = "Unknow"
End Function
如果希望通过下拉列表框中项目的更改来取得体重信息时,可以考虑直接使用组合框控件(ActiveX控件,非内容控件),略。
见附件:
Name.rar
(27.56 KB, 下载次数: 60)
|
|