Word 2013 VBA 从 Field 调用时出现类型不匹配错误

Word 2013 VBA Type mismatch error when calling from Field

我已经有一段时间没有编码了,而且它不是 Visual Basic,所以如果我的问题很简单或解释得不好,请原谅我。

我有一个 Sub CustomSave(),它验证没有空白的 ContentControls 需要填写,然后另存为标题为 "User Name" [=32 的 (.docm) Word 文档=] "Requested Action" 然后删除{保存}(见下文)和文档中的其他 1 个字段以防止无意编辑。

当我从 VBA 运行 时,Sub 运行完美。

从字段调用 Sub(不传递任何内容):
{MacroButton CustomSave Save}

当我激活上述字段时,出现此错误:
(标题)Microsoft Visual Basic for Applications [X]
(内容)类型不匹配
(选项)[确定][帮助]

帮助只是将我带到我找不到答案的通用 Microsoft 联机帮助。

我正在重新输入代码,所以请忽略缺少保存目录,如果我在这里出错,请不要假设实际文档中存在拼写错误:

Sub CustomSave()

' Deselect the Save Field   
Selection.Collapse

' Set checks for required data
Dim tRequest As Boolean
Dim tUser As Boolean
Dim tOffice As Boolean
Dim tCard As Boolean
tRequest = ActiveDocument.SelectContentControlsByTitle("Request").Item(1).ShowingPlaceholderText
tUser = ActiveDocument.SelectContentControlsByTitle("User").Item(1).ShowingPlaceholde    tText
tOffice = ActiveDocument.SelectContentControlsByTitle("Office").Item(1).ShowingPlaceholderText
tCard = ActiveDocument.SelectContentControlsByTitle("Card").Item(1).ShowingPlaceholderText

' Set file name variables
Dim cardHolder As String
Dim cardAction As String
cardHolder = ActiveDocument.SelectContentControlsByTitle("User")(1).Range.Text
cardAction = ActiveDocument.SelectContentControlsByTitle("Request")(1).Range.Text

' Check all and Error out if missing required data
If tRequest Then
    Msg = "Action Requested is required."
    MsgBox = Msg,,"Error"
    Exit Sub

ElseIf tUser Then
    Msg = "Username is required."
    MsgBox = Msg,,"Error"
    Exit Sub

ElseIf ActiveDocument.SelectContentControlsByTitle("Request")(1).Range.Text = "New" Then
    ' Embedded If to check fields only required for new requests

    If tCard Then
        Msg = "Card type is required."
        MsgBox = Msg,,"Error"
        Exit Sub

    ElseIf tOffice Then
        Msg = "Office location is required."
        MsgBox = Msg,,"Error"
        Exit Sub

    Else
        GoTo Save

    End If

'If all checks are satisfied, save to the designated location
Else
Save:
    ' Remove the Save and Clear Fields but not the first Field
    Dim rng As Range

    For Each rng In ActiveDocument.StoryRanges
        With rng.Fields
            While .Count > 1
                .Item(2).Delete
            Wend
        End With
    Next

    ' Finally, perform the save
    ActiveDocument.SaveAs2 ("Dir" & cardHolder & " " & Format(Now(), "yyyy-mm-dd") & " " & cardAction & ".docm")

End If
End Sub

我发现错误是我将代码放在两个地方——在 ThisDocument 对象和宏模块下。我删除了 ThisDocument 版本并且脚本运行。