是否可以在 VBA 中创建一个可以将多行文本选择作为输入的 'input box'?
Is it possible to create an 'input box' in VBA that can take a text selection with multiple lines as an input?
我正在尝试创建一个宏,用于从某些选定文本(小于一页)中过滤掉相关信息。此信息随后将用于填写 MS-Word 模板。
我一直通过 .txt 文件打开选定的文本,但我觉得如果可以将选定的文本复制并粘贴到某种形式的输入框中,将会改进工作流程。
我试过以下方法:
Dim text As String
text = InputBox("Enter selected text here:")
然而,这只接受一个字符串(一行文本)。
谢谢,唐费尔纳多
正如 Rich Holton 指出的那样,您可以创建自己的支持所需功能的 InputBox:
首先创建一个看起来像输入框的用户窗体。我的名字叫 CustomInputBox
.
将 TextBox 的 MultiLine 属性 设置为 True。
示例:
然后为按钮添加一些逻辑和一个函数来显示您的输入框,该函数接受一些参数,如提示和标题:
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
现在您可以像这样使用您的输入框了:
Dim text As String
text = CustomInputBox.Display("Enter selected text here:")
我正在尝试创建一个宏,用于从某些选定文本(小于一页)中过滤掉相关信息。此信息随后将用于填写 MS-Word 模板。
我一直通过 .txt 文件打开选定的文本,但我觉得如果可以将选定的文本复制并粘贴到某种形式的输入框中,将会改进工作流程。
我试过以下方法:
Dim text As String
text = InputBox("Enter selected text here:")
然而,这只接受一个字符串(一行文本)。
谢谢,唐费尔纳多
正如 Rich Holton 指出的那样,您可以创建自己的支持所需功能的 InputBox:
首先创建一个看起来像输入框的用户窗体。我的名字叫 CustomInputBox
.
将 TextBox 的 MultiLine 属性 设置为 True。
示例:
然后为按钮添加一些逻辑和一个函数来显示您的输入框,该函数接受一些参数,如提示和标题:
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
现在您可以像这样使用您的输入框了:
Dim text As String
text = CustomInputBox.Display("Enter selected text here:")