Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

VBA question, create custom index or array of Userform controls

$
0
0

I'm making an Excel Userform with 7 TextBoxes and a ComboBox where text is entered then a button copies the text to the clipboard and opens a new Outlook email with the text inserted.  However I'm not sure how to set the order of Controls where the text is copied.  Using the web I managed to get the following code but get an error "Subscript out of Range, runtime error 9" on sText = sText & MyCtrl(ctrl).Text & vbCrLf

Please note with my apologies I'm a complete VBA novice!  One other thing, the text in txt1 (TextBox1) would be awesome as a hyperlink.  I looked around for doing that but could not find any code that was close.  -Thanks in advance!

Private Sub CommandButton4_Click()
Dim OutlookApp As Object
Dim mItem As Object
Dim GetClip As New DataObject
Dim ctrl As Variant, sText As String
Dim MyCtrl(0 To 7) As Control
    Set MyCtrl(0) = txt1
    Set MyCtrl(1) = txt2
    Set MyCtrl(2) = txt3
    Set MyCtrl(3) = txt4
    Set MyCtrl(4) = txt5
    Set MyCtrl(5) = txt6
    Set MyCtrl(6) = txt7
    Set MyCtrl(7) = txt8

Set OutlookApp = CreateObject("Outlook.Application")
Set mItem = OutlookApp.CreateItem(0)
    sText = ""
    For Each ctrl In MyCtrl
            sText = sText & MyCtrl(ctrl).Text & vbCrLf
        Next ctrl
    With GetClip
        .SetText sText
        .PutInClipboard
    End With
    With mItem
        .To = "Some email here"
        .Subject = "Test Subject!"
        .Body = "Your text here!" & vbCrLf & (sText)
        .Display
        End With
Set GetClip = Nothing
Set ctrl = Nothing
End Sub


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>