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的更多相关文章

随机推荐

  1. Mac 配置教程-日常篇

    今年终于在推出 2018 款 MBP 时,看到升级了 CPU,我就果断下手「拔草」.本文记录使用 Mac 的一些配置,会长期更新. 为了控制文章的篇幅,我将 Mac 使用配置分成了两篇: Mac 配置 ...

  2. 【lg1799】数列

    题目描述 虽然msh长大了,但她还是很喜欢找点游戏自娱自乐.有一天,她在纸上写了一串数字:1,l,2,5,4.接着她擦掉了一个l,结果发现剩下l,2,4都在自己所在的位置上,即1在第1位,2在第2位, ...

  3. 1823: [JSOI2010]满汉全席 2-sat

    链接 https://www.lydsy.com/JudgeOnline/problem.php?id=1823 思路 建图,缩点tarjan 判断impossible 代码 #include < ...

  4. R语言 apply,sapply,lapply,tapply,vapply, mapply的用法

    apply() apply(m,dimcode,f,fargs) m 是一个矩阵. dimcode是维度编号,取1则为对行应用函数,取2则为对列运用函数. f是函数 fargs是f的可选参数集 > ...

  5. 题解——洛谷P4095 [HEOI2013]Eden 的新背包问题(背包)

    思路很妙的背包 用了一些前缀和的思想 去掉了一个物品,我们可以从前i-1个和后i+1个推出答案 奇妙的思路 #include <cstdio> #include <algorithm ...

  6. Images之base image

    Create a base image Most Dockerfiles start from a parent image. If you need to completely control th ...

  7. Markdown 指南

    Markdown 是一种轻量级的「标记语言」,使用用特殊的 Markdown 文档处理器将 Markdown 语法翻译成预设的文档格式.标题大小等,一般用于展示时输出的是 HTML.这个教程可以让使用 ...

  8. 解决github网站打开慢的问题

    一.前言 作为一名合格的程序员,github打开速度太慢怎么能容忍.但是可以通过修改hosts文件信息来解决这个问题.现在chrome访问github速度杠杠的! 二.macOS解决方法 打开host ...

  9. _itemmod_currency_like

    设置物品掉落模式为货币类型功能:掉落的时候 所有人都可以拿,就像公正徽章,每个人都会获得一个.小技巧:配合DBC使用,可以将该道具其显示在角色栏的货币中.1.转存item_template,在item ...

  10. python 目录切换

    #- * -coding: utf - - * - import os, sys path = "c:\\" # 查看当前工作目录 retval = os.getcwd() pri ...