【发布时间】:2016-02-15 21:21:22
【问题描述】:
我有一个宏,我想用它来允许用户从电子邮件中粘贴文本,并自动识别和组织信息以填写表格。
我的问题是要简化“粘贴”过程。
我的想法是插入一个 InputBox 或一个用户表单,用户可以在其中粘贴整个电子邮件文本。虽然没有达到我的预期。
通常当您在 Range("A2") 中使用 CTRL+V(比方说)时,文本会像在电子邮件中一样逐行拆分。
是否可以做同样的事情,但有一个框提示?还是它只允许插入少量数据并且只在 1 行中?
我的代码 1)
EmailText = InputBox("Please insert Email Text Below")
wsRep.Range("A2").Value = EmailText
'它只复制第一行
提示用户表单的相同问题 - NameTextBox
请问有没有其他方法可以解决?
(我想避免用户不得不在工作表之间切换或做任何事情,除了粘贴)
非常感谢。
解决方案:
Dim oDO As DataObject
Dim tmpArr As Variant
Dim Cell As Range
Set oDO = New DataObject
'First we get the information from the clipboard
If MsgBox("Please copy the text from the email and then press OK", vbOKCancel) = vbOK Then
oDO.GetFromClipboard
'Here we send the ClipBoard text to a new string which will contain all the Information (all in 1 line)
sTxt = oDO.GetText
wsRep.Range("A2") = sTxt 'Range is up to you
'Now we can split the email information using the "line break" and this code (found it [here][1])
Application.Goto Reference:=wsRep.Range("A1") 'I need to move to the worksheet to run this code
'This code split each line using the criteria "break line" in rows
For Each Cell In wsRep.Range("A2", Range("A2").End(xlDown))
If InStr(1, Cell, Chr(10)) <> 0 Then
tmpArr = Split(Cell, Chr(10))
Cell.EntireRow.Copy
Cell.Offset(1, 0).Resize(UBound(tmpArr), 1). _
EntireRow.Insert xlShiftDown
Cell.Resize(UBound(tmpArr) + 1, 1) = Application.Transpose(tmpArr)
End If
Next
Application.CutCopyMode = False
End If
【问题讨论】:
-
对于用户窗体,您是否设置了文本框的
Multiline属性?默认为 false,您应该将其设置为 true。