【问题标题】:Is it possible to create an 'input box' in VBA that can take a text selection with multiple lines as an input?是否可以在 VBA 中创建一个“输入框”,可以将多行文本选择作为输入?
【发布时间】:2017-09-06 19:16:12
【问题描述】:

我正在尝试创建一个宏,该宏将从某些选定文本(小于一页长)中过滤掉相关信息。然后,此信息将用于填写 MS-Word 模板。

我一直通过 .txt 文件打开选定的文本,但我认为如果可以将选定的文本复制并粘贴到某种形式的输入框中,它将改进工作流程。

我尝试了以下方法:

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

然而,这只接受一个字符串(单行文本)。

谢谢,唐费尔南多

【问题讨论】:

  • 您需要创建一个用户表单。

标签: vba ms-word inputbox


【解决方案1】:

正如 Rich Holton 所指出的,您可以创建自己的 InputBox 来支持所需的功能:

首先创建一个看起来像 InputBox 的 UserForm。我的叫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

现在您可以像这样使用您的 InputBox:

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

【讨论】:

  • CustomInputBox 将在完整示例中定义
猜你喜欢
  • 2016-12-28
  • 1970-01-01
  • 2012-01-14
  • 1970-01-01
  • 2021-08-15
  • 2015-03-27
  • 1970-01-01
  • 1970-01-01
  • 2015-10-09
相关资源
最近更新 更多