Vba 在普通宏中使用userform值

Vba 在普通宏中使用userform值,vba,ms-word,Vba,Ms Word,我有用户表单“CemeaFinallist”,其中有复选框和按钮。我想在'Normal.newmacros.minipro'中使用复选框Name的值作为变量=CNN 下面是userform按钮脚本 Private Sub Shift_Click() CemeaFinallist.Hide Dim ctl As Control Dim j As Long For Each ctl In Me.Controls If TypeOf ctl Is MSForms.CheckBox Then

我有用户表单“CemeaFinallist”,其中有复选框和按钮。我想在'Normal.newmacros.minipro'中使用复选框Name的值作为变量=CNN

下面是userform按钮脚本

Private Sub Shift_Click()

CemeaFinallist.Hide
Dim ctl As Control
Dim j As Long
For Each ctl In Me.Controls
If TypeOf ctl Is MSForms.CheckBox Then
    If Me.Controls(ctl.Name).Value = True Then

If ctl.Caption = "Select All" Then
Else

Application.Run MacroName:="Normal.NewMacros.minipro"

End If
End If
End If

Next
Application.ScreenUpdating = True
End Sub
以下是Normal.NewMacros宏

Sub MiniPRO()
Application.ScreenUpdating = False
Dim path As String
Dim CNN As String
Dim ex As String
Dim News As String
Dim SD As String


path = "C:\Documents and Settings\Administrator\Desktop\EMEA CEEMEA\EMEA FOR DAILY USE\"
CNN = ctl.Name 'at this stage Run Time Error '424' Object required'
ex = ".DOCX"

Documents.Open FileName:=path & CNN & ex
在用户表单中,使用:

Application.Run MacroName:="NewMacros.MiniPRO", varg1:=ctl.Name
在Normal.NewMacros模块中,使用:

Function MiniPRO(ByVal CtlName as String)
    Application.ScreenUpdating = False
    Dim path As String
    Dim CNN As String
    Dim ex As String
    Dim News As String
    Dim SD As String


    path = "C:\Documents and Settings\Administrator\Desktop\EMEA CEEMEA\EMEA FOR DAILY USE\"
    CNN = CtlName
    ex = ".DOCX"

    Documents.Open FileName:=path & CNN & ex
    '...
End Function

如果Me.Controlsctl.Name.Value=True,那么您也可以将测试替换为更简单的:If ctl.Value=True,那么,因为您已经有了对控件的引用。

代码的哪部分失败了?需要运行时错误“424”对象。在CNN=ctl.name处,您没有声明ctl,所以它的类型是Variant,与Dim ctl as Variant相同。您尚未为其指定任何值,但正试图通过检索Name属性将其用作对象。错误表示找不到任何对象。您需要将MiniPRO dim ctl中的ctl对象定义为对象集ctl=formsCemeaFinallist.checkbox。。。。不是确切的代码,而是沿着这些线的东西。无法从另一个子节点直接访问子节点的局部变量。您可以使用不推荐的Gobal变量,或者更好地使用宏函数替换宏子节点,并将参数传递给应用程序中的函数。运行call。@VincentG您明白我的意思了。确切地说,我需要使用复选框名称或值作为另一个子项的变量。请详细说明如何修复上述代码?编译错误:“Application.Run MacroName:=Normal.NewMacros.MiniPRO,ctl.Name”处的语法错误确定,可能是因为第一个参数是显式的,而第二个参数不是。试一下应用程序。运行MacroName:=Normal.NewMacros.MiniPRO,varg1:=ctl.name我已经添加了我在Userform中使用的全部代码。请注意,我在Word中没有使用太多宏,但是指定模板似乎不是很好,请参见。我将更正上面的代码。顺便说一句,你想要控件名还是控件标题?我不认为你可以像沙特阿拉伯或科特迪瓦那样用空格命名控件