EmEditor多行输入窗口:能否通过宏实现可调宽10行输入并读取为字符串变量?
Absolutely! You can totally build this custom multi-line input window using VBA macros—it’s straightforward and fits all your requirements. Let’s walk through how to do it step by step:
Step 1: Create the Custom UserForm
First, we’ll build the visual interface with your required elements:
- Open the VBA Editor (
Alt + F11), right-click your project in the Project Explorer, select Insert > UserForm. - Customize the form and controls:
- For the UserForm itself: Set its
Captionto something like "Multi-Line Input" (this is the window title). Leave theWidthas a starting value (e.g., 400) — users will be able to drag-resize it later. - Add a
TextBoxcontrol:- Set
MultiLine = True(allows line breaks) - Set
ScrollBars = 2 - fmScrollBarsVertical(adds a vertical scrollbar for longer input) - Set
Heightto ~150 (fits roughly 10 lines with a standard 10-point font) - Set
EnterKeyBehavior = True(makes Enter insert a line break instead of triggering the OK button) - Set
Widthto ~360 (leaves margin around the text box)
- Set
- Add two
CommandButtoncontrols:- Name one
cmdOKand set itsCaptionto "OK" - Name the other
cmdCanceland set itsCaptionto "Cancel"
- Name one
- Position the buttons at the bottom right of the form for a familiar layout.
- For the UserForm itself: Set its
Double-click the UserForm to open its code module, then paste this code:
' Public variable to store the input text (accessible outside the form) Public InputText As String Private Sub cmdCancel_Click() ' Clear input and close the form if user cancels InputText = "" Me.Hide End Sub Private Sub cmdOK_Click() ' Save the textbox content to our public variable InputText = Me.TextBox1.Text Me.Hide End Sub Private Sub UserForm_Initialize() ' Auto-focus the textbox when the form opens Me.TextBox1.SetFocus End Sub ' Make the textbox resize with the window Private Sub UserForm_Resize() ' Adjust textbox width to fit the resized form (leave 20px margin on each side) Me.TextBox1.Width = Me.Width - 40 ' Keep buttons aligned to the bottom right Me.cmdOK.Left = Me.Width - 120 Me.cmdCancel.Left = Me.Width - 60 End Sub
Optional: Limit Input to Exactly 10 Lines
If you want to restrict users to 10 lines max, add this extra event handler to the UserForm code:
Private Sub TextBox1_Change() Dim lineCount As Integer lineCount = UBound(Split(Me.TextBox1.Text, vbCrLf)) + 1 If lineCount > 10 Then ' Trim the input to the first 10 lines Dim lines() As String lines = Split(Me.TextBox1.Text, vbCrLf) Me.TextBox1.Text = Join(Left(lines, 10), vbCrLf) ' Move the cursor to the end of the text Me.TextBox1.SelStart = Len(Me.TextBox1.Text) MsgBox "Maximum 10 lines allowed.", vbExclamation End If End Sub
Step 3: Call the Form from Your Macro
Create a standard module (Insert > Module) and add this code to launch the input window:
Sub OpenMultiLineInput() Dim userInput As String ' Show the custom input form UserForm1.Show ' Check if the user clicked OK (InputText won't be empty) If UserForm1.InputText <> "" Then userInput = UserForm1.InputText ' Do whatever you need with the input here—example: display it MsgBox "You entered:" & vbCrLf & userInput, vbInformation Else MsgBox "No input was entered.", vbExclamation End If ' Clean up the form instance Unload UserForm1 End Sub
How It Works
- Run the
OpenMultiLineInputmacro to launch the window. - Users can type up to 10 lines (or more if you skip the optional line limit), resize the window width, and click OK/Cancel.
- The input is stored in the
userInputstring variable in the calling macro—you can use this variable for further processing (writing to a cell, saving to a file, etc.).
内容的提问来源于stack exchange,提问作者Koteswara Rao
相关产品推荐
相关产品推荐

