【发布时间】:2015-02-05 18:14:52
【问题描述】:
我创建了一个循环来查找某些 HTML 代码的每次迭代并将电子邮件数据作为字符串返回。我们正在寻找的是:
'Jibberish HTML Code
<p><b><font color="#000066" size="3" face="Arial">Delivery has failed to these recipients or groups:</font></b></p>
<font color="#000000" size="2" face="Tahoma"><p><a href="mailto:last.first@location.company.com">last.first@location.company.com</a><br>
'Jibberish HTML Code
<p><b><font color="#000066" size="3" face="Arial">Delivery has failed to these recipients or groups:</font></b></p>
<font color="#000000" size="2" face="Tahoma"><p><a href="mailto:last.first@location.company.com">last.first@location.company.com</a><br>
'Jibberish HTML Code
<p><b><font color="#000066" size="3" face="Arial">Delivery has failed to these recipients or groups:</font></b></p>
<font color="#000000" size="2" face="Tahoma"><p><a href="mailto:last.first@location.company.com">last.first@location.company.com</a><br>
此代码将找到第一个迭代,并且截至目前,循环在第一个找到的值上创建一个无限循环(不会移动到下一个找到的值:
Sub RevisedFindIt()
' Purpose: display the text between (but not including)
' the words "Title" and "Address" if they both appear.
Dim rng1 As Range
Dim rng2 As Range
Dim strTheText As String
Set rng1 = ActiveDocument.Range
With rng1.Find
.Execute FindText:="<font color=" & Chr(34) & "#000000" & Chr(34) & " size=" & Chr(34) & "2" & Chr(34) & " face=" & Chr(34) & "Tahoma" & Chr(34) & "><p><a href=" & Chr(34) & "mailto:", Forward:=True
Do While .Found
Set rng2 = ActiveDocument.Range(rng1.End, ActiveDocument.Range.End)
If rng2.Find.Execute(FindText:=Chr(34) & ">") Then
strTheText = ActiveDocument.Range(rng1.End, rng2.Start).Text
'Debug.Print strTheText
CreateObject("Excel.Application").Run "'TestExport.xlsm'!RunIt", strTheText
End If
Loop
End With
End Sub
正在将数据传递给 Excel 子:
Public Sub RunIt(strTheText As String)
Dim LastRow As Long
Debug.Print strTheText & "Test"
LastRow = ActiveWorkbook.ActiveSheet.Range("A" & ActiveWorkbook.ActiveSheet.Rows.Count).End(xlUp).Row + 1
ActiveWorkbook.ActiveSheet.Range("A" & LastRow).Value = strTheText
End Sub
如何让搜索跳到 Word VBA 中的下一次迭代?
【问题讨论】:
-
您需要提供更多信息。它应该如何表现?什么时候应该“跳到下一次迭代”?既然
rng1.Found只执行一次,那么“下一次迭代”是什么? -
@roryap 查看第一块“代码”,它更多是文档中的文本。查找应该跳转到匹配的每一行并返回电子邮件地址。它目前适用于文档中的第一个电子邮件地址,但不会继续前进。我不确定如何更改
rng1以排除已找到并继续搜索文档的其余部分。 -
嘿@Chrismas007:你为什么不做一个正则表达式来获取电子邮件?
-
@JLILIAman 主要是因为我以前从未使用过它们,这种方法应该稍作调整。