在Word中填充从属下拉列表时出错

时间:2018-07-30 22:22:34

标签: vba ms-word word-vba vbe

我使用了以下代码:

 <code><span>Sub PopulateddState()</span>
    <code><span> Select Case ActiveDocument.FormFields("ddRegion").Result</span>
    <code><span> Case "North"</span>
    <code><span> With ActiveDocument.FormFields     ("ddState").DropDown.ListEntries</span>
    <code><span> .Clear</span>
    <code><span> .Add "Michigan"</span>
    <code><span> .Add "Ohio"</span>
    <code><span> End With</span>
    <code><span> Case "South"</span>
    <code><span> With ActiveDocument.FormFields   ("ddState").DropDown.ListEntries</span>
    <code><span> .Clear</span>
    <code><span> .Add "Georgia"</span>
    <code><span> .Add "Texas"</span>
    <code><span> End With</span>
    <code><span> Case "East"</span>
    <code><span> With ActiveDocument.FormFields  ("ddState").DropDown.ListEntries</span>
    <code><span> .Clear</span>
    <code><span> .Add "New York"</span>
    <code><span> .Add "Maine"</span>
    <code><span> End With</span>
    <code><span> Case "West"</span>
    <code><span> With ActiveDocument.FormFields("ddState").DropDown.ListEntries</span>
    <code><span> .Clear</span>
    <code><span> .Add "California"</span>
    <code><span> .Add "Oregon"</span>
    <code><span> End With</span>
    <code><span> End Select</span>
    <code><span> End Sub</span>`

从此网站:https://www.techrepublic.com/blog/windows-and-office/how-to-populate-a-dependent-dropdown-list-in-word/

每次我尝试将其输入VBE时都说:

  

预期的行号或语句或语句结尾

我在做什么错?

1 个答案:

答案 0 :(得分:0)

您的代码对我有用,尽管我倾向于使用:

Sub PopulateStates()
Dim FmFldList As ListEntries
With ActiveDocument
  Set FmFldList = .FormFields("ddState").DropDown.ListEntries
  Select Case .FormFields("ddRegion").Result
    Case "North"
      With FmFldList
        .Clear
        .Add "Michigan"
        .Add "Ohio"
      End With
    Case "South"
      With FmFldList
        .Clear
        .Add "Georgia"
        .Add "Texas"
      End With
    Case "East"
      With FmFldList
        .Clear
        .Add "New York"
        .Add "Maine"
      End With
    Case "West"
      With FmFldList
        .Clear
        .Add "California"
        .Add "Oregon"
      End With
  End Select
End With
End Sub

PS:发布代码时,请使用工具栏上的{}符号而不是您自己的标记来选择代码。