Donfernando
Donfernando

Reputation: 5

Is it possible to create an 'input box' in VBA that can take a text selection with multiple lines as an input?

I am trying to create a macro that will filter out relevant information from some selected text (smaller than a page long). This information will then be used to fill out a MS-Word template.

I have been opening the selected texts via a .txt file however I feel it will improve workflow if it were possible to copy & paste the selected text into some form of an input box.

I have tried the following:

Dim text As String
text = InputBox("Enter selected text here:")

This however only accepts a string (a single line of text).

Thanks, Donfernanado

Upvotes: 0

Views: 6498

Answers (1)

gizlmo
gizlmo

Reputation: 1922

As Rich Holton pointed out, you can create your own InputBox that supports the desired functionality:

First create a UserForm which looks like an InputBox. Mine is called CustomInputBox.

Set the MultiLine Property of the TextBox to True.

Example:

MessageBox Example

Then add some logic for the buttons and a Function to Show your Inputbox which takes some parameters like Prompt and Title:

Option Explicit

Private inputValue As String
Private Cancel As Boolean

Private Sub btnCancel_Click()
    Cancel = True
    Me.Hide
End Sub

Private Sub btnOK_Click()
    If Me.txtInput.Value <> "" Then
        inputValue = Me.txtInput.Value
    End If
    Me.Hide
End Sub

'This is the Function you are going to use to open your InputBox
Public Function Display(Prompt As String, Optional Title As String = "", Optional Default As String = "") As String
Cancel = False

Me.lblPrompt.Caption = Prompt

If Title <> "" Then
    Me.Caption = Title
Else
    Me.Caption = "Microsoft Excel"
End If

If Default <> "" Then
    Me.txtInput.Value = Default
Else
    Me.txtInput.Value = ""
End If

Me.txtInput.SetFocus
Me.Show vbModal

If Not Cancel Then
    Display = inputValue
Else
    Display = ""
End If
End Function

Now you are able to use your InputBox like this:

Dim text As String
text = CustomInputBox.Display("Enter selected text here:")

Upvotes: 1

Related Questions