20170906xlVBA_GetEMailFromDocument
Public Sub GetDataFromWord()
AppSettings
'On Error GoTo ErrHandler
Dim StartTime, UsedTime As Variant
StartTime = VBA.Timer
'Input code here Dim Wb As Workbook
Dim Sht As Worksheet
Dim oSht As Worksheet
Dim Rng As Range
Dim Arr As Variant Dim Dic As Object
Set Dic = CreateObject("Scripting.Dictionary") 'Dim wdApp As Word.Application
'Dim wdDoc As Word.Document
Dim wdApp As Object
Dim wdDoc As Object 'Const SHEET_NAME As String = "提取信息"
Set Wb = Application.ThisWorkbook
Set Sht = Wb.Worksheets(1) On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If wdApp Is Nothing Then
Set wdApp = CreateObject("Word.Application")
End If
On Error GoTo 0 'Set wdApp = New Word.Application Filename = Dir(Wb.Path & "\*.doc*")
Do While Filename <> ""
Debug.Print Filename
FilePath = Wb.Path & "\" & Filename
Set wdDoc = wdApp.Documents.Open(FilePath)
Text = wdDoc.Content.Text If RegTest(Text, "(\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)") Then
Arr = RegGetArray("(\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)", Text)
For i = LBound(Arr) To UBound(Arr)
Key = CStr(Arr(i))
Debug.Print Key
If Not Dic.Exists(Key) Then
Dic(Key) = Dic.Count + 1
End If
Next i End If Filename = Dir
Loop Application.StatusBar = ">>>>>>>>Regexpress Getting array >>>>>>>>"
wdDoc.Close False '关闭doc
wdApp.Quit '退出app
Set wdApp = Nothing
Set wdDoc = Nothing With Sht
.Cells.ClearContents
.Range("A1:B1").Value = Array("序号", "邮箱")
Set Rng = .Range("A2")
Set Rng = Rng.Resize(Dic.Count, 2)
Rng.Value = Application.WorksheetFunction.Transpose(Array(Dic.Items, Dic.keys))
End With
UsedTime = VBA.Timer - StartTime
Debug.Print "UsedTime:" & Format(UsedTime, "0.000 Seconds")
'MsgBox "UsedTime:" & Format(UsedTime, "0.000 Seconds"), vbOKOnly, "QQ "
ErrorExit:
Set Wb = Nothing
Set Sht = Nothing
Set Rng = Nothing Set Dic = Nothing AppSettings False On Error Resume Next
wdApp.Quit Exit Sub
ErrHandler:
If Err.Number <> 0 Then
MsgBox Err.Description & "!", vbCritical, "QQ "
Debug.Print Err.Description
Err.Clear
Resume ErrorExit
End If
End Sub
Public Sub AppSettings(Optional IsStart As Boolean = True)
If IsStart Then
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual
Application.StatusBar = ">>>>>>>>Macro Is Running>>>>>>>>"
Else
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic
Application.StatusBar = False
End If
End Sub
Public Function RegGetArray(ByVal Pattern As String, ByVal OrgText As String) As String()
Dim Reg As Object, Mh As Object, OneMh As Object
Dim Arr() As String, Index As Long
Dim Elm As String
Set Reg = CreateObject("Vbscript.Regexp")
With Reg
.MultiLine = True
.Global = True
.Ignorecase = False
.Pattern = Pattern
Set Mh = .Execute(OrgText) Index = 0
ReDim Arr(1 To 1)
For Each OneMh In Mh
Index = Index + 1
ReDim Preserve Arr(1 To Index)
'If OneMh.submatches(0) <> "" Then Elm = OneMh.submatches(0)
Arr(Index) = OneMh.submatches(0) Next OneMh
End With
RegGetArray = Arr
Set Reg = Nothing
Set Mh = Nothing
End Function
Public Function RegTest(ByVal OrgText As String, ByVal Pattern As String) As Boolean
'传递参数 :原字符串, 匹配模式
Dim Regex As Object
Set Regex = CreateObject("VBScript.RegExp")
With Regex
.Global = True
.Pattern = Pattern
End With
RegTest = Regex.TEST(OrgText)
Set Regex = Nothing
End Function
20170906xlVBA_GetEMailFromDocument的更多相关文章
随机推荐
- topcoder srm 663 div1
problem1 link 每次枚举$S$的两种变化,并判断新的串是否是$T$的子串.不是的话停止搜索. problem2 link 首先考慮增加1个面值为1的硬币后,$ways$数组有什么变化.设原 ...
- C# 尝试读取或写入受保护的内存。这通常指示其他内存已损坏
用管理员身份运行CMD,输入netsh winsock reset并回车(注意,必须是已管理员身份运行,这个重置LSP连接)运行后提示要重启生效,结果没重启就OK了(重启不重启看最终效果).
- Bootstrap3基础 input-group-btn 按钮与输入框 横向组合
内容 参数 OS Windows 10 x64 browser Firefox 65.0.2 framework Bootstrap 3.3.7 editor ...
- Bootstrap3基础 form-inline 输入框在同一行
内容 参数 OS Windows 10 x64 browser Firefox 65.0.2 framework Bootstrap 3.3.7 editor ...
- SpringBoot 整合携程Apollo配置管理中心
携程官网对apollo的使用讲解了很多种方式的使用,但是感觉一些细节还是没讲全,特别是eureka配置中心地址的配置 这里对springboot整合apollo说一下 >SpringBoot启动 ...
- POJ 1873 The Fortified Forest(凸包)题解
题意:二维平面有一堆点,每个点有价值v和删掉这个点能得到的长度l,问你删掉最少的价值能把剩余点围起来,价值一样求删掉的点最少 思路:n<=15,那么直接遍历2^15,判断每种情况.这里要优化一下 ...
- js动画(速度)
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta ht ...
- arm中断体系结构
ARM处理器中有7种类型的异常,按优先级从高到低的排列如下: 复位异常(Reset). 数据异常(Data Abort). 快速中断异常(FIQ) ...
- Java 静态方法不能重写但可以被子类静态方法覆盖
强调 静态方法是属于类的,只存在一份,会被该类的所有对象共享.不可以被重写. 静态方法可以被子类继承,但是不可以被子类重写 class door{ } class wood_Door extends ...
- springboot集成logback日志
简介 spring boot内部使用Commons Logging来记录日志,但也保留外部接口可以让一些日志框架来进行实现,例如Java Util Logging,Log4J2还有Logback. 如 ...