在if / then

时间:2016-10-20 13:53:04

标签: vba word-vba

我在userform上有一个ToggleButton(TB)和ComboBox(CB),如果TB.value = True,那么它必须确定插入正确文件的CB.value是什么。

我正在使用if / then语句来解决TB.value(我已经将其用于其他用户窗体上的切换,并且它本身也有效。为了解决CB.value,我试图使用选择案例陈述。

当我在if / then语句中使用select case语句运行脚本时,我没有得到要插入的文件。

以下是我的代码示例,包括我如何列出CB来初始化:

Private Sub CommandButton1_Click()

CreateObject (Word.Application.Documents.Add)

If ToggleButton1.Value = True Then
Select Case File
    Case ComboBox1.Value = "File A"
        Selection.InsertFile FileName:="C:\File A"
    Case ComboBox1.Value = "File B"
        Selection.InsertFile FileName:="C:\File B"
    End Select
Else:
    ToggleButton1.Value = False
End If

Unload Me

End Sub



Private Sub Userform_Initialize()

With ComboBox1
    .AddItem "File A", 0
    .AddItem "File B", 1
    End With

End Sub

如果有人能够帮我弄清楚为什么我无法使用上述方法插入文件,那将不胜感激。如果有更好的方法可以做到这一点,我也很乐意听到!

提前谢谢。

1 个答案:

答案 0 :(得分:5)

您的选择案例有误:

Select Case ComboBox1.Value
    Case "File A"
        Selection.InsertFile FileName:="C:\File A"
    Case  "File B"
        Selection.InsertFile FileName:="C:\File B"
    End Select

您在“选择案例”行中列出了您正在测试的内容以及案例行中可能的结果。