VB right click copy/paste in multipage

江枫思渺然 提交于 2019-12-06 02:26:12

Get ActiveControl name on a Multipage control

It's necessary to know the multipage's selected Page via a helper function (ActiveControlName) using SelectedItem property and getting the control (its name) from there. Change your button click events as follows:

Relevant button click events in class module clsBar

'Click event of the copy button
Private Sub cmdCopyButton_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
Dim sACN As String
sACN = ActiveControlName(fmUserform)    ' find control's name
       ' Debug.Print sACN & ".Copy"
fmUserform.Controls(sACN).Copy          ' << instead of fmUserform.ActiveControl.Copy
CancelDefault = True
End Sub

'Click event of the paste button
Private Sub cmdPasteButton_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
Dim sACN As String
sACN = ActiveControlName(fmUserform)
       ' Debug.Print sACN & ".Paste"
fmUserform.Controls(sACN).Paste    ' << instead of fmUserform.ActiveControl.Paste
CancelDefault = True
End Sub

Helper function called by above click events

Function ActiveControlName(form As UserForm) As String
'cf Site: https://stackoverflow.com/questions/47745663/get-activecontrol-inside-multipage
'Purpose: get ActiveControl
 Dim MyMultiPage As MSForms.MultiPage, myPage As MSForms.Page
 If form.ActiveControl Is Nothing Then
    ' do nothing
 ElseIf TypeName(form.ActiveControl) = "MultiPage" Then
    Set MyMultiPage = form.ActiveControl
    Set myPage = MyMultiPage.SelectedItem
    ActiveControlName = myPage.ActiveControl.Name
 Else
    ActiveControlName = form.ActiveControl.Name
 End If
 End Function

Side note

Suggest to check for the length of selected text strings in case of empty strings to prevent from unwanted results.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!