在普通宏中使用userform值

时间:2017-08-09 07:03:12

标签: vba ms-word word-vba

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

enter image description here

以下是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

1 个答案:

答案 0 :(得分:1)

在您的用户窗体中,使用:

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

您也可以使用更简单的If Me.Controls(ctl.Name).Value = True Then替换测试If ctl.value = True Then,因为您已经拥有对该控件的引用。