You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 Caption to something like "Multi-Line Input" (this is the window title). Leave the Width as a starting value (e.g., 400) — users will be able to drag-resize it later.
    • Add a TextBox control:
      • Set MultiLine = True (allows line breaks)
      • Set ScrollBars = 2 - fmScrollBarsVertical (adds a vertical scrollbar for longer input)
      • Set Height to ~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 Width to ~360 (leaves margin around the text box)
    • Add two CommandButton controls:
      • Name one cmdOK and set its Caption to "OK"
      • Name the other cmdCancel and set its Caption to "Cancel"
    • Position the buttons at the bottom right of the form for a familiar layout.
Step 2: Add VBA Code to Handle Input & Buttons

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 OpenMultiLineInput macro 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 userInput string 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 16:52:36