我正在使用Office365版本的Word。我有一些VBA用户表单,我想创建一个功能或程序来浏览。目前,有40个名为Form01,Form02,Form03等的表单对象,用户必须按顺序浏览它们。我已在每个代码中放置代码以向前和向后导航。
Private Sub btnContinue_Click()
Form02.Hide
Form03.Show
End Sub
Private Sub btnBack_Click()
Form02.Hide
Form01.Show
End Sub
但是,如果我需要更改我的表单名称,那么它将会修复每个按钮引用。我还试图在我的代码中嵌入一个页面引用,然后使用一个函数来计算下一个或前一个页面,但我无法找到一种方法将其从字符串值转回到对象引用。我可能会缺少什么?
答案 0 :(得分:1)
你可以这样做: 在共享的通用模块中:
Public Sub showNextForm(currentFormName As String)
nextFormName = "Form" & Format((CInt(Right(currentFormName, 2)) + 1), "00")
For Each uForm In VBA.UserForms 'Loop through all loaded userforms
If uForm.Name = currentFormName Then uForm.Hide
If uForm.Name = nextFormName Then
uForm.Show
nextFormLoaded = True
End If
Next uForm
If Not nextFormLoaded Then
Set nextForm = UserForms.Add(nextFormName) 'Load next userform if it's not already loaded
nextForm.Show
End If
End Sub
Public Sub showPreviousForm(currentFormName As String)
prevFormName = "Form" & Format((CInt(Right(currentFormName, 2)) - 1), "00")
For Each uForm In VBA.UserForms 'Loop through all loaded userforms, and hide current one
If uForm.Name = currentFormName Then uForm.Hide
If uForm.Name = prevFormName Then
uForm.Show
prevFormLoaded = True
End If
Next uForm
If Not prevFormLoaded Then
Set prevForm = UserForms.Add(prevFormName) 'Load previous userform if it's not already loaded
prevForm.Show
End If
End Sub
然后,在每个用户表单中,您将进行“后退”按钮调用
showPreviousForm Me.Name
然后按“下一步”按钮
showNextForm Me.Name