Excel 从电子邮件正文中提取URL
我有数据要添加到Excel中。我发现我们可以通过Outlook VBA实现这一点。内容采用以下格式: 职称:本科生 性别:男 国家:阿尔巴尼亚 关键词:Excel 从电子邮件正文中提取URL,excel,vba,outlook,Excel,Vba,Outlook,我有数据要添加到Excel中。我发现我们可以通过Outlook VBA实现这一点。内容采用以下格式: 职称:本科生 性别:男 国家:阿尔巴尼亚 关键词: 1.环境 2.人口 名字:约翰 电话号码:0532444 用户名:test@dda.com 文件上载: 我根据一篇旧文章创建了以下代码: 子CopyToExcel() 将xlApp作为对象 作为对象的Dim xlWB 将图纸作为对象 将我设置为Outlook.MailItem 变暗vText作为变量 作为字符串的Dim sText 作为变体的
1.环境
2.人口 名字:约翰 电话号码:0532444 用户名:test@dda.com 文件上载:
我根据一篇旧文章创建了以下代码:
子CopyToExcel()
将xlApp作为对象
作为对象的Dim xlWB
将图纸作为对象
将我设置为Outlook.MailItem
变暗vText作为变量
作为字符串的Dim sText
作为变体的暗维它
我想我会坚持多久
暗计数等于长
Dim bx以布尔形式开始
Const strPath As String=“E:\Project\Test oulook.xlsx”'工作簿的路径
如果Application.ActiveExplorer.Selection.Count=0,则
MsgBox“未选择任何项目!”,vbCritical,“错误”
出口接头
如果结束
出错时继续下一步
Set xlApp=GetObject(,“Excel.Application”)
如果错误为0,则
Application.StatusBar=“正在打开Excel源,请稍候…”
设置xlApp=CreateObject(“Excel.Application”)
bXStarted=True
如果结束
错误转到0
'打开工作簿以输入数据
设置xlWB=xlApp.Workbooks.Open(strPath)
设置xlSheet=xlWB.Sheets(“Sheet1”)
'处理每个选定记录
rCount=xlSheet.UsedRange.Rows.Count
对于Application.ActiveExplorer.Selection中的每个olItem
sText=分子筛体
vText=拆分(sText,Chr(13))
'查找工作表的下一个空行
rCount=rCount+1
'检查邮件正文中的每行文本
对于i=UBound(vText)到0,步骤-1
如果InStr(1,vText(i),“title:”)>0,则
vItem=拆分(vText(i),Chr(58))
xlSheet.范围(“A”&R计数)=微调(vItem(1))
如果结束
如果InStr(1,vText(i),“性别:”)>0,则
vItem=拆分(vText(i),Chr(58))
xlSheet.范围(“B”和rCount)=微调(vItem(1))
如果结束
如果指令(1,vText(i),“国家:”)>0,则
vItem=拆分(vText(i),Chr(58))
xlSheet.范围(“C”&rCount)=微调(vItem(1))
如果结束
如果指令(1,vText(i),“关键字:”)>0,则
vItem=拆分(vText(i),Chr(58))
xlSheet.范围(“E”&rCount)=微调(vItem(1))
如果结束
如果指令(1,vText(i),“名字:”)>0,则
vItem=拆分(vText(i),Chr(58))
xlSheet.范围(“G”&rCount)=微调(vItem(1))
如果结束
如果指令(1,vText(i),“电话号码:”)>0,则
vItem=拆分(vText(i),Chr(58))
xlSheet.范围(“I”&rCount)=微调(vItem(1))
如果结束
如果指令(1,vText(i),“用户名:”)>0,则
vItem=拆分(vText(i),Chr(58))
xlSheet.范围(“F”&rCount)=微调(vItem(1))
如果结束
如果InStr(1,vText(i),“upload:”)>0,则
vItem=拆分(vText(i),Chr(58))
xlSheet.范围(“O”&rCount)=微调(vItem(1))
如果结束
接下来我
xlWB.Save
下一代
xlWB.Close SaveChanges:=True
如果BX启动,那么
xlApp.退出
如果结束
设置m=无
Set xlSheet=无
设置xlWB=Nothing
设置xlApp=Nothing
端接头
上载字段显示“http”not.Chr(58)为冒号
通过执行拆分(vText(i),Chr(58))
,您将获取原始字符串并用分隔符冒号拆分它
例如:文件上传:
vItem(0)=文件上载
vItem(1)=http
vItem(2)=//all-free-download.com/free-photos/download/autumns-night-sun_513398.html
因此,为了获得您想要的完整链接,您必须连接vItem
例如:“&vItem(2)Chr(58)是冒号
通过执行拆分(vText(i),Chr(58))
,您将获取原始字符串并用分隔符冒号拆分它
例如:文件上传:
vItem(0)=文件上载
vItem(1)=http
vItem(2)=//all-free-download.com/free-photos/download/autumns-night-sun_513398.html
因此,为了获得您想要的完整链接,您必须连接vItem
例如,vItem(1)和:“&vItem(2)
如果InStr(1,vText(i),“上传:”)>0则
vItem=Split(vText(i),Chr(58),2)如果InStr(1,vText(i),“upload:”)>0则
vItem=Split(vText(i),Chr(58),2)“我试过你的代码。当工作表为空时,查找下一个可用单元格时出现问题(公式xlSheet.UsedRange.Rows.Count对这两个单元格都返回1,未使用任何行,使用了一行)
这里是一个重写,似乎工作正常
if-then例程已被case语句替换
Sub CopyToExcel()
Dim xlApp As Object
Dim xlWB As Object
Dim xlSheet As Object
Dim olItem As Outlook.mailItem
Dim vText As Variant
Dim rCount As Long
Dim vItem As Variant
Dim i As Long
Dim bXStarted As Boolean
Const strPath As String = "E:\Project\Test outlook.xlsx" ' the path of the workbook
If Application.ActiveExplorer.Selection.Count = 0 Then
MsgBox "No Items selected!", vbCritical, "Error"
Exit Sub
End If
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err <> 0 Then
Application.StatusBar = "Please wait while Excel source is opened ... "
Set xlApp = CreateObject("Excel.Application")
bXStarted = True
End If
' xlApp.Visible = True ' show worksheet (for debugging)
On Error GoTo 0
Set xlWB = xlApp.Workbooks.Open(strPath) ' Open the workbook to input the data
Set xlSheet = xlWB.Sheets("Sheet1")
' rCount = xlSheet.UsedRange.Rows.Count ' does not work (returns 1 when no data on worksheet)
Dim formula As String '
formula = "MATCH(TRUE, INDEX(ISBLANK(A:A), 0, 0), 0)" ' cell formula: =MATCH(TRUE, INDEX(ISBLANK(A:A), 0, 0), 0)
rCount = xlApp.Evaluate(formula) ' find next empty line on worksheet using a cell formula
For Each olItem In Application.ActiveExplorer.Selection ' Process each selected email
vText = Split(olItem.body, vbCrLf) ' convert email body to an array of text lines
For i = 0 To UBound(vText) ' Check each line of text in the message body
vItem = Split(":" & vText(i), ":", 3) ' split line into max 3 parts (leading ":" added to prevent fail on blank lines)
Select Case LCase(vItem(1)) ' LCase for case insensitive comparison
Case "title"
xlSheet.Range("A" & rCount) = Trim(vItem(2))
Case "gender"
xlSheet.Range("B" & rCount) = Trim(vItem(2))
Case "country"
xlSheet.Range("C" & rCount) = Trim(vItem(2))
Case "keyword"
xlSheet.Range("E" & rCount) = Trim(vItem(2))
Case "first name"
xlSheet.Range("G" & rCount) = Trim(vItem(2))
Case "phone number"
xlSheet.Range("I" & rCount) = Trim(vItem(2))
Case "username"
xlSheet.Range("F" & rCount) = Trim(vItem(2))
Case "file upload"
xlSheet.Range("O" & rCount) = Trim(vItem(2))
' Case Else
' do something else here
End Select
Next i
xlWB.Save
rCount = rCount + 1 ' point to next empty line of the worksheet
Next olItem
Set olItem = Nothing
xlWB.Close SaveChanges:=True
If bXStarted Then
xlApp.Quit
End If
Set xlSheet = Nothing
Set xlWB = Nothing
Set xlApp = Nothing
End Sub
子CopyToExcel()
将xlApp作为对象
作为对象的Dim xlWB
将图纸作为对象
将我设置为Outlook.mailItem
变暗vText作为变量
暗计数等于长
作为变体的暗维它
我想我会坚持多久
Dim bx以布尔形式开始
Const strPath As String=“E:\Project\Test outlook.xlsx”'工作簿的路径
如果Application.ActiveExplorer.Selection.Count=0,则
MsgBox“未选择任何项目!”,vbCritical,“错误”
出口接头
如果结束
出错时继续下一步
Set xlApp=GetObject(,“Excel.Application”)
如果错误为0,则
Application.StatusBar=“正在打开Excel源,请稍候…”
设置xlApp=CreateObject(“Excel.Application”)
bXStarted=True
如果结束
“xlApp.Visible=True”显示工作表(用于调试)
错误转到0
设置xlWB=xlApp.Workbooks.Open(strPath)'打开工作簿以输入数据
设置xlSheet=xlWB.Sheets(“Sheet1”)
“rC
Sub CopyToExcel()
Dim xlApp As Object
Dim xlWB As Object
Dim xlSheet As Object
Dim olItem As Outlook.mailItem
Dim vText As Variant
Dim rCount As Long
Dim vItem As Variant
Dim i As Long
Dim bXStarted As Boolean
Const strPath As String = "E:\Project\Test outlook.xlsx" ' the path of the workbook
If Application.ActiveExplorer.Selection.Count = 0 Then
MsgBox "No Items selected!", vbCritical, "Error"
Exit Sub
End If
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err <> 0 Then
Application.StatusBar = "Please wait while Excel source is opened ... "
Set xlApp = CreateObject("Excel.Application")
bXStarted = True
End If
' xlApp.Visible = True ' show worksheet (for debugging)
On Error GoTo 0
Set xlWB = xlApp.Workbooks.Open(strPath) ' Open the workbook to input the data
Set xlSheet = xlWB.Sheets("Sheet1")
' rCount = xlSheet.UsedRange.Rows.Count ' does not work (returns 1 when no data on worksheet)
Dim formula As String '
formula = "MATCH(TRUE, INDEX(ISBLANK(A:A), 0, 0), 0)" ' cell formula: =MATCH(TRUE, INDEX(ISBLANK(A:A), 0, 0), 0)
rCount = xlApp.Evaluate(formula) ' find next empty line on worksheet using a cell formula
For Each olItem In Application.ActiveExplorer.Selection ' Process each selected email
vText = Split(olItem.body, vbCrLf) ' convert email body to an array of text lines
For i = 0 To UBound(vText) ' Check each line of text in the message body
vItem = Split(":" & vText(i), ":", 3) ' split line into max 3 parts (leading ":" added to prevent fail on blank lines)
Select Case LCase(vItem(1)) ' LCase for case insensitive comparison
Case "title"
xlSheet.Range("A" & rCount) = Trim(vItem(2))
Case "gender"
xlSheet.Range("B" & rCount) = Trim(vItem(2))
Case "country"
xlSheet.Range("C" & rCount) = Trim(vItem(2))
Case "keyword"
xlSheet.Range("E" & rCount) = Trim(vItem(2))
Case "first name"
xlSheet.Range("G" & rCount) = Trim(vItem(2))
Case "phone number"
xlSheet.Range("I" & rCount) = Trim(vItem(2))
Case "username"
xlSheet.Range("F" & rCount) = Trim(vItem(2))
Case "file upload"
xlSheet.Range("O" & rCount) = Trim(vItem(2))
' Case Else
' do something else here
End Select
Next i
xlWB.Save
rCount = rCount + 1 ' point to next empty line of the worksheet
Next olItem
Set olItem = Nothing
xlWB.Close SaveChanges:=True
If bXStarted Then
xlApp.Quit
End If
Set xlSheet = Nothing
Set xlWB = Nothing
Set xlApp = Nothing
End Sub