Your SelectedPPT is breaking encapsulation and can be set from outside the form, not just the form's code. You can mitigate this design issue by making the field Private and exposing a Property Get procedure for it:
Option Explicit
Private SelectedPPT As String
Public Property Get SelectedFile() As String
SelectedFile = SelectedPPT
End Property
Set the ComboBox row source in the form's Initialize or Activate handler, so it's initialized once. Then assign SelectedPPT when the selection changes in the ComboBox - that's your ComboBox1_Change handler.
That removes the need for that [Set PowerPoint] button and its Click handler.
I would have an [Ok] and a [Cancel] button, and I'd make the form remember whether it's cancelled:
Private IsCancelled As Boolean
Public Property Get Cancelled() As Boolean
Cancelled = IsCancelled
End Property
Private Sub OkButton_Click()
Me.Hide
End Sub
Private Sub CancelButton_Click()
IsCancelled = True
Me.Hide
End Sub
And then you also need to account for the case where the user clicks the red X button to close the form; you can do that by handling the forms' QueryClose event:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = VbQueryClose.vbFormControlMenu Then
IsCancelled = True
Cancel = True
Me.Hide
End If
End Sub
The rest of the logic belongs to the calling code - let's say the form is called MyAwesomeForm; you'd have something like this:
Dim filename As String
With New MyAwesomeForm
.Show
If Not .Cancelled Then
filename = .SelectedFile
'do whatever you wanted to do with that filename
End If
End With
Note:
- At no point the form calls
Unload Me
- It's the caller's responsibility to create and destroy the form object
- Caller uses a
New instance of the form every time
- Caller has read-only access to whatever values the form exposes through properties
function PassPPT(strSelectedPPT as string)......end functionusing likePassPPT(me.combobox1.value)